下面以 CentOS Stream 8 为例,介绍几种常见的 Nginx 部署方式,从简单到灵活,你可以按需选择。
CentOS Stream 8 默认使用 dnf 包管理器,官方仓库已包含 Nginx。
sudo dnf update -y
sudo dnf install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
systemctl status nginx
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问服务器 IP:
http://<服务器IP>
看到 Nginx 欢迎页 即成功。
/etc/nginx/nginx.conf # 主配置文件
/etc/nginx/conf.d/ # 子配置目录
/usr/share/nginx/html/ # 默认网站根目录
/var/log/nginx/ # 日志目录
示例:新建一个站点
sudo vim /etc/nginx/conf.d/example.conf
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html/example;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
测试并重载:
sudo nginx -t
sudo systemctl reload nginx
sudo dnf groupinstall "Development Tools" -y
sudo dnf install pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
cd /usr/local/src
sudo wget http://nginx.org/download/nginx-1.24.0.tar.gz
sudo tar -xzf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure --prefix=/usr/local/nginx
make
sudo make install
/usr/local/nginx/sbin/nginx
sudo netstat -tunlp | grep :80
sudo setsebool -P httpd_can_network_connect 1
sudo systemctl enable nginx
如果你愿意,我可以:
告诉我你的使用场景即可。