Linux(CentOS)下,下载安装Nginx并配置

Linux(CentOS)下,下载安装Nginx并配置

准备工作

安装GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL

查看gcc是否安装

1
2
3
4
5
$ gcc
如果出现“gcc: no input files”信息,说明已经安装好了.
否则
$ yum install gcc # 安装gcc,一路输入 y
$ gcc -v # 查看版本号

同样方法安装pcre zlib openssl(其中devel,是develop开发包的意思)

1
2
3
$ yum install -y pcre pcre-devel
$ yum install -y zlib zlib-devel
$ yum install -y openssl openssl-devel

下载并安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ mkdir nginx-src && cd nginx-src
$ wget http://nginx.org/download/nginx-1.8.0.tar.gz
$ tar xzf nginx-1.8.0.tar.gz
$ cd nginx-1.8.0
$ ./configure
$ make
$ make install
$ whereis nginx
# nginx: /usr/local/nginx
# 默认的安装路径为:/usr/local/nginx;跳转到其目录下sbin路径下,便可以启动或停止它了。
$ cd /usr/local/nginx/sbin
$ ./nginx -h #help
$ nginx #启动
$ nginx -s stop #停止

安装完之后信息说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

添加到系统服务

使用命令“# vi /etc/init.d/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
#!/bin/sh
# chkconfig: 2345 85 15
# Startup script for the nginx Web Server
# description: nginx is a World Wide Web server.
# It is used to serve HTML files and CGI.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx deamon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
test -x $DAEMON || exit 0
d_start(){
$DAEMON || echo -n "already running"
}
d_stop(){
$DAEMON -s quit || echo -n "not running"
}
d_reload(){
$DAEMON -s reload || echo -n "can not reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC conf..."
d_reload
echo "reload ."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo "."
;;
*)
echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

保存退出后,再使用下面的命令,使其可执行;然后,添加配置并查看。
可用chkconfig修改其值,也可用ntsysv工具改变是否自启动。

1
2
3
4
5
$ chmod +x /etc/init.d/nginx
$ chkconfig --add nginx
$ chkconfig nginx on/off
$ chkconfig --list nginx
# nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

配置文件分散管理,include /usr/local/nginx/conf.d/*.conf;

1
2
3
4
5
6
7
8
$ cd /usr/local/nginx/conf
$ vim nginx.conf
# 加入 include /usr/local/nginx/conf.d/*.conf;
$ cd /usr/local/nginx
$ mkdir conf.d
$ cd conf.d
$ touch a.conf
# a.conf 加入配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://114.215.93.103:8080;
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
proxy_redirect off;
proxy_ignore_client_abort on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 512k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
# access_log /opt/wwwlogs/iplat_caihuike_com_access.log;
}
}

注意:nginx.conf里面的默认配置需要注释掉 80 的 server 配置,防止重复配置 localhost

测试

1
2
3
$ cd /usr/local/nginx
$ ./sbin/nginx -t # 测试
$ ./sbin/nginx -s reload # 重启