{"id":2384,"date":"2022-07-26T12:08:04","date_gmt":"2022-07-26T12:08:04","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2384"},"modified":"2024-10-19T17:10:14","modified_gmt":"2024-10-19T17:10:14","slug":"django-red-hat","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/django-red-hat","title":{"rendered":"How to deploy Django application on Red Hat Linux Server"},"content":{"rendered":"<p><strong><a href=\"https:\/\/www.checkmateq.com\/python-development\">Django<\/a> is an open-source python-based framework that allows us to create <a href=\"https:\/\/www.checkmateq.com\/web-development\">web<\/a> applications on <a href=\"https:\/\/www.checkmateq.com\/cloud\">AWS Cloud Ec2 instances<\/a><\/strong>.<\/p>\n<p>In this blog, we will deploy our Django application on a Red Hat Linux Machine or AWS ec2 instance. We will use <strong>Gunicorn<\/strong> as a WSGI server and <strong>Nginx <\/strong>as a web server to improve the application scalability. Django by default comes with SQLite database but we will use <strong>PostgreSQL <\/strong>database.<\/p>\n<h3>Step1: Install the packages<\/h3>\n<p>Login to your remote server and install the <strong>p<\/strong><strong>ython<\/strong>,<strong> postgresql,<\/strong> and <strong>nginx<\/strong> packages using the following commands.<\/p>\n<pre><code>sudo yum update\r\n<\/code><code>sudo yum install python3-pip python3-devel python3-virtualenv postgresql-server postgresql-devel postgresql-contrib gcc nginx \r\n<\/code><\/pre>\n<h3>Step2:\u00a0 Configure PostgreSQL database<\/h3>\n<p>First, for setting up a PostgreSQL instance, run the following command.<\/p>\n<pre dir=\"ltr\">sudo \/usr\/bin\/postgresql-setup --initdb<\/pre>\n<p>Next, start PostgreSQL with systemctl.<\/p>\n<pre><code>sudo systemctl start postgresql<\/code><\/pre>\n<p>Now, create a database and user.<\/p>\n<pre><code>sudo su - postgres<\/code> <code>psql\r\n<\/code><code>CREATE DATABASE djangoapp;\r\n<\/code><code>CREATE USER djangouser WITH PASSWORD 'password123';<\/code> <code>\r\nGRANT ALL PRIVILEGES ON DATABASE djangoapp TO djangouser;\r\n<\/code>ALTER ROLE djangouser SET client_encoding TO 'utf8'; ALTER ROLE djangouser SET default_transaction_isolation TO 'read committed'; ALTER ROLE djangouser SET timezone TO 'UTC'; <code>\\q<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>Next, we need to make changes to the client authentication file &#8220;<strong>pg_hba.conf<\/strong>&#8220;. By default, it allows connections only when the system user matches the database user.<\/p>\n<pre><code>sudo nano \/var\/lib\/pgsql\/data\/pg_hba.conf<\/code><\/pre>\n<p>Scroll down and replace<strong> ident<\/strong> with<strong> md5<\/strong> in the host column.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2424\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer-1-300x43.png\" alt=\"\" width=\"740\" height=\"106\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer-1-300x43.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer-1-1024x146.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer-1-768x110.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Hire-DevOps-Developer-1.png 1057w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now, restart the service and enable it so that it can start automatically at boot.<\/p>\n<pre><code>sudo systemctl restart postgresql\r\nsudo systemctl enable postgresql<\/code><\/pre>\n<h3>Step3: Clone the repository and create a virtual environment<\/h3>\n<p>First, we will clone our project from the Github repository in a directory.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2426\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-1-300x46.png\" alt=\"\" width=\"750\" height=\"115\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-1-300x46.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-1-1024x156.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-1-768x117.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-1-1200x183.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/amazon-aws-services-1.png 1326w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now, create a virtual environment with the following command.<\/p>\n<pre><code>virtualenv venv<\/code><\/pre>\n<p>Activate the virtual environment with the following command.<\/p>\n<pre><code>source venv\/bin\/activate<\/code><\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2431\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/full-stack-development-company-300x68.png\" alt=\"\" width=\"746\" height=\"169\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/full-stack-development-company-300x68.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/full-stack-development-company-768x174.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/full-stack-development-company.png 997w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Next install Django, Gunicorn, and psychopg2 packages in the virtual environment.<\/p>\n<pre><code>pip3 install django gunicorn psycopg2<\/code><\/pre>\n<h3>Step4: Configure Django Project<\/h3>\n<p>Go to the<strong> settings.py<\/strong> file in your project and open it.<\/p>\n<pre><code>sudo vi settings.py<\/code><\/pre>\n<p>Find the database block and replace the default database SQLite with the PostgreSQL database that we have created.<\/p>\n<pre><code>DATABASES = {\r\n    'default': {\r\n        'ENGINE': 'django.db.backends.postgresql_psycopg2',\r\n        'NAME': 'djangoapp',\r\n        'USER': 'djangouser',\r\n        'PASSWORD': 'password123',\r\n        'HOST': 'localhost',\r\n        'PORT': '',\r\n    }\r\n}<\/code><\/pre>\n<p>Next, include domain name or server IP associated with your Django server in <strong>ALLOWED_HOSTS<\/strong>.<\/p>\n<pre><code>ALLOWED_HOSTS = ['Domain_Name_or_IP', 'localhost']<\/code><\/pre>\n<p>Now close the file and install the required packages and dependencies with the following command.<\/p>\n<pre>pip3 install -r requirements.txt\r\n<\/pre>\n<p>Run the following commands to migrate the initial database schema to our PostgreSQL database.<\/p>\n<pre>python3 manage.py makemigrations\r\npython3 manage.py migrate\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2443\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company-1-300x129.png\" alt=\"\" width=\"749\" height=\"322\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company-1-300x129.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company-1-1024x439.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company-1-768x330.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-solutions-company-1.png 1177w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Run the following command to create an admin user.<\/p>\n<pre>python3 manage.py createsuperuser\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2444\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/google-cloud-services-1-300x51.png\" alt=\"\" width=\"741\" height=\"126\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/google-cloud-services-1-300x51.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/google-cloud-services-1-1024x174.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/google-cloud-services-1-768x130.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/google-cloud-services-1.png 1174w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Run the following command to collect static content into configured directory location.<\/p>\n<pre>python3 manage.py collectstatic\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2446\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/mobile-app-development-company-300x18.png\" alt=\"\" width=\"750\" height=\"45\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/mobile-app-development-company-300x18.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/mobile-app-development-company-1024x63.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/mobile-app-development-company-768x47.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/mobile-app-development-company.png 1137w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now, test your project by starting up the Django development server with the following command.<\/p>\n<pre>python3 manage.py runserver 0.0.0.0:8000\r\n<\/pre>\n<p>Go to your browser, and make sure you can access your application with public IP on port 8000. Make sure you have allowed traffic on port 8000.<\/p>\n<p>By appending<strong> \/admin<\/strong> to your URL you can access the default Django admin interface. The credentials for login to the admin interface are those that we created using<strong> createsuperuser<\/strong> command.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2449\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-computing-services-1-300x201.png\" alt=\"\" width=\"737\" height=\"494\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-computing-services-1-300x201.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-computing-services-1-768x515.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cloud-computing-services-1.png 811w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now we will test if Gunicorn can serve the application. Run the following command in your project directory to load the project&#8217;s WSGI module.<\/p>\n<pre>gunicorn --bind <span class=\"token number\">0.0<\/span>.0.0:8000 <mark>myproject<\/mark>.wsgi\r\n<\/pre>\n<p>You should be able to access your application from the browser. Make sure to replace the WSGI module in command with the WSGI module of your project.<\/p>\n<p>Now get out of the virtual environment with the following command.<\/p>\n<pre>deactivate\r\n<\/pre>\n<h3>Step5: Create Gunicorn socket and service files<\/h3>\n<p>First, we will create a Gunicorn socket so that when a connection occurs, the system will automatically start the Gunicorn process to handle the connection.<\/p>\n<p>Create a <strong>gunicorn.socket<\/strong> file using the following command.<\/p>\n<pre>sudo vi \/etc\/systemd\/system\/gunicorn.socket\r\n<\/pre>\n<p>Copy the following code block and paste it into the file.<\/p>\n<pre><code>[Unit]\r\nDescription=gunicorn socket\r\n\r\n[Socket]\r\nListenStream=\/run\/gunicorn.sock\r\n\r\n[Install]\r\nWantedBy=sockets.target<\/code><\/pre>\n<p>Now we will create a system service file for gunicorn.<\/p>\n<pre><span class=\"token function\">sudo<\/span> vi \/etc\/systemd\/system\/gunicorn.service\r\n<\/pre>\n<p>Paste the following code block in the file.<\/p>\n<pre><code>[Unit]\r\nDescription=gunicorn daemon\r\nRequires=gunicorn.socket\r\nAfter=network.target\r\n\r\n[Service]\r\nUser=ec2-user \r\nGroup=ec2-user\r\nWorkingDirectory=\/home\/ec2-user\/django\/django-project\r\nExecStart=\/home\/ec2-user\/django\/venv\/bin\/gunicorn \\\r\n          --access-logfile - \\\r\n          --workers 3 \\\r\n          --bind unix:\/run\/gunicorn.sock \\\r\n          <mark>myproject<\/mark>.wsgi:application\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/code><\/pre>\n<p>Replace the working directory and users, and wsgi module in the file accordingly.<\/p>\n<p>Start and enable to gunicorn socket.<\/p>\n<pre>sudo systemctl start gunicorn.socket\r\nsudo systemctl enable gunicorn.socket\r\n<\/pre>\n<p>Step6: Configure Nginx<\/p>\n<p>Open the Nginx configuration file.<\/p>\n<pre><code>sudo nano \/etc\/nginx\/nginx.conf<\/code><\/pre>\n<p>Add a new server block, just above the default block.<\/p>\n<pre><code>server {\r\n    listen 80;\r\n    server_name <mark>server_domain_or_IP<\/mark>;\r\n\r\n    location = \/favicon.ico { access_log off; log_not_found off; }\r\n    location \/static\/ {\r\n        root \/home\/ec2-user\/djangp\/django-project;\r\n    }\r\n\r\n    location \/ {\r\n        proxy_set_header Host $http_host;\r\n        proxy_set_header X-Real-IP $remote_addr;\r\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\n        proxy_set_header X-Forwarded-Proto $scheme;\r\n        proxy_pass http:\/\/unix:\/<\/code>run\/gunicorn.sock<code>; \r\n     } \r\n}<\/code><\/pre>\n<p>Start and enable Nginx.<\/p>\n<pre><code>sudo systemctl start nginx\r\n<\/code><code>sudo systemctl enable nginx\r\n<\/code><\/pre>\n<p>Now access your application from the browser with either your domain name or server IP.<\/p>\n<p>If you get a Bad Gateway error, it&#8217;s because of Selinux.\u00a0 For this, we can change the Selinux state in the Selinux configuration file.<\/p>\n<pre><code>vi \/etc\/sysconfig\/selinux<\/code><\/pre>\n<p>Change the value of SELinux to permissive and reboot your system. You will now be able to access you application.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2463\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/application-development-services-300x80.png\" alt=\"\" width=\"739\" height=\"197\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/application-development-services-300x80.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/application-development-services-1024x274.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/application-development-services-768x205.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/application-development-services-1200x321.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/application-development-services.png 1216w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>We now have a Django application running with Gunicorn WSGI server and Nginx web server.<\/p>\n<p><a href=\"https:\/\/www.checkmateq.com\/contact-us\">Please contact<\/a> with our <a href=\"https:\/\/www.checkmateq.com\/virtual-cto-services\">Virtual CTO Services in India<\/a> if you have anything related to <a href=\"https:\/\/www.checkmateq.com\/cloud\">cloud<\/a> infrastructure, <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">IT Staff Augmentation Services<\/a>, <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">Hire Developers in India<\/a> or <a href=\"https:\/\/www.checkmateq.com\/technology-consulting\">software architecture consulting<\/a> to be discussed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django is an open-source python-based framework that allows us to create web applications on AWS Cloud Ec2 instances. In this blog, we will deploy our Django application on a Red Hat Linux Machine or AWS ec2 instance. We will use Gunicorn as a WSGI server and Nginx as a web server to improve the application &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/django-red-hat\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to deploy Django application on Red Hat Linux Server&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":2470,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[15,2,71,70,69,68,14],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2384"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2384"}],"version-history":[{"count":56,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2384\/revisions"}],"predecessor-version":[{"id":4621,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2384\/revisions\/4621"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2470"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}