Install Django on shared hosting server
eeintech Django May 18, 2020 (Updated on Dec. 14, 2022)
If you have not installed Django before on a shared hosting server (with no root access), there are extra steps to the installation process which can make it difficult the first time around. The main difference is that you cannot use the built-in Django server (eg. python manage.py runserver) to run your website (furthermore, it is not recommended for production). To get Django to serve your website, you'll need to use a "middle-man" which translates traditional server requests into Django requests. On shared hosting servers using cPanel, this middle-man is Phusion Passenger.
Before we start, be aware that Django version 2.2 and newer cannot natively run on a shared web-hosted server without a newer version of SQLite, a lot of people ran into this issue and had to install Django version 2.1 or older. Assuming you've already tried to install Django version 2.2 or newer on your shared server and you've tried to run it, this is the error you may have gotten:
$ python manage.py migrate django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
If this is your case, I wrote this SQLite installation guide which will walk through the steps of running the latest version of SQLite on your shared hosting server, check it out!
Are you ready to install Django? Let's do it!
1/ In cPanel, each Python environment is associated with a domain or sub-domain. Make sure your domain is attached to your account or that you have created a sub-domain to welcome your web application.
2/ In the cPanel search bar, enter python and hit Enter. You'll land on the Python Web Applications page where you can create virtual environments for your web application developed in Python and automatically link them to Phusion Passenger's web server.
3/ To create a new virtual environment, click on "Create Application" on the right-hand side: the following form will appear. Check out the instructions below to fill it out.
|(1)||Select the Python version used for the virtual environment, I recommend using the latest version (currently: 3.7.3)|
|(2)||Enter the name of the folder associated to your domain or subdomain that will be used to store the files of your web application|
|(3)||Select the domain or subdomain used for this web application. You can also append a url location (optional) like www.domain.com/django|
|(4)||Those 2 fields are for the Phusion Passenger application which will run your web application. Leave those blank (they will be automatically populated)|
|(5)||Enter the file name and path to the Phusion Passenger log file, it will be handy to know its location to check the status of your application (replace "user" with your username)|
Once complete, click on the "Create" button. cPanel will automatically create your Python virtual environment and link it to Passenger.
4/ Access your server with SSH using your favorite Terminal or the one built-in cPanel (type "terminal" in top search bar). Activate your virtual environment with the following command:
$ source /home/user/virtualenv/name-of-env/3.7/bin/activate
Replace user with your server username and name-of-env with the name you put in the previous form (item 2).
Before we continue, navigate to your web application folder:
(name-of-env) $ cd /home/user/name-of-folder
5/ Now we can install Django and get it up and running in your environment. Start by upgrading pip if you haven't done it yet:
(name-of-env) $ pip install --upgrade pip
Then install Django:
(name-of-env) $ pip install django
Once installed, start a project (replace myproject with your own project name):
(name-of-env) $ django-admin startproject myproject
Navigate to the project folder then run migrations:
(name-of-env) $ cd myproject (name-of-env) $ python manage.py migrate
6/ The last steps are the following:
- Create a superuser (= admin) to log in the admin section of Django
- Update the file used by Passenger as an entry point to your new project
To create an admin in Django, execute the following command:
(name-of-env) $ python manage.py createsuperuser
Enter a username, email address and password (twice). That's it for the admin part.
Now we're going to tell Passenger where your new application is so that it can load it and display Django's demo page at the URL you have specified in the Python application settings (in step 3). But before that, we'll actually check that Passenger is running fine on your server.
Navigate to that URL, do you see the following barebone page?
If yes, Passenger is running correctly. If not, you may want to request help from your hosting provider.
Let's navigate back to the root of your web application directory and edit the passenger_wsgi.py file:
(name-of-env) $ cd .. (name-of-env) $ nano passenger_wsgi.py
Under the sys.path line, add the following line (the second one):
sys.path.insert(0, os.path.dirname(__file__)) # Already in the file, keep it sys.path.insert(0, 'myproject') # Add this line
Then, remove all those lines (in nano editor, scroll down to the "def" line and hit Ctrl+K as many times as there are lines to remove):
def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) message = 'It works!\n' version = 'Python %s\n' % sys.version.split() response = '\n'.join([message, version]) return [response.encode()]
Add the following lines to replace the ones you've just removed (in nano editor, shortcut for pasting is the same as on your system, eg. Ctrl+V/⌘+V):
import myproject.wsgi application = myproject.wsgi.application
Save it (in nano editor: Ctrl+W) with the same name.
Finally, you'll need to restart the Passenger application. To do so, enter python in cPanel's top search bar and hit Enter to open the Python Web Applications page. Locate your web application in the list and restart it (counter-clockwise arrow icon). Now navigate to your web application URL: you should see the Django demo website up and running!
You can use and navigate it the same way you would do on your computer (domain.com/admin to access Django's admin page). Congratulation Django is ready for use on your server!