How to Run a Django Website on a Live Server with Linux 


               


How to Run a Django Website on a Live Server with Linux



Linux


In this article, we're going to show all the steps need to run a a django website on a live server with linux, specifically in this article, we are referencing the ubuntu linux operating system.

This doesn't mean that your computer has the ubuntu linux operating system. What it means is you are running the commands on a ubuntu linux command terminal, usually from the internet. The web hosting company that you are hosting your website on usually provides a console. This is where you type in commands.

So let's begin.

So the first thing you have to do is choose a web hosting company to host your website on.

There are plenty of online web hosting companies for django project, including vultr and digitalocean.

In this article, I choose the vultr web hosting company.

So you sign up for an account with vultr.

You then click the + sign, which allows you to deploy a new server.

As a first option, you must choose a location. I chose 'New York'. You choose any where where you want your server to be located. It doesn't matter that much, but if you have an area where you know you have more users, then select this location. But this isn't critical.

After this, select the operating system (os), Ubuntu 16.04 x64.

For the server size, you can select the smallest size, 20 GB SSD (which as of the time of writing this is $2.50/month which is very cheap).

Then the only thing you have to do after this is choose a Server Hostname & Label, which may be 'mywebsite' or 'myvideosite' or 'videosharingsite'.

You then select 'Deploy Now' at the bottom.

If you check into the details of your account, you will see you have a default root user and a password created for you.

Now open the console, which is the command terminal where you run linux commands to set up your private virtual server.

You will have to log into your root user.

So as the user, specify root

Then type in the temporary password that was given to you by the web hosting company.

But you don't want to keep this password given to you by the web hosting company. You want to change it to what you want.

This is specified by the line below.



You will then be prompted to change the password to whatever you want. You will be asked to re-enter the password.

If you get the message that the password has been successfully updated, then the password change was successful.

After you have done this, you want to create a new user.

The root user can be a pretty dangerous thing in linux, because the root user has all privileges. This can make it a security threat. So it's advised to create a new user other than the root user.

So we do this through the following line below.



This adds the user, david, to the remote system.

You will then be prompted to create a password for this user, david.

You now have a new user to this remote system.

So being that we said the root user is dangerous to use in terms of security, we want to work through another user, not the root user.

But the other user, in this case, david, must have some administrative privileges, such as the ability to run the sudo command.

So to give these privileges to the user, david, we specify the following line below.



So now the user, david, has privileges to run administrative commands such as sudo.

As before, we mentioned that allowing login to your system through the root user is dangerous and is a security flaw. Therefore, now that we created a user that has sudo privileges, we can disable root user login.

We disable root user login by specifying the following line.



nano is a text editor in linux.

What we're doing is opening up the SSH configuration settings in this nano text editor.

What we do now is we go to the PermitRootLogin attribute and changes yes to no. We then press Ctrl+X to exit out. We will be asked if we want to save these changes. We specify 'Y' for yes. We then press Enter. These changes have now been made.

Being that we have made these changes, we want to reload the SSHD configuration settings.

We do this through the following line shown below.



Now this reloads the SSH configuration settings we have made.

So now that this is all set, we can now log in to the new user we have created.

We do this through the following line shown below.



To log in to another user, you specify the user name, followed by the @ sign, followed the website IP address.

One last thing we want to do for security purposes for our system is set up a firewall.

This is done by the following statements below.



So now with these commands, the firewall should be active and enabled.

So this just adds give security measures to our private virtual server.

So the first thing we have to do is make sure that our VPS has all updates.

So we get all updates through the following line shown below.



We then want get all current upgrades to our VPS.

This is done by the following line of code below.



Now we have all upgrades to our VPS.

Python3 should already be installed on the system. To test this, type in, python3. You should see the Python3 terminal appear.

So now we just need to install pip.

We do this through the following line shown below.



We now have pip installed in our remote system, meaning we can install python modules for our remote system.

We next install nginx. nginx is an integral part of serving up a django website, because it works in coordination with the gunicorn software, which is software that acts as the django web server. When a user requests our website, nginx acts to route their request to gunicorn, which is is the django web server. And this ultimately serves up the django page.

This is done by the following line shown below.



So now we have nginx installed.

Next we want to install virtualenv

We do this through the following line shown below.



We now have the virtual environment installed so that each project can be self-contained.

However, before we proceed with a virtual environment, we want to get our django project on the server. We want to upload our django project to the server.

One of the best ways to do this is through filezilla.

If you don't have filezilla, go the website filezilla

To log onto your server, specify the IP address as the host name, the user that you created as the user name, and the password for the user.

This login must be done through SFTP- SSH File Transfer Protocol. You don't have to specify a port.

Now upload your topmost project root directory to the server. This should occur in the folder, home/username/. In my case, this is home/david/

Before you upload the code, however, there are 2 things you must do in the settings.py file of the project.

First, you must add the IP address of your website to the ALLOWED_HOSTS variable in the settings.py file. So if your IP address is 22.33.45.129, the line will be, ALLOWED_HOSTS = ['22.33.45.129']. Later if you want to have a named domain server, then you also have to specify the URL of the website, for example, tomshardware.com. The line would be, ALLOWED_HOSTS = ['22.33.45.129', 'tomshardware.com']

