Deploy Django app on a shared host
This post is about deploying a Django application on Linux shared hosting using Cpanel. I will be guiding you step by step in the process of deploying a new or an existing Django app on shared hosting.
Table of Contents
Why Shared Hosting?
When it comes to deploying Django applications, you have many options available. Deploying on shared hosting might not be the best option overall but it surely has some advantages. The most prominent advantage is cost. Popular options like pythonanywhere or heroku 5$ to 7$ a month on the basic plan which could limit the RAM usage at 512MB and disk usage at 1GB. However, you can get a shared hosting which gives you 1 GB RAM and 20GB SSD for as less as 1.44$ a month. In addition to this, if you already have a website on shared hosting, you do not need to spend anything at all.
Check if your hosting is compatible
To follow this tutorial, your shared hosting must have following features:
- Linux Hosting
- Access to CPanel
- Support for python apps
- Command-Line access to the server
Most of the shared hostings are Linux hostings. You can check your hosting specifications from whichever the hosting company you bought it.
To check if your hosting supports python, log in to your CPanel and look for the option “Setup Python App” marked in the following screenshot. If you can find it, you are good to go.
To check if you have command line access to your server. Try to find “SSH Access” or “Terminal” options in your Cpanel.
If you are having any confusion in checking your hosting for compatibility, you can contact your hosting support. Good hosting providers have good support always ready to assist you on any issue.
If your hosting is compatible, then great!. You can follow this tutorial.
If your hosting is not compatible or you don’t have a hosting yet, you will have to purchase shared hosting. Make sure to check the four features in the above list when buying a shared hosting for Django. It is always better to contact the customer support of the hosting and ask them the questions. In this way, you will also test how good their support is. Remember! “A good hosting has a good support”. The best way to contact is via live chat.
Let me mention here the shared hosting that I am using. I am using the NameCheap shared hosting, the stellar plus plan. Previously, I used the stellar plan of their shared hosting. I can recommend this hosting because I am satisfied with its services. If you buy the stellar plan, you can use Django version 2.1 and host three websites with unlimited bandwidth. If you buy the stellar plus plan, you can use the latest version of Django and host unlimited websites.
If you plan to buy shared hosting for deploying Django, consider using my affiliate link. It will not increase the price for you but I will get a commission from whatever you buy in your first order. If you are going to buy a domain too along with hosting, you can use this link to get a special discount on the domain.
Deploy a Django App
Steps in Deploying
Get Command-Line Access
You can get command-line access to your hosting server in two ways: using SSH or via an online terminal. If you want to work via SSH, read your hosting provider’s documentation on how to do that. I will be using the online terminal because it is less complicated to use.
In your CPanel, open the Manage Shell option.
Turn on Enable SSH access
Now you will see the Terminal option in CPanel. Open it to check if it is working.
If a terminal like shown in the below screenshot opens, you have got the terminal access to your server. Keep this browser tab open. We will need it in the upcoming steps.
Setup a new Python app
In your CPanel, open Setup Python App
Click on the Create Application button.
- Set the Python Version to 3.7.3
- The Application Root is the folder in your hosting where the python application will be created. This will be the folder where you will start or upload your Django project.
- In the Application URL, select the domain/subdomain and sub-directory where you want your Django application to be live. This will be the web address where you will see your Django app.
- In the Application startup file, type
passenger_wsgi.py
- In the Application entry point, type
application
You can also see the settings in the screenshot below. After you are done, click on Create.
Open the URL where you set up your python app. You should see a page like this.
Install Django
After your python app is set up, you will see a page as shown in the screenshot below. Copy the command to enter the virtual environment.
Enter the command into the online terminal by Right Click / Paste and press enter. You will enter the python virtual environment. In my case, the command is
source /home/umercxio/virtualenv/django/3.7/bin/activate && cd /home/umercxio/django
It will not be the same for you. If there is no “&&cd …” part in your command, you will manually have to cd to the directory where you have set up your application.
Now you have to install Django. Do not install the latest version of Django. It will not work properly on shared hosting. I had problems with it after which the hosting support told me to use version 2.1. Install version 2.1 of Django by running the following command.
pip install django==2.1
If any other modules are required for your project, install them here using pip.
You can not use a version of Django greater than 2.1 on shared hosting if you are using MySQL database. Using the latest version of Django on shared hosting is only possible if you use PostgreSQL database. I have a complete tutorial on that here. (This link is also present at the end of this post)
Note: If you are facing an error with SSL when trying to use pip, use python version 3.6 instead of 3.7. This will solve the problem and you will be able to use pip.
Confirm the Django installation by running the following command.
django-admin --version
Start a Django project
Create a new project
Go to the terminal in your Cpanel and enter the following command
django-admin startproject myapp ~/django
Make sure to replace “myapp” with your application name and “django” at the end with the directory name where you set up your python app.
Now open the file manager in CPanel (You can also use FTP) and go the folder where you set up your Django app. In my case, it is the “django” folder. You will see a folder with your app’s name and the manage.py file here.
Edit the passenger_wsgi.py file.
Delete everything in the file and add just one line.
from myapp.wsgi import application
Do not forget to replace “myapp” with your application name. Click on Save Changes to save the changes to the file.
Now open your application folder_ in my case, it is “myapp”.
Edit the settings.py file.
In the ALLOWED_HOSTS list, add the URL where our application will be running (which you provided while setting up the python app). In my case, it is “test.umer.link”. If your site has a www variant, make sure to add that here too if you want your site to be working in www as well.
Now go to Set up Python app in your CPanel and Restart your application.
Now open the URL where you have set up your application and you should see the Django welcome page.
Upload an existing project
Upload your project into the folder where you have set up the python app.
Edit the passenger_wsgi.py file
Delete everything and add the following code:
from app.wsgi import application
Make sure to replace “app” with your own app name.
Edit your project’s settings.py file and add your application URL to the ALLOWED_HOSTS list. If your domain also has a www variant, make sure to add that too in the list.
Set up the database
In this part, we will set up a MySQL database from the hosting with Django. It is also possible to use PostgreSQL database. I have a complete tutorial on that here. For now, let’s continue with the MySQL database because it is commonly available in most of the shared hosting packages.
Go to the MySQL Databases option in CPanel.
Create a new Database
Create a new User. Copy the password of this user somewhere because we will need it in the upcoming steps.
Add the user to the database
Allow all the privileges to this user.
Go to the terminal again. Enter the virtual environment and run the following command to install mysql with pip.
pip install pymysql
Edit your settings.py file. Replace the default database code with this.
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'databasename',
'USER': 'databaseusername',
'PASSWORD': 'databasepassword',
'HOST': 'localhost',
'PORT': '3306',
}
Make sure to replace the databasename with the database name, databaseusername with the user name and databasepassword with the password for the user.
Now edit the __init__.py file in the app directory.
Add the following code to the file and save it.
import pymysql
pymysql.install_as_MySQLdb()
Apply migrations to the database.
python manage.py makemigrations
python manage.py migrate
Create a superuser now if you want to access the admin panel. This step is optional.
python manage.py createsuperuser
Set up static files
Edit your settings.py file and add the following two lines at the end.
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Now open the terminal and run the following command.
python manage.py collectstatic
The static files will be copied to a folder named “static” inside your Django app folder. You have to copy this folder to your domain root.
If you have setup Django app on a subdomain like me, the domain root folder will be a folder in your hosting. If you have set up on the main domain, it will be the “public_html” folder.
Go to the domain root folder and check if the static folder has been successfully copied.
Go to “Setup Python App” in CPanel and Restart the app.
Some changes can be made in the settings to avoid copying the static folder to the domain root every time. I have written a complete guide about media files and static files in Django on shared hosting which you can see here. (The link is also available at the end of the post)
Test the app
Now go to http://yourappurl/admin and log in using the superuser details you created previously.
If you see the admin panel, congratulations! your Django app has been deployed on your shared hosting.
Deploy in 5 minutes
After you have followed this tutorial successfully and practiced deploying an existing application twice, you can deploy the Django app to your shared hosting in under 5 minutes using the pdf handbook that I have created. It just highlights the steps that you have to go through in the process of deploying your application.
Frequently Asked Questions
FAQs
It keeps on showing the error page and I don’t know what went wrong
In your CPanel, go to “Set up Python app”. Edit your problem causing application there and set a path for “Passenger log file”
Restart the python app. Now open the log file to see the error and work on solving that.
Can I use git to get my files?
Yes. You can use git to get your files. Open the terminal in CPanel (or get command-line access via ssh). Navigate to the folder where you want to set up your django app and use git to get the files in that directory.
I am having SSL problem while using pip
Use Python 3.6 instead of 3.7. It will solve the problem.
My hosting does not support python
Get a shared hosting that supports python. I use and recommend Namecheap shared hosting.
In the wsgi file, I have to use the name of the django app or the django project?
You have to use the name of the project. The contents of the passenger_wsgi.py will be:
from projectname.wsgi import application
You can actually find the wsgi.py file inside the project folder. It is in the same folder as your settings.py.
Can I do something so that I do not have to copy the static folder to the domain root every time I run collectstatic?
I have a complete tutorial on this issue here. I am highlighting the main steps here.
Open your settings.py and scroll to the end to change the settings for your static files. Change the static url to:
STATIC_URL = '/static/'
Change your static root to:
STATIC_ROOT = os.path.join('/home/username/public_html/static')
Just replace username with your cpanel username. The above code can be used only if you have installed django on the main domain. In this case, your domain root is the public_html folder.
If you have installed django on a subdomain or an addon domain, you will use the name of the folder (which is your domain or subdomain root) instead of public_html like this:
STATIC_ROOT = os.path.join('/home/username/subdomain.umer.link/static')
Final settings look like this:
I have made some changes in the files and they are not updated on the website
You need to restart the python app every time you make some changes in the files. There are a few points to note:
- If you have made changes in models.py, restart the python app and do migrations.
- If you have added any static files, restart the python app and then do collectstatic.
- For any other file, just simply restart the python app.
I hope this post was helpful. If you have any problem, feel free to ask in the comments. I will update the answers in the FAQ section.
Comments
Post a Comment