The easiest way to deploy DJANGO project on digital ocean, aws on ubuntu server

Karan Goyal
3 min readNov 27, 2020

--

Create a droplet / ec2 / lightsail ubuntu instance choose nearby server location, Choose password protected droplet rather than ssh it will make it easier

You can get a $100 bonus for 2 months using my referral on digital ocean droplet, it’s worth trying cloud hosting for 2 months
Click here for bonus

  1. Open access console you will find it’s an option at:-

2. Login with username as root and password which you entered above and then enter the following commands

ufw enable
ufw allow OpenSSH

3. Now close the console And open the command prompt or terminal on your computer

4. On terminal type command (IP will be provided when you generate new instance/droplet)

ssh root@ip_adress

5. Login with your droplet password and hit the following commands

sudo apt-get updatesudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3sudo pip3 install virtualenvcd /mkdir myprojectcd myproject

6. Make a virtual environment for your app

virtualenv projectenvsource projectenv/bin/activatepip install django (or pip install -r requirements.txt)

If you got psycopg2 error run following command
sudo apt-get install libpq-dev python-dev

7. Upload your code folder either by git or Filezilla, make sure you have the below settings in settings.py

ALLOWED_HOSTS = ["server_domain_or_IP or * "]
. . .

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

8. Run the code

python manage.py collectstaticsudo ufw allow 8000python manage.py runserver 0.0.0.0:8000

Now your website should be live, you can test it by opening your http://your_droplet_ip:8000

Now deactivate the virtual environment and get ready for apache settings

(projectenv) $ deactivate

Configure Apache Server

  1. Type the following command or open this file using a file manager
sudo vim /etc/apache2/sites-available/000-default.conf 

Commet out the document root at first as in below image (put # in front)

2. Enter the following code in that file

Alias /static /..static directory.../static<Directory /....static directory....../static>
Require all granted
</Directory>

<Directory /...directory of wsgi.py ...../myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-home=/.... directory of python env....../myprojectenv python-path=/.... directory of manage.py .../ WSGIProcessGroup myproject
WSGIScriptAlias / /.... directory of wsgi file ..../wsgi.py

This is my example apache code

Apache settings for djano

Just save it.

3. Now type the following commands

cd /myproject/code_folder_name
chmod 664 db.sqlite3
sudo chown :www-data db.sqlite3
sudo ufw allow 'Apache Full'
cd ../chmod 777 -R code_folder_name

4. Just restart the server your app should be working fine on IP.
sudo service apache2 restart

Thanks for reading

--

--

Karan Goyal
Karan Goyal

Written by Karan Goyal

Full Stack Developer, Top Rated on Upwork, Worked on 90+ jobs in past 9 months and all jobs are rated 5 stars, coding is fun, and always looking forward to code

No responses yet