How to Create or Edit a Gunicorn.service File in Linux



Linux


In this article, we show how to create or edit a gunicorn.service file in Linux.

Gunicorn is a Django web server that is able to process django files.

Gunicorn works in coordination with nginx to serve django websites in a production environment.

In a non-production environment, we simply can use the runserver command in order to test our websites.

But using the runserver command is inadequate for a production website.

Gunicorn is much more efficient for a production website. In coordination with nginx, it can serve web pages permanently on the web.

In order to set up gunicorn, there are a number of things that should be done.

First thing, gunicorn should be installed in the project, typically in the virtual environment of the project. This is done by the line, pip install gunicorn

gunicorn is installed within the virtual environment, just like all the other modules, such as numpy, matplotlib, etc.

Now that this is up and running, we get out of the virtual environment for our project. We do this through the word, deactivate

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 or edit it.

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 folder. We also need to create a sock file for the project. This sock file communicates with nginx, and they are both able to communicate through this socket.

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



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 be set up and 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.

So we have now set up and enabled gunicorn to be run for our website.

With nginx fully configured, we can run a django website in a production environment.

And this is how to create a gunicorn.service file in linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...