1. 安装依赖包
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
2. 下载nginx包,下载到 /home/software
wget https://nginx.org/download/nginx-1.16.0.tar.gz
3.解压
tar zxvf nginx-1.16.0.tar.gz
4.安装
cd /home/software/nginx-1.16.0
5. 编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错
mkdir /var/temp/nginx -p
6.为nginx pid创建目录,防止重启nginx报: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
mkdir /usr/local/nginx/logs -p
7. 在nginx解压目录,输入如下命令进行配置,目的是为了创建makefile文件
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/locak/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_ssl_module
8.编译并安装
make && make install
9. 进入/usr/local/nginx/sbin目录查看nginx版本
./nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/locak/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi
10.将nginx交给systemd管理
10.1在系统服务目录里创建nginx.service文件
vi /usr/lib/systemd/system/nginx.service
10.2 写入
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
11.
vi /usr/local/nginx/conf/nginx.conf
# 添加
pid /usr/local/nginx/logs/nginx.pid;
12. 开启、关闭、状态、重启
systemctl enable nginx #开启启动
systemctl status nginx #状态
systemctl start nginx #启动nginx
systemctl stop nginx #关闭nginx
systemctl restart nginx #重启nginx
12.启动后,访问ip:80
参考: https://www.jianshu.com/p/ca5ee5f7075c