在CentOS系统上安装Nginx可以通过以下步骤完成,这里提供两种常用方法:YUM仓库安装和源码编译安装。
特点:简单快捷,自动处理依赖关系,适合生产环境。
sudo yum update -y
Nginx在默认的CentOS仓库中可能不存在,需要先启用EPEL:
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
bash
sudo systemctl status nginx
http://your_server_ip
),看到Nginx欢迎页即表示成功。允许HTTP(80)和HTTPS(443)流量:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
特点:可自定义模块和版本,适合需要特定配置的场景。
sudo yum groupinstall "Development Tools" -y
sudo yum install pcre-devel zlib-devel openssl-devel -y
访问Nginx官网获取最新稳定版链接,例如:
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make
sudo make install
/usr/local/nginx/sbin/nginx
curl http://localhost
或浏览器访问服务器IP。
创建Systemd服务文件 /etc/systemd/system/nginx.service
:
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
然后启用服务:
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl stop httpd
)。setenforce 0
或调整策略。nginx
或 www-data
)对相关目录有访问权限。安装完成后,配置文件路径:
- YUM安装:/etc/nginx/nginx.conf
- 源码安装:/usr/local/nginx/conf/nginx.conf