linux安装nginx和前端部署vue项目

1、打包前端项目

npm run build

执行完后会在根目录下生成一个dist文件夹,这个dist文件夹就是我们后面要部署到nginx的东西。

2、将dist文件夹上传到服务器中

自己建一个目录,上传即可(尽量不要在root目录下,可能涉及权限问题)

3、安装配置nginx

3.1 在安装nginx前需要先安装安装gcc、pcre-devel、zlib-devel、openssl-devel

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

3.2 下载nginx

下载地址:https://nginx.org/download/

下载图中所选最新版本,移动到/home/kts/ktsworkplace/front/nginx(我准备存放nginx的位置)下

注:也可以先进入到上述目录,然后执行下面这条命令一键下载tar包,更方便

wget http://nginx.org/download/nginx-1.9.9.tar.gz

解压

tar -zxvf nginx-1.9.9.tar.gz

进入nginx目录

cd nginx-1.9.9

进行配置

下面三条命令依次执行,上一个执行完后再执行下一个

./configure --prefix=/home/kts/ktsworkplace/front/nginx
make
make install

3.3 修改配置文件

如果有xtcp的话直接打开当前目录/home/kts/ktsworkplace/front/nginx/conf

需要修改以下几处

需要注意的是dist文件夹尽量放在根目录下自己建的文件夹里,不要放在root里,可能会涉及权限问题,导致前端报错403


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
        }

        # 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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


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

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

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

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

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

修改listen       后面的监听位置(此处我修改为8080),具体根据前端代码的实际情况去选取监听端口位置,至于为什么要进行修改部分原因如下:

在Linux系统中,端口号小于1024的端口(包括80端口)是特权端口,只有root用户或具有相应权限的进程才能绑定。

4、启动nginx

cd /home/kts/ktsworkplace/front/nginx/sbin
./nginx //启动nginx

启动成功在浏览器中输入你前端的地址。

如果成功则显示图中Welcome to nginx!

5、当之后每次修改配置文件后,nginx都要进行重启

# 未配置环境变量使用绝对路径运行
/home/kts/ktsworkplace/front/nginx/sbin/nginx -s reload

### 如何配置Nginx部署Vue3单页应用(SPA)前端项目 #### 安装启动Nginx 为了确保Nginx能够正常运行,在Linux环境下可以通过命令`sudo systemctl status nginx`来检查其状态[^1]。 #### 构建Vue3项目 构建Vue3项目以便生成用于生产的静态资源,这一步通过执行`npm run build`命令完成。此操作会创建一个名为`dist/`的目录,其中包含了所有必要的静态文件[^2]。 ```bash npm run build ``` #### 修改Nginx配置文件 接下来需要编辑或新建Nginx站点配置文件,通常位于`/etc/nginx/sites-available/default`或其他自定义位置。对于Vue SPA来说,重要的是设置正确的根路径指向之前提到的`dist/`目录,并处理HTML5模式下的历史记录API请求重定向至index.html: ```nginx server { listen 80; server_name yourdomain.com; root /path/to/dist; # 替换成实际vue项目的dist目录的位置 location / { try_files $uri $uri/ /index.html; } error_page 404 /404.html; } ``` 这段配置使得任何未匹配到具体文件或目录的URL都将被导向至`index.html`,从而允许Vue Router接管路由逻辑[^3]。 #### 测试与重启Nginx服务 保存更改后的配置文件之后,先利用`nginx -t`指令验证新配置是否有误;如果一切顺利,则可通过`nginx -s reload`使新的配置生效而不中断现有连接。 ```bash sudo nginx -t sudo nginx -s reload ``` #### 访问应用程序 最后,在浏览器里输入相应的域名或者服务器公网IP地址即可看到已经上线的Vue3应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值