1、安装依赖包(这些如果安装过可以跳过)
yum install gcc-
yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel
2、下载nginx安装
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx
make
make install
3、安装好试试Nginx是否能用
cd /usr/local/nginx/sbin
#启动
./nginx
#看80端口是否被nginx占用如果是就说明启动成功
netstat -ntlp |80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8496/nginx: master
#停止
./nginx -s stop
#判断配置文件是否正确
./nginx -t
4、生成服务
在 /usr/lib/systemd/system下创建nginx.service
vim /usr/lib/systemd/system/nginx.service
拷贝下面的内容到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 daemon-reload
常用命令
#启动服务
systemctl start nginx.service
#停止服务
systemctl stop nginx.service
#开机启动
systemctl enable nginx.service
#关闭开机启动
systemctl disable nginx.service
#pid文件默认在/nginx/logs/下面,如果想修改pid位置需要在/nginx/conf/nginx.conf中修改
vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#这里设置pid位置
pid logs/nginx.pid;