Nginx是一款轻量级的web服务器,同时也是一款轻量级的反向代理服务器。对于web开发人员来说,主要用来做Http反向代理,负载均衡以及动静分离等功能。本次学习过程基于CentOS 6.8(64),并采用源码方式来安装Nginx(1.12.2)。
首先,创建用来运行Nginx的用户和用户组
[root@nginx1 ~]# groupadd nginx
[root@nginx1 ~]# useradd nginx -s /sbin/nologin -M -g nginx
接着,安装Nginx依赖
[root@nginx1 ~]# yum install -y gcc pcre* zlib zlib-devel
# 如果需要支持ssl,那么还需要安装openssl
[root@nginx1 ~]# yum install -y openssl openssl-devel
最后,编译安装Nginx
[root@nginx1 ~]# cd /root/softwares/
[root@nginx1 softwares]# ls -l
total 5980
-rw-r--r--. 1 root root 6119936 Mar 22 13:42 nginx-1.12.2.tar
[root@nginx1 softwares]# tar -xf nginx-1.12.2.tar
[root@nginx1 softwares]# cd nginx-1.12.2
# 在configure的最后指定了五个临时目录是因为如果不指定的话,那么在运行Nginx之后默认就会在Nginx的安装目录下生成这五个目录,有强迫症的我并不希望在运行后把Nginx的安装目录搞得乱糟糟的,所以统一指定到Nginx安装目录的temp目录下
# 另外,可以使用./configure --help来查看更多的安装配置选项
[root@nginx1 nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx/1.12.2 \
--user=nginx \
--group=nginx \
--http-client-body-temp-path=/usr/local/nginx/1.12.2/temp/client_body_temp \
--http-proxy-temp-path=/usr/local/nginx/1.12.2/temp/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/1.12.2/temp/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/1.12.2/temp/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/1.12.2/temp/scgi_temp
[root@nginx1 nginx-1.12.2]# make && make install
[root@nginx1 nginx-1.12.2]# cd /usr/local/nginx/1.12.2/
[root@nginx1 1.12.2]# ls -l
total 16
drwxr-xr-x. 2 root root 4096 Mar 22 14:23 conf
drwxr-xr-x. 2 root root 4096 Mar 22 14:23 html
drwxr-xr-x. 2 root root 4096 Mar 22 14:23 logs
drwxr-xr-x. 2 root root 4096 Mar 22 14:23 sbin
# 由于configure时指定了五个临时目录在temp目录下,该目录在Nginx的运行过程中是不会主动创建的,必须手动创建,否则启动时候会报错
[root@nginx1 1.12.2]# mkdir temp
[root@nginx1 1.12.2]# tree
.
|-- conf
| |-- fastcgi.conf
| |-- fastcgi.conf.default
| |-- fastcgi_params
| |-- fastcgi_params.default
| |-- koi-utf
| |-- koi-win
| |-- mime.types
| |-- mime.types.default
| |-- nginx.conf
| |-- nginx.conf.default
| |-- scgi_params
| |-- scgi_params.default
| |-- uwsgi_params
| |-- uwsgi_params.default
| `-- win-utf
|-- html
| |-- 50x.html
| `-- index.html
|-- logs
|-- sbin
| `-- nginx
`-- temp
5 directories, 18 files
此时Nginx就安装完成了,接下来就可以运行Nginx来玩玩啦!启动Nginx后,打开网页访问80端口能够得到下面的样子就表示Nginx正确安装并成功启动了,如果访问不了,请注意是否被防火墙拦截了。
[root@nginx1 1.12.2]# pwd
/usr/local/nginx/1.12.2
[root@nginx1 1.12.2]# ./sbin/nginx
[root@nginx1 1.12.2]#
[root@nginx1 1.12.2]# ps axu | grep nginx | grep -v grep
root 12002 0.0 0.0 20004 648 ? Ss 14:26 0:00 nginx: master process ./sbin/nginx
nginx 12003 0.0 0.1 20448 1248 ? S 14:26 0:00 nginx: worker process
[root@nginx1 1.12.2]#
# 除此之外,还可以使用nginx -s quit来停止Nginx,以及使用nginx -s reload来重启Nginx
[root@nginx1 1.12.2]# ./sbin/nginx -s stop