项目使用ng build 打包后,复制dist.zip到服务器上。
安装 Nginx
sudo apt install nginx
使用以下命令启动服务
sudo systemctl status nginx
在/etc/nginx/conf.d目录下新增配置文件例如:
angular-xx.conf
server {
listen 4200;
server_name localhost;
#access_log /var/log/nginx/angular-myapp.log main;
#默认请求
location / {
root /home/xxxx/dist/myapp; #定义服务器的默认网站根目录位置
index index.html index.htm; #定义首页索引文件的名称
try_files $uri $uri/ /index.html;
#fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
#include /etc/nginx/fastcgi_params;
}
# 配置404页面为angular编译生成的index.html
error_page 404 /index.html;
location = /index.html {
root /home/xxxx/dist/myapp;
}
}
重新加载nginx.config文件,在/usr/sbin重启服务
sudo nginx -c /etc/nginx/nginx.conf
sudo ./nginx -s reload
在网址访问:服务器ip:4200/即可访问项目。
每次新加项目,需要重新加载conf文件、重新启动nginx服务。
sudo nginx -s quit
sudo nginx -c /etc/nginx/nginx.conf
sudo ./nginx -s reload
如果重启出问题使用一下:
sudo killall nginx
sudo nginx