I am using ‘ Django One Click Image ’ to create a new droplet on Digital Ocean.
The process of creating new droplet, generate new Django project for each experiment and point to different domain name can be done 100% programmatically. However for speed and simplicity, some of the steps is manually for now. Comment if you think i should build an automated tool to “Create New Experiment on one Digital Ocean Droplet”.
Step 1: Set up New Document Root Directories for Each Experiments
$ sudo mkdir -p /home/experiment1.com
$ sudo mkdir -p /home/experiment2.com
$ sudo mkdir -p /home/experiment3.com
Add new directories for each experiments you want to create. The -p flag will create necessary parent directory along the way.
Next we need to transfer ownership to django user in django group created by Digital Ocean one click image.
$ sudo chown -R django:django /home/experiment1.com
$ sudo chown -R django:django /home/experiment2.com
$ sudo chown -R django:django /home/experiment3.com
The permission of the web root folder /home should be set to 777.
$ sudo chmod -R 777 /home
Step 2: Clone content for each experiment to newly created directory.
$ git clone firstname.lastname@example.org:username/experiment1.git /home/experiment1.com
$ git clone email@example.com:username/experiment2.git /home/experiment2.com
$ git clone firstname.lastname@example.org:username/experiment3.git /home/experiment3.com
Step 3: Create Server Block Files for Each Experiment Domain
We then need to create server block config file for each experiment by copying over the default django config file create by Digital Ocean one click image.
$ cp /etc/nginx/sites-available/django /etc/nginx/sites-avaiable/experiment1.com
We then need to open up the server blog config file in a text editor. You can use Nano or VIM.
$ vim /etc/nginx/sites-available/experiment1.com
Without the comment line, the file should look similar to this
If we look at the listen directives on line 6 and 7, only one of our server block can have the default_server spec. This specifies which block should server a request if the server_name does not match any of available server block.
We also need to change the location Django media, static directory to our experiment1.com directory
Our config file should look like this.
Step 4: Enable our Server Blocks, Create Gunicorn System Service File and Restart Nginx
After creating new server blocks, we need to enable them. We can do this by creating symbolic links to these files in our site-enabled directories.
We can create this link using this commands
$ ln -s /etc/nginx/sites-available/experiment1.com /etc/nginx/sites-enabled/
$ ln -s /etc/nginx/sites-available/experiment2.com /etc/nginx/sites-enabled/
Create new Gunicorn system service file for each new experiment
$ cd /etc/systemd/system/
$ cp gunicorn.service experiment1.service
$ vim experiment1.service
And edit the .service file to point to our experiement1.com directory. After editing, the file should look like this.
Let test Gunicorn to see if it can serve our application
$ cd /home/experiment1.com/experiment1.com
$ gunicorn — — bind 0.0.0.0:8000 django_project.wsgi:application
Then run the following command to start and enable the experiment1.service
$ systemctl start experiment1
$ systemctl enable experiment1
This should create the gunicorn.sock inside your home/experiment1/directory.
Step 5: Point domain name ‘expriment1.com’ to Digital Ocean droplet IP.
Visit your domain name provider and config A and AAAA record to point to ipv4 and ipv6 address of your Digital Ocean droplet.
It will take couple minutes to couple hours for our domain name provider to update your new record.
You can also test it locally in your laptop by editing your hosts file in your local machine.
$ sudo nano /etc/hosts
Add following line with ???.???.???.??? is your Digital Ocean IP address and open expriment.com or www.experiment1.com in any browsers. The browsers will forward your to the DO ip and Nginx shoud start serving your experiment app content.
$ ???.???.???.??? experiment1.com www.experiment1.com
Check your domain to see if its updated with new records using:
$ dig experiment1.com