概述
本文介绍在Linux下部署Nginx的详细步骤,以供参考。
环境说明
OS Version:CentOS Linux release 8.1.1911 (此为发行版本)
OS Version:CentOS Linux release 8.2.2004 (此为发行版本)
Linux下查看系统版本命令
查看系统发行版本:cat /etc/redhat-release
查看系统内核版本:uname -a
准备内容
Linux文件命令行工具Xshell 4
下载地址
链接:https://pan.baidu.com/s/1cqrgxkJqLK_EOEccMnPxDA
(https://pan.baidu.com/s/1cqrgxkJqLK_EOEccMnPxDA )
提取码:xxyu
部署步骤(部分步骤可忽略)
- 通过Xshell工具使用root用户登录到Linux系统(这里使用的是阿里云ECS)。
- 添加EPEL存储库(sudo yum install epel-release -y)
- 下载nginx软件包 (sudo yum install -y nginx)
- 设置自动启动Nginx (sudo systemctl enable nginx)
- 启动Nginx (sudo systemctl start nginx)
- 查看Nginx运行进程(ps -ef | grep nginx)
- 查看Nginx启动状态 (sudo systemctl status nginx)
常用位置
- 静态页面:/usr/share/nginx/html
- 配置文件:/etc/nginx/nginx.conf
常用命令
- 校验Nginx配置文件:/usr/sbin/nginx -t
- 查看Nginx:sudo systemctl status nginx
- 停止Nginx:sudo systemctl stop nginx
- 启动Nginx:sudo systemctl start nginx
- 重启Nginx:sudo systemctl reload nginx
配置文件示例
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.haloc.com;
location / {
root /usr/share/nginx/html/haloc;
index index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
}
server {
listen 80;
listen 443;
server_name www.haloc.com;
ssl on;
root /usr/share/nginx/html;
index index.html index.htm;
ssl_certificate cert/409932398_www.haloc.com.pem;
ssl_certificate_key cert/409932398_www.haloc.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:8080/;
}
}
# HTTP 强转 HTTPS
server {
listen 80;
server_name www.haloc.com;
return 301 https://$host$request_uri;
}
}