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