在CentOS Minimal上安装Nginx的步骤如下:
首先,确保你的系统是最新的:
sudo yum update -y
Nginx不在CentOS的默认仓库中,但可以通过EPEL(Extra Packages for Enterprise Linux)仓库安装:
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
打开浏览器,访问服务器的IP地址或域名,你应该能看到Nginx的默认欢迎页面。
如果你需要自定义Nginx配置,可以编辑/etc/nginx/nginx.conf
文件或创建一个新的配置文件并将其链接到/etc/nginx/conf.d/
目录。
例如,创建一个新的配置文件:
sudo nano /etc/nginx/conf.d/default.conf
添加以下内容:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
保存并退出编辑器,然后重新加载Nginx配置:
sudo systemctl reload nginx
现在,你应该能够通过浏览器访问你的自定义页面了。
如果你需要停止或重启Nginx服务,可以使用以下命令:
sudo systemctl stop nginx
sudo systemctl restart nginx
通过以上步骤,你应该能够在CentOS Minimal上成功安装和配置Nginx。