【入门】CentOS 7 系统上安装 Nginx

【入门】CentOS 7 系统上安装 Nginx

经验文章nimo972025-06-29 8:38:122A+A-

在 CentOS 7 系统上安装 Nginx,可按以下步骤操作:

1. 添加 Nginx 官方仓库

首先要创建 Nginx 仓库配置文件。

sudo vi /etc/yum.repos.d/nginx.repo

接着在编辑器里添加下面的内容:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

完成添加后保存并关闭文件。

2. 安装 Nginx

执行以下命令来安装 Nginx:

sudo yum install nginx -y

输出内容:

[root@localhost down]# sudo yum install nginx -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                        | 4.8 kB  00:00:00
 * epel: ftp-stud.hs-esslingen.de
base                                                                        | 3.6 kB  00:00:00
extras                                                                      | 2.9 kB  00:00:00
nginx                                                                       | 2.9 kB  00:00:00
updates                                                                     | 2.9 kB  00:00:00
nginx/x86_64/primary_db                                                     |  97 kB  00:00:01
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.26.1-2.el7.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================
 Package            Arch                Version                           Repository          Size
===================================================================================================
Installing:
 nginx              x86_64              1:1.26.1-2.el7.ngx                nginx              807 k

Transaction Summary
===================================================================================================
Install  1 Package

Total download size: 807 k
Installed size: 2.8 M
Downloading packages:
nginx-1.26.1-2.el7.ngx.x86_64.rpm                                           | 807 kB  00:00:36
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:nginx-1.26.1-2.el7.ngx.x86_64                                                 1/1
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* https://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* https://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : 1:nginx-1.26.1-2.el7.ngx.x86_64                                                 1/1

Installed:
  nginx.x86_64 1:1.26.1-2.el7.ngx

Complete!
[root@localhost down]#


3. 启动并设置 Nginx 开机自启

安装完成后,启动 Nginx 服务:

sudo systemctl start nginx

若要让 Nginx 在系统启动时自动运行,执行:

sudo systemctl enable nginx

4. 配置防火墙

若系统开启了防火墙,需要开放 HTTP 和 HTTPS 端口:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload



5. 验证 Nginx 是否安装成功

在浏览器中访问服务器的公网 IP 地址。如果看到 "Welcome to nginx!" 的页面,就表明 Nginx 安装成功了。

我CentOs的IP是:192.168.203.133

访问:

http://192.168.203.133/


其他常用命令

重启 Nginx:

sudo systemctl restart nginx

重新加载配置:

sudo systemctl reload nginx

停止 Nginx:

sudo systemctl stop nginx
点击这里复制本文地址 以上内容由nimo97整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

尼墨宝库 © All Rights Reserved.  蜀ICP备2024111239号-7