nginx在centos怎样设置
导读:1. 安装Nginx 在CentOS上安装Nginx有两种常见方式:YUM源安装(快速便捷,推荐新手)和源码编译安装(灵活自定义,适合有特定需求的情况)。 YUM源安装步骤: 更新系统并安装EPEL仓库(EPEL提供Nginx等额外软件包...
1. 安装Nginx
在CentOS上安装Nginx有两种常见方式:YUM源安装(快速便捷,推荐新手)和源码编译安装(灵活自定义,适合有特定需求的情况)。
YUM源安装步骤:
- 更新系统并安装EPEL仓库(EPEL提供Nginx等额外软件包):
sudo yum update -y sudo yum install epel-release -y
- 安装Nginx:
sudo yum install nginx -y
- 启动Nginx并设置开机自启:
sudo systemctl start nginx sudo systemctl enable nginx
源码编译安装步骤(可选):
- 安装编译工具和依赖:
sudo yum install -y gcc make pcre-devel openssl-devel zlib-devel
- 下载并解压Nginx源码(以1.25.3为例):
wget http://nginx.org/download/nginx-1.25.3.tar.gz tar -zxvf nginx-1.25.3.tar.gz cd nginx-1.25.3
- 配置编译选项(指定安装路径、启用SSL等模块):
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre
- 编译并安装:
make & & sudo make install
- 创建systemd服务文件(方便管理):
内容如下:sudo nano /etc/systemd/system/nginx.service
[Unit] Description=The NGINX HTTP and reverse proxy server After=network.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=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target
- 使服务生效并启动:
sudo systemctl daemon-reload sudo systemctl start nginx sudo systemctl enable nginx
2. 基础配置
Nginx的主配置文件位于/etc/nginx/nginx.conf
(YUM安装)或/usr/local/nginx/conf/nginx.conf
(源码安装),虚拟主机配置通常放在/etc/nginx/conf.d/
(YUM)或/usr/local/nginx/conf/conf.d/
(源码)目录下。
核心配置说明:
- 全局块(文件顶部):设置Nginx全局参数,如工作进程数、错误日志路径:
user nginx; # 运行用户(默认nginx) worker_processes auto; # 工作进程数(auto自动匹配CPU核心数) error_log /var/log/nginx/error.log warn; # 错误日志路径和级别 pid /var/run/nginx.pid; # 主进程PID文件路径
- events块:配置连接处理方式,优化高并发性能:
events { worker_connections 1024; # 每个工作进程最大并发连接数 use epoll; # 事件模型(Linux下推荐epoll) multi_accept on; # 允许一个进程同时接受多个连接 }
- http块:配置HTTP服务器参数,包含虚拟主机和MIME类型:
http { include /etc/nginx/mime.types; # 引入MIME类型文件 default_type application/octet-stream; # 默认MIME类型 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; # 长连接超时时间(秒) gzip on; # 启用GZIP压缩 include /etc/nginx/conf.d/*.conf; # 包含虚拟主机配置 }
- server块(虚拟主机):定义单个网站的配置,如监听端口、域名、根目录:
server { listen 80; # 监听80端口(HTTP) server_name example.com www.example.com; # 域名(可多个,空格分隔) root /var/www/html/example.com; # 网站根目录 index index.html index.htm; # 默认首页文件 location / { try_files $uri $uri/ =404; # 尝试查找文件,不存在则返回404 } error_page 404 /404.html; # 自定义404错误页面 location = /404.html { root /var/www/html; # 错误页面路径 } }
3. 虚拟主机配置
Nginx支持基于域名的虚拟主机(最常用),通过不同server_name
区分多个网站。
步骤:
- 创建网站目录和首页文件:
sudo mkdir -p /var/www/html/example.com sudo echo "Welcome to Example.com" > /var/www/html/example.com/index.html
- 创建虚拟主机配置文件(如
example.com.conf
):
粘贴上述sudo nano /etc/nginx/conf.d/example.com.conf
server
块配置,修改server_name
和root
为你的域名和目录。 - 测试配置语法并重载Nginx:
sudo nginx -t # 检查配置是否有语法错误 sudo systemctl reload nginx # 重载配置(不中断服务)
4. 防火墙设置
如果服务器启用了防火墙(如firewalld
),需要开放HTTP(80)和HTTPS(443)端口:
sudo firewall-cmd --permanent --add-service=http # 永久开放HTTP
sudo firewall-cmd --permanent --add-service=https # 永久开放HTTPS
sudo firewall-cmd --reload # 重载防火墙规则
若需临时关闭防火墙(仅测试用,生产环境不推荐):
sudo systemctl stop firewalld
5. SSL配置(可选,启用HTTPS)
使用Let’s Encrypt免费获取SSL证书,步骤如下:
- 安装Certbot和Nginx插件:
sudo yum install -y certbot python3-certbot-nginx
- 获取并安装证书(替换
example.com
为你的域名):sudo certbot --nginx -d example.com -d www.example.com
- 按提示完成配置(如选择是否自动重定向HTTP到HTTPS)。
- 证书自动续期(Let’s Encrypt有效期90天):
sudo certbot renew --dry-run # 测试续期
6. 常用命令
- 启动Nginx:
sudo systemctl start nginx
- 停止Nginx:
sudo systemctl stop nginx
- 重载配置:
sudo systemctl reload nginx
(不中断服务) - 查看状态:
sudo systemctl status nginx
- 测试配置语法:
sudo nginx -t
- 查看错误日志:
sudo tail -f /var/log/nginx/error.log
通过以上步骤,你可以在CentOS上完成Nginx的安装、基础配置及虚拟主机设置,满足常见的Web服务需求。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: nginx在centos怎样设置
本文地址: https://pptw.com/jishu/727193.html