How to add another website on Ubuntu, Nginx & Docker Print

  • Ubuntu, Nginx, Docker, Virtualization
  • 0

Below are the best of our knowledge that saved many clients from the process hungry control panels by choosing them Ubuntu and Nginx with Docker. 

1. Create a directory - you can map your directory where actual files are laid. 

sudo mkdir -p /var/www/yourwebsite.com 
CD /var/www/yourwebsite.com

sudo chown -R $USER:$USER /var/www/yourwebsite.com


Note, you should put public_html inside yourwebsite.com which really makes strong environment. 

2. Create a docker compose file. (Sometime it creates automatically or it can be docker-compose.yml or compose.yaml (If you have any database or any thing hosted with the docker)

You can insert your docker configuration file code. 

Basically yaml instructs the docker

contains YAML instructions that tell Docker:

  • Which containers to create
  • Which images to use
  • Which ports to expose
  • Which folders to mount
  • Which networks to join

3. Once you upload your files and folder in the yourwebsite.com/public_html You can create Nginx Virtual Host, which basically point the website to a directory that is what Nginx job. To create the nginx configuration file with yourwebsite.com domain use below code. (Note Domain is yourwebsite.com - it can be changed in your own case)

sudo nano /etc/nginx/sites-available/site2.com.conf

Inser the code below

server {
    listen 80;
    server_name site2.com www.site2.com;

    location / {
        proxy_pass http://127.0.0.1:3002;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

4. Enable Website 

sudo ln -s /etc/nginx/sites-available/yourwebsite.com.conf \ /etc/nginx/sites-enabled/

Test Nginx 

sudo nginx -t

Reload 

sudo systemctl reload nginx

5. If you have used the Docker, you should start container with

docker compose up -d

verify

docker ps

You should see the container running on port 3002.

http://yourwebsite.com:3002

Now you need to register SSL certificate than only you can add the port 443

To register the Certificate

sudo sudo certbot --nginx -d yourwebsite.com

Once you register now you can add port 443 into Nginx Configuration file below code.

server {
listen 80;
listen [::]:80;

server_name yourwebsite.com;

return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name india.astratms.com;

root /var/www/yourwebsite.com;
index index.html index.htm index.php;

ssl_certificate /etc/letsencrypt/live/india.astratms.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/india.astratms.com/privkey.pem;

access_log /var/log/nginx/india.astratms.com.access.log;
error_log /var/log/nginx/india.astratms.com.error.log;

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

Note: 
If you are getting 504 GAteway Timeout, 502 Bad Gateway or long running process request time

sudo nano /etc/nginx/nginx.conf

Inside the http {} section add or increase:

proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;
client_body_timeout 300;
client_header_timeout 300;

Or inside specific server { } block

location / {
proxy_pass http://127.0.0.1:8080;

proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
}

Then: 

sudo nginx -t
sudo systemctl reload nginx


Was this answer helpful?

« Back