How can I deploy my Angular 2 + Typescript + Webpa

2019-01-14 15:38发布

I am actually learning Angular 2 with Typescript and developed a little app by based on the angular-seed project (angular-seed). I have built the app for production purposes and got dist folder ready to be deployed containing my bundle files like this:

dist/                    
  main.bundle.js              
  main.map         
  polyfills.bundle.js          
  polyfills.map            
  vendor.bundle.js         
  vendor.map

However, as a fresher, I have no idea how to deploy it now on my EC2 server. I read that I have to config Nginx server to serve my static file but do I have to config it particularly to work with my bundle files?

Excuse my mistakes if any. Thanks a lot in advance!

4条回答
Emotional °昔
2楼-- · 2019-01-14 16:15

You are on the right track.....

Just install the nginx on your EC2. In my case I had a linux Ubuntu 14.04 installed on "Digital Ocean".

First I updated the apt-get package lists:

sudo apt-get update

Then install Nginx using apt-get:

sudo apt-get install nginx

Then open the default server block configuration file for editing:

sudo vi /etc/nginx/sites-available/default

Delete everything in this configuration file and paste the following content:

server {
    listen 80 default_server;
    root /path/dist-nginx;
    index index.html index.htm;
    server_name localhost;
    location / {
        try_files $uri $uri/ =404;
    }
}   

To make the changes active, restart the webserver nginx:

sudo service nginx restart

Then copy index.html and the bundle files to /path/dist-nginx on your server and you are up and running.

查看更多
做个烂人
3楼-- · 2019-01-14 16:31

If anyone still struggling with production setup of angular 2/4/5 app + Nginx, then here is the complete solution:

Suppose you want to deploy your angular app at HOST: http://example.com and PORT: 8080 Note - HOST and PORT might be different in your case.

Make sure you have <base href="/"> in you index.html head tag.

  1. Firstly, go to your angular repo (i.e. /home/user/helloWorld) path at your machine.

  2. Then build /dist for your production server using the following command:

    ng build --prod --base-href http://example.com:8080

  3. Now copy /dist (i.e. /home/user/helloWorld/dist) folder from your machine's angular repo to the remote machine where you want to host your production server.

  4. Now login to your remote server and add following nginx server configuration.

    server {
    
        listen 8080;
    
        server_name http://example.com;
    
        root /path/to/your/dist/location;
    
        # eg. root /home/admin/helloWorld/dist
    
        index index.html index.htm;
    
        location / {
    
            try_files $uri $uri/ /index.html;
    
            # This will allow you to refresh page in your angular app. Which will not give error 404.
    
        }
    
    }
    
  5. Now restart nginx.

  6. That's it !! Now you can access your angular app at http://example.com:8080

Hope it will be helpful.

查看更多
姐就是有狂的资本
4楼-- · 2019-01-14 16:32

A quicker way to deploy is as below:

1. Install nginx as mentioned by Herman.
2. Copy your dist/* files to /var/www/html/ without disturbing /etc/nginx/sites-available/default.
sudo cp /your/path/to/dist/* /var/www/html/
3. Restart nginx:
sudo systemctl restart nginx

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-14 16:32

I'm using the official angular CLI instead to deploy to production and is very easy to do. You can deploy to pre-production ie, or production this way:

ng build --env=pre --output-path=build/pre/

ng build --env=prod --output-path=build/prod/

查看更多
登录 后发表回答