1.安装依赖库
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
如果已经安装的可以不重复安装。
2.下载Nginx
官网:http://nginx.org/en/download.html
3.安装
解压:
tar -zxvf nginx-1.16.1.tar.gz
指定安装目录,生成 Makefile,为下一步的编译做准备:
cd nginx-1.16.1/
./configure --prefix=/usr/softinstall/nginx
编译:
make
安装:
make install
4.配置nginx开机启动:
1)切到"/usr/lib/systemd/system"目录,创建nginx.service文件:
cd /usr/lib/systemd/system
vim nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/softinstall/nginx/sbin/nginx -c /usr/softinstall/nginx/conf/nginx.conf
ExecReload=/usr/softinstall/nginx/sbin/nginx -s reload
ExecStop=/usr/softinstall/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]服务安装的相关设置,可设置为多用户
2)退出并保存新建的"nginx.service"
文件后,执行以下命令使nginx开机启动:
systemctl enable nginx.service
执行开机自启命令后,会在/etc/systemd/system/multi-user.target.wants/目录下生成nginx.service的软链接,multi-user.target.wants目录步骤1)的WantedBy制定的运行级别,此时可以执行相关命令操作nginx服务:
[root@localhost system]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
systemctl enable nginx.service //开机启动nginx服务
systemctl disable nginx.service //禁止开机启动nginx服务
systemctl is-enable nginx.service //查询是否开机启动nginx服务
systemctl start nginx.service //启动nginx服务
systemctl stop nginx.service //停止nginx服务
systemctl reload nginx.service //重新加载nginx服务
systemctl status nginx.service //查看nginx服务状态
3) 启动Nginx
systemctl start nginx.service