Question

How to run multiple website on same Droplet that already host an active website?

I’m quite new to backend and servers configurations.

I have a Django website already running using Nginx and Gunicorn and is successfully deployed to my Droplet.

I’m deciding to host multiple websites including test apps of different sorts i.e PHP, Wordpress, Laravel, Java supported backends, on the current droplet. But I don’t know exactly how to do it. I have some rough knowledge of Nginx Blocks. However I cannot dare to experiment with it because I’m afraid if something goes wrong my current running website would go down.

I don’t know how to do it? Will it affect my current website? How can I separate my current active website form all other process running in my Droplet?

Any help will be appreciated and also I would love to refer me to some tutorials and references so I can achieve what I want.

Note: I’m running Ubuntu 18.04 on my Droplet.

Thanks


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
July 19, 2021

Hello,

As you already have Nginx installed, what you could do is to use separate Nginx Server Blocks for each of your websites.

You can follow the steps here on how to do that:

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

For PHP you could follow the steps here on how to install it on your server, you can skip the Nginx installation step as you already have this installed:

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04

Hope that this helps. Regards, Bobby

KFSys
Site Moderator
Site Moderator badge
April 26, 2024

Configuring multiple Django projects on a single DigitalOcean droplet using Nginx as a reverse proxy involves several key steps, including setting up Nginx server blocks (similar to virtual hosts in Apache) and managing Django project settings for production. Here’s a comprehensive step-by-step guide on how to do this:

Step 1: Prepare the Droplet

  1. Create a Droplet: If you haven’t already, create a DigitalOcean Droplet with a Linux distribution like Ubuntu.

  2. SSH into your Droplet:

    bashCopy code

    ssh root@your_droplet_ip

  3. Update your system:

    bashCopy code

    sudo apt update && sudo apt upgrade

Step 2: Install Dependencies

  1. Install Python, pip, and other necessary packages:
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl git
  1. Install and set up virtual environments for Python to isolate project dependencies:
sudo apt install python3-venv

Step 3: Set Up Your Django Projects

For each Django project:

  1. Create a project directory:
mkdir /var/www/project1 cd /var/www/project1
  1. Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate
  1. Install Django and other dependencies (make sure to have a requirements.txt):
pip install django gunicorn psycopg2-binary
  1. Configure your Django project settings to connect to the database and manage static and media files correctly.

  2. Collect static files:

python manage.py collectstatic
  1. Create a Gunicorn systemd service file for each project to handle the application server. Example for project1:
sudo nano /etc/systemd/system/project1_gunicorn.service

Add the following configuration:

[Unit]
Description=gunicorn daemon for project1
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/project1
ExecStart=/var/www/project1/venv/bin/gunicorn --workers 3 --bind unix:/var/www/project1/project1.sock project1.wsgi:application

[Install]
WantedBy=multi-user.target

Start and enable the service:

sudo systemctl start project1_gunicorn
sudo systemctl enable project1_gunicorn

Step 4: Configure Nginx as a Reverse Proxy

  1. Create a Nginx server block for each project:
sudo nano /etc/nginx/sites-available/project1

Example configuration for project1:

server {
    listen 80;
    server_name project1.domain.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www/project1;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/project1/project1.sock;
    }
}

Enable the Nginx server block by linking it to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/project1 /etc/nginx/sites-enabled
  1. Test the Nginx configuration and restart Nginx:
sudo nginx -t
sudo systemctl restart nginx
  1. Repeat for each additional Django project, ensuring you use unique socket files and server names.

Step 5: Secure Your Server

  1. Set up a firewall (e.g., using UFW):
sudo ufw allow 'Nginx Full'
sudo ufw allow ssh
sudo ufw enable
  1. Add SSL certificates using Let’s Encrypt for secure HTTPS connections:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d project1.domain.com

Final Steps

  • Double-check all configurations to ensure there are no conflicts between server blocks.
  • Regularly update your system and Django applications for security and stability.

By following these steps, you can efficiently run multiple Django projects on a single DigitalOcean droplet, each accessible via its unique domain and securely handled by Nginx as a reverse proxy.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel