CentOS 安装 Nginx 包教程

Nginx 是一款高性能的 HTTP 和反向代理服务器,以及一个 IMAP/POP3/SMTP 代理服务器,本文将详细介绍如何在 CentOS 系统上安装 Nginx 包。
安装前的准备
确保您的 CentOS 系统已更新到最新版本,可以使用以下命令进行更新:
sudo yum update
安装编译工具和依赖库:
sudo yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
安装 Nginx
使用 yum 安装 Nginx:
sudo yum install -y nginx
检查 Nginx 是否安装成功:
nginx -v
如果看到类似以下信息,则表示 Nginx 安装成功:

nginx version: nginx/1.18.0 启动 Nginx 服务:
sudo systemctl start nginx
设置 Nginx 服务开机自启:
sudo systemctl enable nginx
测试 Nginx 是否运行正常:
在浏览器中输入您的服务器 IP 地址,如果看到如下页面,则表示 Nginx 运行正常:
Welcome to the Nginx 1.18.0 server. 配置 Nginx
编辑 Nginx 配置文件:
sudo vi /etc/nginx/nginx.conf
根据需要修改配置文件,以下是一个简单的配置示例:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
} 保存并退出配置文件。

重启 Nginx 服务以应用配置:
sudo systemctl restart nginx
相关问答 FAQs
Q1:如何查看 Nginx 的进程?
A1:使用以下命令查看 Nginx 的进程:
sudo ps -ef | grep nginx
Q2:如何停止 Nginx 服务?
A2:使用以下命令停止 Nginx 服务:
sudo systemctl stop nginx
通过以上步骤,您可以在 CentOS 系统上成功安装并配置 Nginx,祝您使用愉快!

