在CentOS 7中安装Nginx。
当使用以下命令安装Nginx时,发现无法安装成功。
yum install -y nginx
[如果出现没有可用软件包]需要做一点处理。
安装Nginx源
执行以下命令:
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装该rpm后,我们就能在/etc/yum.repos.d/
目录中看到一个名为nginx.repo
的文件。
安装Nginx
安装完Nginx源后,就可以正式安装Nginx了。
yum install -y nginx
Nginx默认目录
输入命令:
whereis nginx
即可看到类似于如下的内容:
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx
以下是Nginx的默认路径:
(1) Nginx配置路径:/etc/nginx/
(2) PID目录:/var/run/nginx.pid
(3) 错误日志:/var/log/nginx/error.log
(4) 访问日志:/var/log/nginx/access.log
(5) 默认站点目录:/usr/share/nginx/html
事实上,只需知道Nginx配置路径,其他路径均可在/etc/nginx/nginx.conf
以及/etc/nginx/conf.d/default.conf
中查询到。
2、通过压缩包进行安装
1、下载安装包 nginx: download
![](https://img-blog.csdnimg.cn/img_convert/55d48a80ef574e6fd6fdc4b7bc544290.png)
右键,复制链接地址,在liunx中使用 wget获取【我是在 root 根目录下执行的下载,他会下载到root目录下】
![](https://img-blog.csdnimg.cn/img_convert/6ee91c401067f7e6da7988da5322cef3.png)
2、解压缩【我将压缩文件剪贴到了 /root/share/nginx目录下】
![](https://img-blog.csdnimg.cn/img_convert/673a13c9143c144a154e8abee1f93cc1.gif)
[root@localhost share]# mkdir nginx
[root@localhost share]# mv /root/nginx-1.20.2.tar.gz nginx
[root@localhost share]# ls
app centos nginx webapp
[root@localhost share]# ls nginx
nginx-1.20.2.tar.gz
[root@localhost share]# cd nginx
[root@localhost nginx]# tar -zxf nginx-1.20.2.tar.gz
[root@localhost nginx]# ls
nginx-1.20.2 nginx-1.20.2.tar.gz
![](https://img-blog.csdnimg.cn/img_convert/ec71e0c7993d70dc53adaea0df02b18c.gif)
3、进入箭头所指的文件夹
![](https://img-blog.csdnimg.cn/img_convert/5538a7b41029526eecff97be6e99ac09.png)
4、通过 ./configure 进行nginx配置
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx
按下enter键,执行
![](https://img-blog.csdnimg.cn/img_convert/2f813792ad159d9b4a55d6b939472c93.png)
5、编译 并 安装 【一次执行两个指令】
make && make install
![](https://img-blog.csdnimg.cn/img_convert/1a3d182a54b0e9ae01439e3195871cb7.png)
如果出现上述问题,说明缺少依赖,安装依赖
执行
yum -y install gcc openssl openssl-devel pcre-deve
再次执行 ./configure 进行基本配置
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx
再次编译 并 安装 【一次执行两个指令】,本次就不会报错了
make && make install
![](https://img-blog.csdnimg.cn/img_convert/b785bbae4b984ee4d36fb4c6d8e8678e.png)
这样压缩包安装就OK了,在这里建议使用压缩包安装,因为yum安装找不到 ./configure 目录,那么我们无法添加 ssl 和 stream 支持。
Nginx 常用命令
(1) 启动:
nginx
(2) 测试Nginx配置是否正确:
nginx -t
(3) 优雅重启:
nginx -s reload
安装Nginx后的注意事项:
Nginx默认使用80端口,因为Linux有防火墙,因此,我们需要查看 80 端口是否开启,如果没有开启,需要开启
1、查看开放的端口信息
irewall-cmd --list-all
![](https://img-blog.csdnimg.cn/img_convert/2ee0836e1768cb40995fd502c2cd7e4d.png)
2、开启 80 端口
sudo firewall-cmd --add-port=80/tcp --permanent
![](https://img-blog.csdnimg.cn/img_convert/20e2c202ead4ce8fa3645bfaddfdef2e.png)
3、重启防火墙,使增加的端口生效
firewall-cmd --reload
4、查看nginx的相关路径信息
whereis nginx
![](https://img-blog.csdnimg.cn/img_convert/551d9eb685b29a3c9efeed354c476d73.png)
我们要想执行Nginx命令,就必须进入 红圈 内的路径,这个路径不同的操作系统可能不一致
5、查看Nginx 的版本信息
[root@localhost sbin]# cd /usr/sbin
[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.20.1
6、修改Nginx配置,因为我的linux 80 端口被占用,因此,我需要使用其他端口
![](https://img-blog.csdnimg.cn/img_convert/4473048901c42d8e24ced10bd8fe6418.png)
Nginx 的配置文件放在了这个目录下
![](https://img-blog.csdnimg.cn/img_convert/572e1392bf679a907e9591db3baea68b.png)
修改配置文件,80 改成 8001
![](https://img-blog.csdnimg.cn/img_convert/6a58b05f41bb08a649664ff514282ee8.png)
7、防火墙开通 8001 端口 并重启防火墙
![](https://img-blog.csdnimg.cn/img_convert/9ea13fefcaba85bd4b0a33da101e968f.png)
8、重启、停止、开启Nginx 相关指令
[root@localhost nginx]# cd /usr/sbin
[root@localhost sbin]# ./nginx --启动Nginx
[root@localhost sbin]# ./nginx -s stop --关闭Ngix
[root@localhost sbin]# ./nginx
[root@localhost sbin]# ./nginx -s reload --重加载Nginx [修改配置文件后,必须执行,否则不生效]
9、访问 8001 端口 我的: http://192.168.136.135:8001/
![](https://img-blog.csdnimg.cn/img_convert/fdd731451c48ebeec6b3ae5a2e3c8287.png)
怎么变成了 welcome to centos 了?这其实是nginx的欢迎页面(每个版本的nginx各不相同),这并不代表nginx没有正常启动, 我们可以查看 /usr/share/nginx/html目录下的index.html 的内容,判断Nginx是否正确启动
![](https://img-blog.csdnimg.cn/img_convert/6636f19d8727a9bb0256745cb5ac8bdd.png)
OK,截止到这儿,Nginx安装、启动成功
设置Nginx服务自启动【前提是你的nginx 已经启动,80端口可访问】
1、在 /lib/systemd/system 目录添加 nginx.service 文件
#进入自启文件目录
cd /lib/systemd/system
#自定义nginx自启文件
touch nginx.service
2、编辑nginx.service
#编辑自启文件
vi nginx.service
3、添加一下内容, Restart=always 【如果是手动安装,直接复制粘贴,并按照下面的步骤一步步执行】
![](https://img-blog.csdnimg.cn/img_convert/8ac19a920535183c3de23ba25a4e3ff4.gif)
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
Restart=always
[Install]
WantedBy=multi-user.target
![](https://img-blog.csdnimg.cn/img_convert/5fe38d36386f2eae59f5ee4050dddbd9.gif)
![](https://img-blog.csdnimg.cn/img_convert/c9ac4eeecb26a23a6fa578e75db1c353.png)
4、设置开机自启
# 设置开机启动
systemctl enable nginx
![](https://img-blog.csdnimg.cn/img_convert/af065b9c5553ed67c32616f4357421fc.png)
5、其它命令
![](https://img-blog.csdnimg.cn/img_convert/d94fae0a7bc4235f6f9b9fca8c4bfd79.gif)
# 启动 nginx
systemctl start nginx
# 停止
systemctl stop nginx
# 加载配置文件
systemctl reload nginx
![](https://img-blog.csdnimg.cn/img_convert/653eb5e28b8e2ee9076ea9f3e12d41a6.gif)
6、关闭虚拟机
init 0
7、开机,输入 whereis nginx ,测试是否设置成功
![](https://img-blog.csdnimg.cn/img_convert/ebee619c555950ece98f22c75a4df9e0.png)
设置成功,over