Monday, 10 April 2017

Use a domain name to communicate to a MEAN stack server hosted by nginx

I have a Ubuntu Server on DigitalOcean, which hosts several websites. I just built a mean.js stack app on my mac, and I plan to deploy it to production, thus to this existing server (though I don't know if I need to create another droplet like here).

I followed this link to install node.js and mongodb, etc. Then, I cloned my own app from the github:

sudo git clone https://github.com/softtimur/myapp.git /opt/myapp
cd /opt/myapp
sudo npm install
npm start

As a result, in a browser, by entering https://xxx.xx.xx.xx:3000/#/home, it communicates well with the server.

Now, I would like to use the domain name I bought from GoDaddy (ie, myapp.io) rather than the IP address to communicate to the server.

I have modified the records in DNS of myapp.io such that it points to the IP address. As a result, https://www.myapp.io leads well to the server, however, it leads to another page set by nginx by default.

Then, I set /etc/nginx/sites-available/myapp.io and /etc/nginx/sites-enabled/myapp.io as follows:

server {
        listen 3000;
        listen [::]:3000;

        root /opt/myopp/;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name myopp.io;

        location / {
                try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
                client_max_body_size 15M;
        }

        location /phpmyadmin {
           root /usr/share/;
           index index.php index.html index.htm;
           location ~ ^/phpmyadmin/(.+\.php)$ {
                   try_files $uri =404;
                   root /usr/share/;
                   fastcgi_pass unix:/var/run/php5-fpm.sock;
                   fastcgi_index index.php;
                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                   include /etc/nginx/fastcgi_params;
           }
           location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                   root /usr/share/;
           }
        }
}

After restarting nginx, npm start returns an error: Port 3000 is already in use.

Could anyone tell me if this approach is correct? If yes, how could I fix the error, eg., the nginx config file?



via SoftTimur

No comments:

Post a Comment