CentOS6.5上编译安装Nginx服务

目录大纲

  • 1.系统环境以及服务插件介绍
  • 2.安装配置nginx
    • 2.1.安装nginx
    • 2.2.配置nginx
    • 2.3.测试nginx

1.系统环境以及服务插件介绍

系统:centos 6.5-x86_64
服务:

  • nginx版本:1.11.2

依赖和工具:

2.安装配置nginx

2.1.安装nginx

1
2
3
4
5
6
7
8
[root@chmod src]# tar -zxvf pcre-8.39.tar.gz
[root@chmod src]# tar -zxvf zlib-1.2.8.tar.gz
[root@chmod src]# tar -zxvf openssl-1.0.2h.tar.gz
[root@chmod src]# tar -zxvf nginx-1.11.2.tar.gz
[root@chmod src]# mkdir /data/nginx
[root@chmod src]# cd nginx-1.11.2
[root@chmod nginx-1.11.2]# ./configure --prefix=/data/nginx --pid-path=/data/nginx/nginx.pid --error-log-path=/data/nginx/log/error.log --http-log-path=/data/nginx/log/access.log --with-http_ssl_module --with-pcre=/data/src/pcre-8.39 --with-zlib=/data/src/zlib-1.2.8 --with-openssl=/data/src/openssl-1.0.2h
[root@chmod nginx-1.11.2]# make && make install

2.2.配置nginx

2.2.1.添加nginx运行用户,添加虚拟机主机配置,添加网站根目录:
1
2
3
4
5
[root@chmod ~]# useradd -M -s /sbin/onlogin
[root@chmod ~]# mkdir /www/nginx/test -p
[root@chmod ~]# echo hello > /www/nginx/test/index.html
[root@chmod ~]# chown -R www.www /www/nginx
[root@chmod ~]# mkdir /data/nginx/conf/vhost
2.2.2.修改nginx的主配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@chmod conf]# cat nginx.conf
user www www;

worker_processes auto;

error_log /data/nginx/log/error.log crit;
pid /data/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 51200;
multi_accept on;
}

http
{
include mime.types;
default_type application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";

#limit_conn_zone $binary_remote_addr zone=perip:10m;
##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

server_tokens off;
access_log off;

include vhost/*.conf;
}
2.2.3.配置一个测试虚拟主机网站:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@chmod vhost]# cat test.conf 
server
{
listen 80;
#listen [::]:80;
server_name 192.168.0.243;
index index.html index.htm index.php default.html default.htm default.php;
root /www/nginx/test;

#error_page 404 /404.html;

location ~ [^/]\.php(/|$)
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
try_files $fastcgi_script_name =404;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

location ~ /\.
{
deny all;
}

access_log off;
}

2.3.测试nginx

2.3.1.启动服务:
1
[root@chmod ~]# /data/nginx/sbin/nginx
2.3.2.防火墙放行端口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@chmod ~]# iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
[root@chmod ~]# service iptables save
[root@chmod www]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:8080
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
2.3.3.访问测试:

1.png

到此nginx配置完成,访问正常。但是仅仅有nginx,只能提供静态页面的访问,做动态网站,还需要php和mysql的支持。

文章目录
  1. 目录大纲
  2. 1.系统环境以及服务插件介绍
  3. 2.安装配置nginx
    1. 2.1.安装nginx
    2. 2.2.配置nginx
      1. 2.2.1.添加nginx运行用户,添加虚拟机主机配置,添加网站根目录:
      2. 2.2.2.修改nginx的主配置文件
      3. 2.2.3.配置一个测试虚拟主机网站:
    3. 2.3.测试nginx
      1. 2.3.1.启动服务:
      2. 2.3.2.防火墙放行端口:
      3. 2.3.3.访问测试: