执行安装
官方文档:官方文档
首先安装必备软件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
下载源码到 /usr/local/src/
cd /usr/local/src/
进行下载(可自行修改版本号)
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar zxvf nginx-1.16.1.tar.gz
解压后进入nginx目录:添加配置
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
编译
make && make install
安装完成
测试是否成功:查看版本
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
同时查看安装的插件
[root@localhost nginx-1.18.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-threads
设置开机启动 和 systemctl 控制
有两种方式:
1. 配置 /etc/init.d/nginx 官方脚本配置
创建启动文件,将脚本添加进去,根据自己的安装位置,适当修改脚本中 nginx安装路径与配置路径;
仔细观察脚本,脚本里面的nginx的相关路径与本机安装的路径不一致。
nginx文件 | 本机路径 | 脚本路径 |
---|---|---|
nginx执行文件 | /usr/bin/nginx | /usr/sbin/nginx |
nginx.conf配置文件 | /usr/local/nginx/conf/nginx.conf | /etc/nginx/nginx.conf |
# !bin/bash 若出现 目标目录(后面的)不存在就自行创建
ln -s /usr/bin/nginx /usr/sbin/nginx
ln -sfn /usr/local/nginx/conf/ /etc/nginx/
编辑文本
vim /etc/init.d/nginx
添加权限
chmod a+x /etc/init.d/nginx
2. 创建 /usr/lib/systemd/system/nginx.service 文件
创建文件,将脚本粘贴进去
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
~
~
即可使用命令:systemctl start nginx
若出现 报错:
[root@localhost nginx-1.18.0]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; static; vendor preset: disabled)
Active: failed (Result: exit-code) since 四 2020-12-17 21:25:07 CST; 2s ago
Docs: http://nginx.org/en/docs/
Process: 4290 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=1/FAILURE)
12月 17 21:25:07 localhost.basic systemd[1]: [/usr/lib/systemd/system/nginx.service:22] Failed to parse boolean val...arget
12月 17 21:25:07 localhost.basic systemd[1]: Starting nginx - high performance web server...
12月 17 21:25:07 localhost.basic nginx[4290]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
12月 17 21:25:07 localhost.basic nginx[4290]: nginx: [emerg] getpwnam("www") failed
12月 17 21:25:07 localhost.basic nginx[4290]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
12月 17 21:25:07 localhost.basic systemd[1]: nginx.service: control process exited, code=exited status=1
12月 17 21:25:07 localhost.basic systemd[1]: Failed to start nginx - high performance web server.
12月 17 21:25:07 localhost.basic systemd[1]: Unit nginx.service entered failed state.
12月 17 21:25:07 localhost.basic systemd[1]: nginx.service failed.
nginx: [emerg] getpwnam("www") failed 表示没有这个用户,请到nginx.conf 中修改 #user nobody 或者 添加相应的用户