​

1.服务器安装操作系统

云服务器 去配置操作系统 我选的是 8.2 后边不支持了 好像有问题

2.使用finalshell 连接服务器

3.安装Nginx所需环境

3.1 gcc 安装

  1. 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装

    yum -y install gcc-c++

    centos8已经不支持 需要手动给转镜像 安装依赖时提示在“/etc/yum.repos.d“, “/etc/yum/repos.d“, “/etc/distro.repos.d“中没有被启用的仓库

    • 解决 方法先创建一个/etc/yum.repos.d目录

    mkdir /etc/yum.repos.d

    • ​使用curl命令从阿里云镜像站点下载CentOS8的仓库配置文件并保到/etc/yum.repos.d/CentOS-Base.repo

    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

    • 重新建立缓存

    yum makecache

    • 再次安装依赖,不再提示在"/etc/yum.repos.d", “/etc/yum/repos.d”, "/etc/distro.repos.d"中没有被启用的仓库了

3.2 PCRE pcre-devel 安装

  1. PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl
    兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre
    库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

3.3 zlib 安装

  1. zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在
    Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

3.4 OpenSSL 安装

  1. OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL
    协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持
    https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

3.5 切换到 /usr/local 目录下,下载源代码解压

cd /usr/local
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

3.6 编译nginx,带:ssl/gzip/realip/http2等功能

./configure --prefix=/usr/local/nginx --with-threads --with-http_realip_modul
make 编译
make install 编译后安装
参数说明:
–with-threads:启用thread pool支持。
–with-http_realip_module:允许从请求报文头中更改客户端的ip地址,默认为关
–with-http_stub_status_module:启用ngx_http_stub_status_module 支持查看nginx的状态页。
–with-http_ssl_module:启用ngx_http_gzip_static_module支持,支持在线实时压缩输出数据流
–with-http_ssl_module:启用https支持。
–with-http_v2_module:启用http2

3.7 查看nginx的版本信息

/usr/local/nginx/sbin/nginx -V
whereis nginx 查看nginx装到哪儿去了
cd /usr/local/nginx

3.8 启动Nginx

cd sbin
执行nginx命令, 就是启动nginx这个软件

./nginx

测试nginx软件是否启动成功, 测试之前先关闭服务器防火墙

systemctl stop firewalld

3.8 配置开机自启动

即在rc.local增加启动代码就可以了。
进入编辑状态 指令:
vi /etc/rc.local
需自己手动输入保存 增加一行 /usr/local/nginx/sbin/nginx
设置执行权限指令:
chmod 755 /etc/rc.local

重启试一下 指令:
reboot

3.9 配置前端项目

在vue项目 npm run build 我们获得了一个dist文件夹

把这个拖到nginx的 /usr/local/nginx/html

cd /usr/local/nginx/html

修改配置文件

cd /usr/local/nginx/conf

找到nginx.conf

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            root   /usr/local/nginx/html/dist/;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /gpp/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            #rewrite ^/api/(.*)$ /$1 break;  #重写
            proxy_pass http://127.0.0.1:9992/; # 设置后端服务器的协议和地址
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

cd /usr/local/nginx/sbin/ 进入到nginx安装目录下的sbi目录

./nginx -s reload 重启nginx
别忘了关闭防火墙

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