One of the main problems faced by a developer is hosting his application in a development server without the help of a Devops person. Developers will always have to depend on someone else to deploy their code to a development server.
In this blog, we will go through four scripts which will help developers to deploy their django application to a development server hosted in AWS EC2 ubuntu instance without any Devops help.
We will use Nginx as the webserver and uWsgi as the webserver gateway interface.
1) Firstly you need to setup a django application if you do not have one and create a uwsgi socket file.
django-uwsgi.sh (Create a file and copy the below snippet)
DEPLOYMENT_PATH='/home/ubuntu'
cd ${DEPLOYMENT_PATH}
virtualenv sample_env --no-site-packages
cd ${DEPLOYMENT_PATH}/sample_env && source bin/activate
pip install django==1.7.1
pip install uwsgi
cd ${DEPLOYMENT_PATH}
django-admin.py startproject sample
sudo apt-get install nginx
echo '[uwsgi]
master = true
socket = /tmp/uwsgi.sock
chmod-socket = 666
chdir = /home/ubuntu/sample
wsgi-file = /home/ubuntu/sample/sample/wsgi.py
virtualenv = /home/ubuntu/sample_env
vacuum = true
enable-threads = true
daemonize= /home/ubuntu/sample/uwsgi.log' >> uwsgi.inideactivate
If you already have a django application you can skip the above commands for creating a django app and virtual environment from the script and execute it.
2) Next we need to configure the nginx webserver. Copy the nginx config file to the project directory and execute the next script
nginx.config
server {
listen 80;
# server_name example.com;
error_log /home/ubuntu/sample/sample.log;
location / {
uwsgi_pass unix:///tmp/uwsgi.sock;
include uwsgi_params;
}
}
When the script is executed the nginx config file is copied to the location “/etc/nginx/sites-available/”.In the nginx configuration we define default http port 80.
3) Finally we need to restart the nginx webserver to reflect the changes in the web.
nginx-restart.sh
#!/bin/sh
DEPLOYMENT_PATH='/home/ubuntu'
. ${DEPLOYMENT_PATH}/sample_env/bin/activate
sudo killall -s INT uwsgi
uwsgi --ini /home/ubuntu/uwsgi.ini
sudo service nginx restart
To execute these scripts, change their permissions such that these files are executable except for the nginx config file. For that, execute the following command in the terminal.
chmod +x filename.sh
To deploy your webapp local or any other server just change the location of the directory paths accordingly in the four scripts.
After executing these scripts you can check the browser with the server ip-address or your domain name and if you are using it in local, use localhost as domain name.
Please note that these scripts are just to host your web app in a development server, do not use them to deploy it in production server.
We collaborate with visionary leaders on projects that focus on quality and require the expertise of a highly-skilled and experienced team.