阅读量:73
在CentOS系统上安装Nginx可以通过以下步骤完成。以下指南适用于CentOS 7和CentOS 8。
CentOS 7
-
更新系统包
sudo yum update -y -
安装EPEL仓库 EPEL(Extra Packages for Enterprise Linux)仓库提供了许多额外的软件包,包括Nginx。
sudo yum install epel-release -y -
安装Nginx
sudo yum install nginx -y -
启动Nginx服务
sudo systemctl start nginx -
设置Nginx开机自启动
sudo systemctl enable nginx -
检查Nginx状态
sudo systemctl status nginx -
配置防火墙 如果你使用的是firewalld,需要允许HTTP和HTTPS流量。
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
CentOS 8
-
更新系统包
sudo dnf update -y -
安装Nginx
sudo dnf install nginx -y -
启动Nginx服务
sudo systemctl start nginx -
设置Nginx开机自启动
sudo systemctl enable nginx -
检查Nginx状态
sudo systemctl status nginx -
配置防火墙 如果你使用的是firewalld,需要允许HTTP和HTTPS流量。
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
验证安装
打开浏览器,访问服务器的IP地址或域名,你应该能看到Nginx的默认欢迎页面。
配置文件
Nginx的主要配置文件位于 /etc/nginx/nginx.conf。你可以根据需要编辑这个文件或创建新的配置文件在 /etc/nginx/conf.d/ 目录下。
例如,创建一个新的配置文件 /etc/nginx/conf.d/default.conf:
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
然后重新加载Nginx配置:
sudo systemctl reload nginx
这样,你就成功地在CentOS上安装并配置了Nginx。