Linux下部署Nginx

概述

本文介绍在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)。连接Linux
  • 添加EPEL存储库(sudo yum install epel-release -y
  • 下载nginx软件包 (sudo yum install -y nginx)
    下载nginx软件包
  • 设置自动启动Nginx (sudo systemctl enable nginx)
    启动Nginx
  • 启动Nginx (sudo systemctl start nginx)
  • 查看Nginx运行进程(ps -ef | grep nginx
    启动Nginx
  • 查看Nginx启动状态 (sudo systemctl status nginx)
    查看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;
        }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值