How can I deploy my Angular 2 + Typescript + Webpack app - nginx

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!

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.

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.
Firstly, go to your angular repo (i.e. /home/user/helloWorld) path at your machine.
Then build /dist for your production server using the following command:
ng build --prod --base-href http://example.com:8080
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.
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.
}
}
Now restart nginx.
That's it !! Now you can access your angular app at http://example.com:8080
Hope it will be helpful.

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

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/

Related

Basic clean wordpress install on Nginx returns 502 error

I’m learning about running a server on a raspberry pi and just want to run a simple default wordpress site served with Nginx. For some reason loading the site locally in a browser returns a 502 error despite my other basic non-wordpress sites loading correctly. A clean download of the default wordpress installation files are inside /var/www/wp.example.co.uk
I’ve made a wp.example.co.uk.conf file inside /etc/nginx/sites-available - also symlinked to /etc/nginx/sites-enabled - with the code:
upstream wp-php-handler {
server unix:/var/run/php/php7.3-fpm.sock;
}
server {
listen 5432;
server_name _;
root /var/www/wp.example.co.uk;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass wp-php-handler;
}
}
Whenever I view it in a browser (http://mylocalip:4323) it returns a 502 error. Why does this happen?
Note: I’m following a YouTube tutorial (where the relevant part is ~6:43 of https://www.youtube.com/watch?v=8psimaAr1U8) that shows the same code working, which leads me to believe that my code should work as-is.
Thanks
It looks like the tutorial might be outdated after only 6 months. It tells you to install php-fpm, and then it just assumes that version 7.3 is going to be installed. If you run the command apt show php-fpm | grep "Depends:" it'll tell you which version is actually being installed. Now while you could just run apt install php7.3-fpm to follow along with the tutorial, I've included some instructions below on how to use a more recent version of PHP.
Install the version you want e.g. apt install php8.1-fpm or just apt install php-fpm for the current default version.
Run ls -d /var/run/php/* | grep sock --color=never to view all of the versions of PHP-fpm that are available on your system for you to use. The version that you just installed should be listed here.
In the line of your config file that says server unix:/var/run/php/php7.3-fpm.sock;, replace the file referenced with one of the files listed in step 2.
Don't forget to reload Nginx when done. On Ubuntu and Debian systems this is done with the command sudo systemctl reload nginx.

How to setup nginx for a Flask app on a server with Plesk installed

I want to deploy a little Flask webapp on a root cloud server with Plesk installed. I followed this tutorial as it uses Nginx and Gunicorn, like proposed by the developer of Flask: Miguel Grinberg.
The problem is that Plesk has a modified variant of nginx pre-installed named sw-nginx and I've absolutely no clue on how to implement that line of code:
server {
listen 80;
server_name my.subdomain.com;
location / {
include proxy_params;
proxy_pass http://unix:/var/www/vhosts/mydomain.com/my.subdomain.com/myproject/app.sock;
}
}
If I just put in in a .conf file, the configtest of Nginx fails:
$ nginx -t
nginx: [emerg] open() "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/conf.d/myproject.conf:6
nginx: configuration file /etc/nginx/nginx.conf test failed
Maybe the syntax of the proxy_pass value is wrong (I tried many, here I put one of them), or maybe the proxy_pass param isn't supported or has to be implemented somewhere else?
PS: I also found this answer How to deploy Flask project on Plesk subdomain
But I read somewhere, that replacing sw-nginx by the standard nginx wouldn't be supported by Plesk. Don't want to break anything...
Thanks for your help.

Nginx can't serve static content and return 403(Forbidden) error page

I am new to nginx and trying to serve static contents with nginx and getting 403 error.I have server config like this:
server {
listen 8000;
server_name localhost;
root /Users/ismayilmalik/Documents/github/nginx-express;
location / {
index index.html;
}
I have executed commands below:
chmod -R 755 /nginx-express
chmod -R 644 /nginx-express/*.*
And the folder has drwxr-xr-x rigt.What's wrong here?
Please go to your nginx error logs to get details.
Run this command to show last errors:
tail -20 /var/log/nginx/error.log
It's good to go through error logs located /var/log/nginx/***.error. I had problems similar to this once. The solution was the user nginx was running as.
If nginx is running as www then www will not have access to ismayilmalik folders unless you also grant access to /Users/ismayilmalik home folder, but that is not secure. The best solution would be to allow nginx to run as ismayilmalik if you want to access your home folder through nginx.
I solved it finally.Actually nginx had all permission to serve static content from:
/Users/ismayilmalik/Documents/github/nginx-express;
The reason was when started nginx could not create error.log file in it'sroot directory. After manually creating the file it worked fine. I am using macOs and to find logs folder executed the command below to find all enironment variables for nginx:
nginx -V
BTW before this I had changed nginx user to from nobody to admin in main config file like below.
user [username] [usergroup]
By default nginx master process runs under root and child process under nobody.

Deploying multiple meteor apps on a single server (multitenancy). Nginx -passenger configuration for 3rd level domain

I need to deploy more than one bundled meteor apps on the same server, using nginx-passenger. I followed the phusion passenger guide and all works smoothly with one app. No documentation find on how to configure nxinx-passenger (I mean the appX.mydomain.conf in /etc/nginx/sites-availabe) to run more than one app. I need to publish the apps in the form app1.mydomain.com, app2.mydomain.com and so on. Someone can help me to understand how to do?
Thanks in advance!
Edit: my original config file
server {
listen 80;
server_name app1.mydomain.com;
# Tell Nginx and Passenger where your app's 'public' directory is
root /path/toApp1/bundle/public;
# Turn on Passenger
passenger_enabled on;
# Tell Passenger that your app is a Meteor app
passenger_app_type node;
passenger_startup_file main.js;
# Tell your app where MongoDB is
passenger_env_var MONGO_URL mongodb://localhost:27017/myapp1db;
# Tell your app what its root URL is
passenger_env_var ROOT_URL app1.mydomain.com;
}
Edit: my proposed config file for the second meteor instance
server {
listen 80;
server_name app2.mydomain.com;
# Tell Nginx and Passenger where your app's 'public' directory is
root /path/toApp2/bundle/public;
# Turn on Passenger
passenger_enabled on;
# Tell Passenger that your app is a Meteor app
passenger_app_type node;
passenger_startup_file main.js;
# Tell your app where MongoDB is
passenger_env_var MONGO_URL mongodb://localhost:27017/myapp2db;
# Tell your app what its root URL is
passenger_env_var ROOT_URL app2.mydomain.com;
}
You will need a file for each Meteor instance in
/etc/nginx/sites-available
similar to the one you have already.
You will need to sym-link these files to
/etc/nginx/sites-enabled
The name of the files isn't important except for your convenience, let's say you have domain app1.mydomain.com, so the file is called app1
Save this as
/etc/nginx/sites-available/app1
and issue these commands:
cd /etc/nginx/sites-enabled
ln -s ../sites-available/app1
You will just need to sort out which ports each server will run on, and set up the contents of the nginx files (which I assume you did already for the first one)

Nginx keeps showing Welcome to Nginx

I am using Cent OS 6.1.
I installed Nginx by ./configure method from source. I started the nginx server by sudo nginx and it can serve the Welcome to Nginx page.
However, when I edit the /usr/local/nginx/conf/nginx.conf file, I found that changing the ...location / {... }... block has no effects.
For example, changing
location / {
root html;
index index.htm index.html;
}
to
location / {
root xyz123; #which does not exist
index index.htm index.html;
}
should give 404. But it keeps showing the welcome page.
Even I remove the whole location block, it still shows the welcome page. But if I change the /usr/local/nginx/html to /usr/local/nginx/htmlxyz it shows 404. Is there another conf file running that overridden the nginx.conf?
p.s. I did sudo nginx -s stop then sudo nginx or sudo nginx -s reopen but didnt help :(
Why you install over EPEL. I've installed 10 nginx server just like that and it is working with Node.js.
Maybe you can get some error ? Show full ./configure.
Example Nginx / Php / Mysql for Centos :
https://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-centos-6.4
Default centos nginx html path :
/usr/share/nginx/html/

Resources