在CentOS Stream 8上安装Nginx的步骤如下:
方法一:使用yum安装
-
更新系统包
sudo dnf update -y
-
安装EPEL仓库(如果尚未安装) EPEL(Extra Packages for Enterprise Linux)提供了许多额外的软件包。
sudo dnf install epel-release -y
-
安装Nginx
sudo dnf install nginx -y
-
启动Nginx服务
sudo systemctl start nginx
-
设置Nginx开机自启
sudo systemctl enable nginx
-
检查Nginx状态
sudo systemctl status nginx
-
访问Nginx默认页面 打开浏览器,访问服务器的IP地址或域名,应该能看到Nginx的默认欢迎页面。
方法二:使用源码编译安装
如果你需要最新版本的Nginx或者有特定的配置需求,可以选择从源码编译安装。
-
安装必要的依赖包
sudo dnf groupinstall "Development Tools" -y sudo dnf install pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
-
下载Nginx源码 访问Nginx官方网站,找到最新版本的下载链接,然后使用
wget
下载。wget http://nginx.org/download/nginx-
.tar.gz tar -zxvf nginx- .tar.gz cd nginx- -
配置编译选项
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v3_module
-
编译并安装
make sudo make install
-
启动Nginx服务
sudo /usr/local/nginx/sbin/nginx
-
设置Nginx开机自启 创建一个systemd服务文件:
sudo vi /etc/systemd/system/nginx.service
添加以下内容:
[Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
启用并启动服务:
sudo systemctl daemon-reload sudo systemctl enable nginx sudo systemctl start nginx
-
检查Nginx状态
sudo systemctl status nginx
-
访问Nginx默认页面 同样,打开浏览器访问服务器的IP地址或域名。
注意事项
- 在生产环境中,建议使用防火墙(如firewalld)来限制对Nginx端口的访问。
- 定期更新Nginx和相关依赖包以确保安全性和稳定性。
希望这些步骤能帮助你在CentOS Stream 8上成功安装Nginx!