HTML · 2022年5月26日 0

在Linux安装Nginx

1:下载nginx的tar包,网址如下:

https://nginx.org/en/download.html

2:使用xshell或其它方法上传tar包到Linux服务器(本文使用的是NxShell)

NxShell链接:https://nxshell.github.io/

3:解压tar包

tar -xvf nginx-1.22.0.tar.gz

4:进入解压出来的文件夹

cd nginx-1.22.0

5:安装nginx

进入文件夹后可以用ll查看文件夹内容

使用:./configure 进行安装

问题处理

A:缺少C依赖

使用:yum install -y gcc 安装依赖

B:缺少pcre依赖

使用:yum install -y pcre pcre-devel 安装依赖

C:缺少zlib依赖

使用:yum install -y zlib zlib-devel 安装依赖

当看到下面这张图代表依赖没有问题了:

6:接下来执行

1.make
2.make install

7:启动nginx

首先找到nginx,使用命令 whereis nginx,然后进入nginx文件夹,然后进入sbin

./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置

查看nginx是否启动:ps -ef | grep nginx

8:开放端口

开放指定端口:firewall-cmd --add-port=80/tcp --permanent
移除指定端口:firewall-cmd --permanent --remove-port=80/tcp
重启防火墙:firewall-cmd --reload
查看指定端口是否开放成功:firewall-cmd --query-port=80/tcp

9:安装成系统服务

创建服务脚本 vi /usr/lib/systemd/system/nginx.service
粘贴以下代码进去wq保存退出q!不保存退出

[Unit]
Description=nginx - web server
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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

重新加载系统服务 systemctl daemon-reload
启动服务 systemctl start nginx.service
使用:ps -ef|grep nginx 查看nginx进程
使用:kill xxx 关闭nginx