最近使用Angular
做了第一個應用,但是網上的教程大多是教如何開發,部署相對較少,所以這裏就簡單記錄一下如何在nginx
中部署Angular
應用。
注:
Angular
應用可以編譯成靜態頁面,然後部署在任何 web 服務器上,這裏僅僅是選擇nginx
而已,同時編譯後的文件其實就僅僅是靜態文件而已,與其它 html 文件本質上無異。
一、編譯
前提: 請確保
@angular/cli
已經安裝
在項目主目錄下輸入以下命令:
ng build
成功則輸入類似於下面的信息:
Date: 2017-10-14T08:19:18.595Z
Hash: aa580b91f10a49a65d87
Time: 28823ms
chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB [entry] [rendered]
chunk {main} main.bundle.js, main.bundle.js.map (main) 55.9 kB {vendor} [initial] [rendered]
chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 217 kB {inline} [initial] [rendered]
chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 163 kB {inline} [initial] [rendered]
chunk {vendor} vendor.bundle.js, vendor.bundle.js.map (vendor) 5.74 MB [initial] [rendered]
並生成了新的目錄dist
及其下的子文件/目錄,此時則成功將應用編譯成靜態資源。
二、部署
原則上,您可以將這些資源拷貝到任何項目中,比如
JavaWeb
、NodeJs
等項目中,它們同樣的可以運行。前提: 服務器已經安裝
nginx
,並假設nginx
安裝目錄爲/usr/local/nginx
nginx 的部分相關命令:
- nginx : 啓動服務
- nginx -s stop : 先查出 nginx 進程 id,然後使用 kill 命令強制殺掉進程
- nginx -s quit : 等待 nginx 進程處理任務完畢,然後再進行停止
- nginx -s reload : 重啓服務
- ps aux|grep nginx : 查看 nginx 進程
1) 準備源文件
拷貝項目編譯後的dist
目錄下的所有文件到服務器上,比如拷貝至/usr/local/web/home
2) 配置nginx
sudo vi /usr/local/nginx/conf/nginx.conf
修改http
->server
節點下 localhost
和error_page 404
的值如下:
location / {
# root html;
# index index.html index.htm;
root /usr/local/web/home;
index index.html index.html;
}
#error_page 404 /404.html;
error_page 404 /;
這裏同時修改了404
錯誤的跳轉路徑,是爲了防止直接訪問路由地址時出現404錯誤,文件全信息如下(端口被修改):
#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 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# index index.html index.htm;
root /usr/local/web/home;
index index.html index.html;
}
#error_page 404 /404.html;
error_page 404 /;
# 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;
# }
#}
}
重啓nginx
sudo /usr/local/nginx/sbin/nginx -s reload
瀏覽器訪問即可。