Another thing you must do in the settings.py file is add a STATIC_ROOT variable. This allows static files to be shown on a live server. If you create a static directory right underneath the topmost project directory (where all apps are), then you specify the line, STATIC_ROOT= os.path.join(BASE_DIR, 'static/'). More on this will be discussed.

We then upload the project.

So once this is uploaded, we can now set up our virutal environment for our project.

We go into the project directory. Let's say the project directory is mywebsite.

Make sure that you're in this project directory by specifying the absolute path with the chdir command or using the cd function, specifying cd mywebsite.

We then specify the following line of code below.



What this line of code does is it creates a virtual environment folder called venv in the project directory. This is needed in order to activate a virtual environment for a project.

To activate a virtual environment, we use the following statement shown below.



You should now see (venv) to the left of the comamnd prompt.

We're now in the virtual environment.

We want to now install all the modules we need to run our website.

The most important module is the django module.

We install the django module by the following line of code shown below.



So now django is installed.

Also, at this time, we want to install any other modules you need, such as Pillow, numpy, matplotlib, etc.

Next we need to install gunicorn, which is the django web server. It serves django projects.

We install gunicorn with the following line.



gunicorn is installed within the virtual environment, just like all the other modules.

gunicorn is kind of like runserver, as in how we run a server on a local computer with, python3 manage.py runserver. However, it is more efficient and is meant to be the 'runserver' for production websites. With the runserver command, if you stop runserver, the website goes down. gunicorn can serve pages up perpetually.

Now that this is up and running, we get out of the virtual environment for our project.

We do this through the following line,



We should now be out of our virtual environment.

You should no longer see (venv) to the left of the command terminal.

Next, we need to create a gunicorn.service file.

We are able to create a gunicorn.service file by specifying the following line of code shown below.



This opens the nano text editor within this console.

We now have to write the gunicorn.service file.

In this gunicorn.service file, we specify a user name, the working directory for our project, the virtual environment for our project, as well as a sock file. It's just various metadata for our project that is essential for gunicorn to know. Remember that gunicorn is the django web server. It needs to know the user. It needs to know the working directory for the project. It needs to know the virtual environment. We also need to create a sock file for the project. This socket is used as a communication channel in which gunicorn and nginx communicate with each other.

You should have the following in the gunicorn.service file.



Make sure that the whole line of ExecStart is all on one line. Or else, the code will not work.

After you have entered all of this, press Ctrl+X. Then press, 'Y', in order to save this gunicorn.service file. Then press 'Enter'.

We have now created our gunicorn.service file, which is essential in order for gunicorn to work with our website.

Again, gunicorn is the django web server. It allows for the processing of a django project.

So now to start gunicorn, we specify the following line.



We next enable gunicorn.



So now gunicorn is enabled.

Now that gunicorn is enabled, we need to finish configuring nginx, which handles traffic to gunicorn, in order for a django project to be processed on a live server.

We do this by specifying the following line shown below.



So we now open up the nano editor again. Any time we type in sudo nano, we are opening up the nano text editor. Last time, it was to configure the gunicorn.service file. Now we are setting up the nginx configurations. Nginx routes traffic to gunicorn so that our website can be served to the web.

For these nginx configurations, we write in the following code below.



In these nginx configurations, we listen on port 80, which is the default port of the web to listen to web requests.

We then must specify the IP address and/or domain name of our website.

Since right now, all we have is the IP address, we specify the IP address after the server_name.

The file also addresses our static files.

We also must specify our sock file, which was created in the gunicorn.service file.

This socket is how the nginx and gunicorn file communicate with each other, how nginx is able to route traffic to the gunicorn server.

Next save and close the file by typing in Ctrl+X, 'Y', and then 'Enter'

Next, we enable the file by linking it to the sites-enabled directory.

So we simply made this site available in the sites-available directory. Now we must make enable this site by linking it to the sites-enabled directory.

This is done through the following line shown below.



So now our website, our django project mywebsite, is enabled on nginx.

nginx and gunicorn now serve up the website permanently unless we disable it.

We can now test our nginx configuation for syntax errors by typing in the following line shown below.



If there are no errros, then restart nginx by typing in the following line shown below.



So now nginx is restarted.

Lastly, we specify the following line.



So we are all up and running.

Now type in your IP address in a web browser and your website should appear.

Everything should work except for the display of static files.

To make the static files appear, type in the following line.



The static files should now be displayed on your website.

So now the website is fully functional with the IP address.

If you want a domain name to your website instead of an IP address, then you need to buy a domain name or take an existing domain name and assign it to this IP address.

Then you need to go back into your settings.py file. If the domain name you want is petersclambar.org which has an IP address of 22.33.44.129, then the ALLOWED_HOSTS variable should be, ALLOWED_HOSTS = ['22.33.44.129', 'petersclambar.org']

One last thing we must do is edit the nginx configuration file in the sites-available directory.

We open up this file through the following line below.



This opens up the nano editor so that we can edit the nginx configurations.

We then change the file to following.



All we need was change the server_name to the IP address and the domain name.

We now have petersclambar.org live. This is a Django website live on the web.

And this is how to run a django website on a live server with linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...