环境准备
一台cenots7虚拟机或者一台centos7云服务器
服务器可以连接网路
下载
第一种:使用wegt
安装wegt命令yum install -y wegt
在虚拟机根目录执行wget [http://nginx.org/download/nginx-1.18.0.tar.gz](http://nginx.org/download/nginx-1.18.0.tar.gz)
后面的版本可以根据自己的选择来下载,具体的版本参考download。
然后使用tar zxvf nginx-1.18.0.tar.gz解压缩
第二种:下载压缩包
打开下载地址,这里可以根据自己的选择下载不同的版本,我下的是1.22.1
1.使用xshell或者ssh工具把压缩包直接上传到服务器,
2.使用tar zxvf nginx-1.22.1.tar.gz解压缩
安装必须的依赖
1.yum install -y gcc
2. yum install -y pcre pcre-devel
3.yum install -y lib lib-devel
安装
进入刚才解压的nginx,cd nginx-1.22.1目录下,执行./configure --prefix=/usr/local/nginx —prifix=安装目录
执行make && make install
即可在/usr/local/下安装nginx
进入/usr/local/下,可以看到nginx已经安装了
启动Nginx
现在我们安装完了,并没有任何脚本去自动启动,先使用手动的方式去启动
进入/usr/local/nginx/sbin目录
执行./nginx,即可启动nginx.
使用ps -ef | grep nginx查看一下,可以看到nginx已经启动了
在浏览器验证一下,在浏览器输入IP地址(可以使用ip addr查看),发现无法访问。是因为我们的防火墙没有关闭。
systemctl stop firewalld.service // 关闭防火墙systemctl disable firewalld.service // 关闭开机自启动
再次验证一下,就可以打开了。
其他nginx命令如下:
./nginx // 启动./nginx -s stop //快速停止./nginx -s quit // 优雅关闭,在退出前完成已经接受的连接请求./nginx -s reload // 重新加载配置
关闭火墙
firewall-cmd --reload // 重启防火墙
安装成系统服务
创建服务脚本vi /usr/lib/systemd/system/nginx.service
按a,复制下面的内容
[Unit]Description=nginx - web serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopExecQuit=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true[Install]WantedBy=multi-user.target
使用:wq保存
重新系统加载服systemctl daemon-reload
停掉之前启动的nginx./nginx -s stop
启动nginx服务systemctl start nginx.service
设置开机启动systemctl enable nginx.service
再次测试
关掉服务器reboot,nginx服务停掉了,我们再次打开虚拟机,打开浏览器,nginx开机启动正常.
