一、Nginx的安装版本分为:
Mainline version(主要开发版本,其实就是还处于开发版)
Stable version(当前最新稳定版)
Legacy versions(旧的稳定版)
nginx官网下载地址:https://nginx.org/en/download.html
二、nignx安装(推荐源码安装)
1、yum安装
1)配置下载源:
[root@s1 ~]# yum install epel-release -y # 下载epel依赖源
[root@s1 ~]# yum install -y nginx # 下载nginx
[root@s1 ~]# rpm -ql nginx # 查看nginx的配置文件有哪些
[root@s1 ~]# which nginx # 查看nginx程序的路径
/usr/sbin/nginx
2)检查安装与查看帮助
# 查看nginx安装包信息
[root@s1 ~]# rpm -qi nginx
# 使⽤安装完成的⼆进制⽂件nginx
[root@s1 ~]# nginx -h
3)验证nginx配置是否正常,查看版本
[root@s1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@s1 ~]# nginx -V
nginx version: nginx/1.16.1
4)启动脚本
[root@s1 ~]# cat /usr/lib/systemd/system/nginx.service
[Service]
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
5)配置Nginx:
默认配置⽂件:/etc/nginx/nginx.conf,,默认配置如下:
[root@s1 ~]# grep -v "#" /etc/nginx/nginx.conf | grep -v "^$"
user nginx;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
http {
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
6)启动Nginx:
[root@s1 ~]# systemctl start nginx # 启动服务
[root@s1 ~]# systemctl status nginx # 查看启动状态是否成功
查看nginx进程:
[root@VM-4-16-centos ~]# ps -aux | grep nginx
root 21665 0.0 0.0 39308 940 ? Ss 10:51 0:00 nginx: master process /usr/sbin/nginx
nginx 21666 0.0 0.1 41780 1940 ? S 10:51 0:00 nginx: worker process
nginx 21667 0.0 0.1 41780 1940 ? S 10:51 0:00 nginx: worker process
root 22154 0.0 0.0 112812 976 pts/0 S+ 10:52 0:00 grep --color=auto nginx
7)访问nginx
2、源码安装
1)准备编译安装的环境
[root@s2 ~]# yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ \
glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel nettools iotop bc zip unzip \
zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
- 准备源码包并解压安装
安装Nginx:
官⽅源码包下载地址: https://nginx.org/en/download.html
[root@s2 ~]# cd /usr/local/src/ # 安装路径
[root@s2 src]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@s2 src]# tar xf nginx-1.16.1.tar.gz
[root@s2 src]# cd nginx-1.16.1/
准备配置文件安装:
[root@s2 nginx-1.16.1]# ./configure --prefix="/apps/nginx" \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
3)编译安装并赋权操作
[root@s2 nginx-1.16.1]# make #编译步骤,根据Makefile⽂件⽣成相应的模块
[root@s2 nginx-1.16.1]# make install #创建⽬录,并将⽣成的模块和⽂件复制到相应的⽬录:
[root@s2 nginx-1.16.1]# useradd nginx -s /sbin/nologin -u 2000 #以普通⽤⼾启动nginx
[root@s2 nginx-1.16.1]# chown nginx.nginx -R /apps/nginx/ # 添加目录权限
4)验证版本及编译参数
[root@s2 nginx-1.16.1]# /apps/nginx/sbin/nginx -V
5) 创建Nginx⾃启动脚本–nginx.service:
[root@s1 ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/apps/nginx/sbin/nginx -t
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6)修改配置文件
vim /apps/nginx/conf/nginx.conf
解开注释:
pid /run/nginx.pid;
注意:(/usr/lib/systemd/system/nginx.service和/apps/nginx/conf/nginx.conf文件的PIDFile=/run/nginx.pid保持一致即可)
7)验证Nginx⾃启动脚本
[root@VM-4-16-centos nginx-1.22.0]# ln -s /apps/nginx/sbin/nginx /usr/bin/nginx # 添加nginx程序软链接
# 服务配置和启动
[root@s2 nginx-1.16.1]# systemctl daemon-reload
[root@s2 nginx-1.16.1]# systemctl start nginx
[root@s2 nginx-1.16.1]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@s2 nginx-1.16.1]# systemctl status nginx
8)访问编译安装的nginx web界⾯:
三、部署过程中的问题:
1、启动成功但有报错
报错:
Can't open PID file /run/nginx.pid (yet?) after start-post: No such file or directory
Jun 14 11:28:12 VM-4-16-centos systemd[1]: nginx.service start operation timed out. Terminating.
Jun 14 11:28:12 VM-4-16-centos systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
原因:Nginx自身Bug问题,导致Nginx服务优化后出现“Can’t open PID file /var/run/nginx.pid (yet?) after start: No such…ctory”报错
解决方法:
以下两个服务文件和配置文件对PID的描述保持一致
/usr/lib/systemd/system/nginx.service
PIDFile=/run/nginx.pid
/apps/nginx/conf/nginx.conf
pid /run/nginx.pid;