centos安装nginx,部署vue项目

centos安装nginx,部署vue项目

1、安装nginx依赖包

 # 首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库)
 $  yum install gcc-c++ -y
 $  yum install pcre pcre-devel -y
 $  yum install zlib zlib-devel -y
 $  yum install openssl openssl--devel -y

2、下载安装包和解压

# 安装之前,最好检查一下是否已经安装有nginx
$   find -name nginx
# 如果系统已经安装了nginx,那么就先卸载
$   yum remove nginx
# 首先进入/usr/local目录
$   cd /usr/local
# 从官网下载最新版的nginx
$   wget http://nginx.org/download/nginx-1.17.7.tar.gz
# 解压nginx压缩包
$   tar -zxvf nginx-1.17.7.tar.gz

3、开始安装

# 会产生一个nginx-1.17.7 目录,这时进入nginx-1.17.7目录
$   cd  nginx-1.17.7
# 接下来安装,使用--prefix参数指定nginx安装的目录,make、make install安装
# --prefix 指定安装目录,默认为 /usr/local/nginx
# --with-http_ssl_module 启用ssl模块
$   ./configure --with-http_ssl_module
$   make
$   make install
# 如果没有报错,顺利完成后,最好看一下nginx的安装目录
$   whereis nginx
# 安装完毕后,进入安装后目录(/usr/local/nginx)便可以启动或停止它了。
# 到此,使用CentOS安装nginx已经完成了,其实看看还是蛮简单的。

4、启动

$ /usr/local/nginx/sbin/nginx
# 检查是否启动成功:
# 打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
# 如果运行的时候不带-c参数,那就采用默认的配置文件,即/usr/local/nginx/conf/nginx.conf
# 查看运行进程状态:
$ ps aux | grep nginx

5、其他命令

# 部分命令如下:
# 重启:
$ /usr/local/nginx/sbin/nginx –s reload
# 停止:
$ /usr/local/nginx/sbin/nginx –s stop
# 测试配置文件是否正常:
$ /usr/local/nginx/sbin/nginx –t
# 强制关闭:
$ pkill nginx

6、配置文件nginx.conf


#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    # 压缩
    gzip  on;

    server {
        listen       80;
        server_name  localhost; 
        location / {
            root   html;
            index  index.html index.htm;
            # 强制跳转到https
            rewrite ^(.*)$  https://$host$1 permanent;
        }

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    # HTTPS server
    server {
      listen       443 ssl;
      server_name  localhost;

	  # [ssl证书]
      #ssl_certificate      /etc/letsencrypt/live/sdmp.makern.cn/fullchain.pem;
      #ssl_certificate_key  /etc/letsencrypt/live/sdmp.makern.cn/privkey.pem;

      ssl_session_cache    shared:SSL:1m;
      ssl_session_timeout  5m;

      ssl_ciphers  HIGH:!aNULL:!MD5;
      ssl_prefer_server_ciphers  on;

      location / {
        root /opt/vue/dist2;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
      }

      location /sdmp {
        proxy_pass         https://localhost:8080/sdmp;
        proxy_http_version 1.1;# http协议版本
        proxy_set_header   Host $host;
        proxy_set_header   Upgrade $http_upgrade;# websocket配置
        proxy_set_header   Connection "Upgrade";# websocket配置
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;# 客户端的ip
        proxy_set_header   X-Forwarded-Proto $scheme;# 判断客户端使用的是http还是https
        proxy_read_timeout 600s;# 超时设置,表明连接成功以后等待服务器响应的时候,如果不配置默认为90s
      }

	  # root和alias区别:https://blog.csdn.net/tuoni123/article/details/79712246
	  # 真实路径=alias别名指定的路径; 真实路径=root的路径加上location路径
	  #
      #location /s{
      #  alias /opt/vue/dist;
      #  index  index.html;
      #  try_files $uri $uri/ /s/index.html;
      #}
    }

}

ps: 测试websocket , ws://127.0.0.1:8080/sdmp/webSocket/device/12

6、配置开机启动

# 创建nginx.service 脚本
$ vi /lib/systemd/system/nginx.service
# nginx.service内容如下:
[Unit]
Description=nginx
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
# 设置开机启动
$ systemctl enable nginx.service
# 取消开机启动
$ systemctl disable nginx.service
# 查看状态
$ systemctl status nginx.service
# 启动服务
$ systemctl start nginx.service
# 关闭服务
$ systemctl stop nginx.service
# 重启服务
$ systemctl restart nginx.service

7、nginx-readme

# =============nginx readme========================
# 安装位置      /usr/local/nginx
# 配置文件路径    /usr/local/nginx/conf/nginx.conf
# 启动            /usr/local/nginx/sbin/nginx
# 重启            /usr/local/nginx/sbin/nginx -s reload
# 停止            /usr/local/nginx/sbin/nginx -s stop
# 测试配置文件    /usr/local/nginx/sbin/nginx –t
# 强制关闭      pkill nginx
​
# 检查是否启动成功
# 打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
# 如果运行的时候不带-c参数,那就采用默认的配置文件,即/usr/local/nginx/conf/nginx.conf
# 查看运行进程状态:ps aux | grep nginx

# shell脚本
# 设置开机启动:systemctl enable nginx.service
# 取消开机启动:systemctl disable nginx.service
# 查看状态:systemctl status nginx.service
# 启动:systemctl start nginx.service
# 关闭:systemctl stop nginx.service
# 重启:systemctl restart nginx.service
发布了53 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/blog_zxb/article/details/103735608