1.安装依赖
yum install gcc gcc-c++ make automake autoconf libtool pcre* zlib openssl openssl-devel
2.下载tar包
wget http://nginx.org/download/nginx-1.15.4.tar.gz
3.解压
tar -zxvf nginx-1.15.4.tar.gz #解压
cd nginx-1.15.4/
4.编译安装
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--build=CentOS \
--http-log-path=/var/log/nginx/access.log \
--with-http_stub_status_module\
make && make install #编译安装
5.nginx启动命令加入service
ln -s /usr/lib64/nginx/modules /etc/nginx/modules
Will throw this error: nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
Just create directory
mkdir -p /var/cache/nginx
[emerg]: getpwnam(“nginx”) failed
需要创建nginx 用户
useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx
创建service
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
保存退出
6.启动服务
如果nginx启动着,需要先停掉
systemctl start nginx.service
nginx加入开机自动启动
systemctl enable nginx.service
重启一下,检查启动之后nginx是否自动启动
验证是否加入开机自动启动
systemctl is-enabled nginx.service
enabled
enabled 可以正常使用 service nginx start
7.nginx版本升级
下载最新版本tar包
wget http://nginx.org/download/nginx-1.21.0.tar.gz
查看现有nginx编译参数
nginx -V
解压新的tar包,复制编译参数,覆盖安装最新版本nginx
tar -zxvf nginx-1.21.0.tar.gz
cd ./nginx-1.21.0
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--build=CentOS \
--http-log-path=/var/log/nginx/access.log \
--with-http_stub_status_module\
make && make install #编译安装
备份原有的nginx可执行文件,复制最新的nginx脚本到原有目录
cd ./nginx-1.21.0/obj
mv /usr/sbin/nginx /usr/sbin/nginx.old
cp ./nginx /usr/sbin/nginx
测试nginx配置文件
nginx -t
没问题重启nginx即可