nginx 源码包下载地址:
http://nginx.org/en/download.html
1)下载依赖库:
[root@localhost ~]# yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
2) 创建nginx用户
[root@localhost sbin]# useradd -s /sbin/nologin -M nginx #否则会出现问题1
3)解压nginx:
[root@localhost ~]# mkdir /downloads
[root@localhost ~]# cd /downloads/
[root@localhost download]# tar -xzf nginx-1.16.1.tar.gz -C /usr/local/
4) 编译安装
[root@localhost local]# cd /usr/local/
[root@localhost local]# mkdir -p /usr/local/nginx /var/tmp/nginx/client/
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
[root@localhost nginx-1.16.1]# make
[root@localhost nginx-1.16.1]# make install
5)启动nginx:
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -t #检查配置文件语法问题
显示结果如下为成功:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# /usr/local/nginx/sbin/nginx #启动nginx,可能会出现问题2,/var/run/nginx/nginx.pid找不到的问题
6) 添加环境变量
[root@localhost init.d]# vim /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@localhost init.d]# source /etc/profile
7)查看端口80:
[root@localhost sbin]# ss -lntp
8)nginx的相关动作:
nginx -s reload 重新载入配置文件
nginx -s reopen 重启 Nginx
nginx -s stop 快速关闭 Nginx
nginx -s quit 关闭Nginx
扩展1:若想用systemctl启动方式启动nginx服务,可以进行如下修改:
[root@localhost sbin]# vim /usr/lib/systemd/system/nginx.service
添加如下内容:
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost sbin]# systemctl daemon-reload #重新加载配置文件,便可进行systemctl命令使用
问题1:
由于没有nginx用户,会出现如下问题:
解决方法:
[root@localhost sbin]# useradd -s /sbin/nologin -M nginx
问题2:
/var/run/nginx/nginx.pid文件打不开
解决方法:
[root@localhost nginx]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
将原来的#pid logs/nginx.pid; 去掉注释
并改为:pid /usr/local/nginx/nginx.pid;
再次启动就可以了
实验结果显示:
访问ip可得: