<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ruhan Khandakar</title>
    <description>The latest articles on DEV Community by Ruhan Khandakar (@ruhanrk).</description>
    <link>https://dev.to/ruhanrk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F221539%2F1e407e4f-414c-48cb-b96e-316ccfe5ba0c.png</url>
      <title>DEV Community: Ruhan Khandakar</title>
      <link>https://dev.to/ruhanrk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ruhanrk"/>
    <language>en</language>
    <item>
      <title>Deploy Django + MySql Application into AWS EC2 instance with nginx and SSL</title>
      <dc:creator>Ruhan Khandakar</dc:creator>
      <pubDate>Thu, 13 May 2021 04:11:18 +0000</pubDate>
      <link>https://dev.to/ruhanrk/deploy-django-mysql-application-into-aws-ec2-instance-with-nginx-and-ssl-4f98</link>
      <guid>https://dev.to/ruhanrk/deploy-django-mysql-application-into-aws-ec2-instance-with-nginx-and-ssl-4f98</guid>
      <description>&lt;p&gt;All the version used in this tutorial&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python 3.8.X&lt;/li&gt;
&lt;li&gt;Django 3.2.2&lt;/li&gt;
&lt;li&gt;gunicorn 20.1.0&lt;/li&gt;
&lt;li&gt;MySQL 8.0.20 (RDS)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  EC2 Setup
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Create EC2 instance (Screenshots attached below), during this instance creation.
we'll have to download &lt;code&gt;pem/cer&lt;/code&gt; file. It'll be used later to access this instance from the terminal via SSH
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsepcum5zrogbcvfyytte.gif" alt="aws_ec2" width="760" height="418"&gt;
&lt;/li&gt;
&lt;li&gt;To connect the instance, Follow &lt;em&gt;&lt;em&gt;how to connect to instance guide&lt;/em&gt;&lt;/em&gt; from the EC2 dashboard.&lt;/li&gt;
&lt;li&gt;After login into our instance, run the following commands to update our instance -&amp;gt; &lt;code&gt;sudo apt update; sudo apt upgrade -y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In order to use pip3 and venv we have to install some packages -&amp;gt; &lt;code&gt;sudo apt install python3-pip python3-venv&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Let's create one virutal env -&amp;gt; &lt;code&gt;python3 -m venv env&lt;/code&gt;. And activate it -&amp;gt; &lt;code&gt;source env/bin/activate&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now we have to install &lt;code&gt;gunicorn&lt;/code&gt;&lt;sup&gt;&lt;em&gt;&lt;em&gt;&lt;code&gt;details&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;&lt;/sup&gt; (make sure you're in the virtual environment) -&lt;code&gt;pip3 install gunicorn&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: if your project has a MySQL database, you have to install some other packages&lt;br&gt;
&lt;code&gt;sudo apt install python3-dev default-libmysqlclient-dev build-essential libssl-dev&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Now let us pull the repository and install all the dependencies. After this do the necessary &lt;code&gt;migrations&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Let us install nginx&lt;sup&gt;&lt;em&gt;&lt;em&gt;&lt;code&gt;details&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;&lt;/sup&gt; -&amp;gt; &lt;code&gt;sudo apt install nginx -y&lt;/code&gt;. now you can check with &lt;code&gt;sudo nginx&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Before starting the &lt;code&gt;nginx&lt;/code&gt; setup we have to change our instance's  &lt;em&gt;&lt;em&gt;&lt;code&gt;security groups&lt;/code&gt;&lt;/em&gt;&lt;/em&gt; (Check below screenshots)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgpwy0c1466ey9okj07rx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgpwy0c1466ey9okj07rx.gif" alt="ec2_security_group" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now let us do a little test, we're going to start our application by gunicorn (what is it? will discuss it in later.) -&lt;code&gt;gunicorn --bind 0.0.0.0:8000 courier_allocation.wsgi:application&lt;/code&gt;, after running this we'll see something like 👇🏻&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs4lmrdb1aat4p2bamby9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs4lmrdb1aat4p2bamby9.png" alt="Screenshot 2021-05-09 at 4.57.28 PM" width="800" height="134"&gt;&lt;/a&gt; &lt;br&gt;
and now we can access our application from our instance IP address, like this &lt;code&gt;ec2-XX-XX-XX-XX.ap-south-1.compute.amazonaws.com:8000&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But now if we close our terminal then we won't be able to access our application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now we have to do some setup so that our application can run in the background. Here, we'll use &lt;code&gt;supervisor&lt;/code&gt;&lt;sup&gt;&lt;em&gt;&lt;em&gt;&lt;code&gt;details&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;&lt;/sup&gt;. To install this package we have to run &lt;code&gt;sudo apt install -y supervisor&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After installation, we have to create one supervisor config file. For this follow the below steps.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;First go to &lt;code&gt;cd /etc/supervisor/conf.d/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Here we'll create one config file -&amp;gt; &lt;code&gt;sudo touch gunicorn.conf&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In this &lt;code&gt;conf&lt;/code&gt; file paste the below code.&lt;/li&gt;
&lt;/ol&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    [program:gunicorn]
    directory=/home/ubuntu/PROJECT_FOLDER
    command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/PROJECT_FOLDER/app.sock DJANGO_PROJECT_FOLDER.wsgi:application
    autostart=true
    autorestart=true
    stderr_logfile=/home/ubuntu/logs/gunicorn.err.log
    stdout_logfile=/home/ubuntu/logs/gunicorn.out.log
    [group:guni]
    programs:gunicorn
&lt;/code&gt;&lt;/pre&gt;

&lt;/blockquote&gt;




&lt;ul&gt;
&lt;li&gt;Now we have to run some commands&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;sudo supervisorctl reread&lt;/code&gt; -&amp;gt; Output - &lt;em&gt;&lt;em&gt;&lt;code&gt;guni: available&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo supervisorctl update&lt;/code&gt; -&amp;gt; Output -  &lt;em&gt;&lt;em&gt;&lt;code&gt;guni: added process group&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo supervisorctl status&lt;/code&gt; -&amp;gt; Output -  &lt;em&gt;&lt;em&gt;&lt;code&gt;guni:gunicorn                    RUNNING   pid 20199, uptime 0:00:17&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;




&lt;ul&gt;
&lt;li&gt;Now let's do the nginx&lt;sup&gt;&lt;em&gt;&lt;em&gt;&lt;code&gt;details&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;&lt;/sup&gt; configuration&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;First we have to go to - &lt;code&gt;cd /etc/nginx/sites-available/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Here we need to create one config file - &lt;code&gt;sudo touch django.conf&lt;/code&gt; (you can choose any file name). Paste the below code inside your newly created file
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    # Nginx Configuration for sample Django application
    ######################################################
    server {
        listen 80;
        server_name ec2-XX-XX-XX-XX.ap-south-1.compute.amazonaws.com;
        location / {
            include proxy_params;
            proxy_pass http://unix:/home/ubuntu/PROJECT_FOLDER/app.sock;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;After this run - &lt;code&gt;sudo ln django.conf /etc/nginx/sites-enabled/&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;now let's test - &lt;code&gt;sudo nginx -t&lt;/code&gt; - Output -&amp;gt; &lt;code&gt;nginx: the configuration file /etc/nginx/nginx.conf syntax is ok&lt;/code&gt; &lt;code&gt;nginx: configuration file /etc/nginx/nginx.conf test is successful&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Sometime you might get this error -&amp;gt; &lt;code&gt;nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64&lt;/code&gt;. To fix this we have to add &lt;code&gt;server_names_hash_bucket_size  128;&lt;/code&gt; at the top of your http block (located in &lt;code&gt;/etc/nginx/nginx.conf&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Now we have to restart our nginx service -&amp;gt; &lt;code&gt;sudo service nginx restart&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Our &lt;code&gt;nginx&lt;/code&gt;&lt;sup&gt;&lt;em&gt;&lt;em&gt;&lt;code&gt;details&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;&lt;/sup&gt; is up and running 😁. You can directly access your app from Public IP Address (EC2)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note: Still a lot of configuration is left, like cache, route multiple ports, load balancer etc. Will discuss it in a different post. 🙂&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  SSL Setup
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;* Requirements: domain address which is mapped to your Instance Public IP.&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup SSL certificate with certbot is quite easy. We just need to install some packages and a little bit of setup&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt; Install packages - &lt;code&gt;sudo apt install certbot python3-certbot-nginx&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Before we generate the certificate, we have to add our domain name in our &lt;code&gt;django.conf&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Change existing &lt;code&gt;server_name&lt;/code&gt; to - &lt;code&gt;server_name test.domain.com;&lt;/code&gt; (if you want to map multiple domains then use as space separate like this - &lt;code&gt;server_name test.domain.com testing.domain.com;&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;After setup is done, run the below two commands&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;sudo certbot certonly --nginx&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sudo certbot certonly --nginx&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Now we have to &lt;code&gt;restart&lt;/code&gt; our &lt;code&gt;nginx&lt;/code&gt; - &lt;code&gt;sudo service nginx restart&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Now you can visit your secure website 😊.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Django Static File issue Fix after deployment (production)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;You might notice that in production, &lt;code&gt;CSS/JS/Images/Fonts&lt;/code&gt; files are not working for &lt;code&gt;/admin&lt;/code&gt;.&lt;br&gt;
This is because of the &lt;code&gt;Static&lt;/code&gt; folder issue. In the local development environment &lt;code&gt;Django&lt;/code&gt; use &lt;code&gt;Python HTTPServer&lt;/code&gt;, and it automatically handles &lt;code&gt;Static files&lt;/code&gt;. But in production our server is running through &lt;code&gt;nginx&lt;/code&gt;, that's why we have to do little setup. Let's do the setup.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;First, we have to create one &lt;code&gt;static&lt;/code&gt; folder in our &lt;code&gt;root&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;Now inside our project main &lt;code&gt;settings.py&lt;/code&gt; we just need to add one line of code&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;STATIC_ROOT =  BASE_DIR / 'static/'&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now run this command &lt;code&gt;python3 manage.py collectstatic&lt;/code&gt;&lt;sup&gt;&lt;em&gt;&lt;em&gt;&lt;code&gt;3&lt;/code&gt;&lt;/em&gt;&lt;/em&gt;&lt;/sup&gt;. With this command, &lt;code&gt;Django&lt;/code&gt; will copy all static assets into your &lt;code&gt;Root Static Folder&lt;/code&gt;. In our case, it's the &lt;code&gt;Static&lt;/code&gt; folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now one setup is left, which is &lt;code&gt;nginx&lt;/code&gt;,&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to your &lt;code&gt;nginx&lt;/code&gt; &lt;code&gt;django.conf&lt;/code&gt; file and add the below code after the &lt;code&gt;location&lt;/code&gt; code block&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;location /static/ {
               autoindex on;
               alias /home/ubuntu/PROJECT_FOLDER/static/;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Now restart &lt;code&gt;nginx&lt;/code&gt; server - &lt;code&gt;sudo service nginx restart&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  nginx
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Nginx&lt;/code&gt;, pronounced like “&lt;em&gt;&lt;em&gt;engine-ex&lt;/em&gt;&lt;/em&gt;”, is an open-source web server that, since its initial success as a web server, is now also used as a reverse proxy, HTTP cache, and load balancer.&lt;/p&gt;

&lt;p&gt;Nginx is built to offer low memory usage and high concurrency. Rather than creating new processes for each web request, Nginx uses an asynchronous, event-driven approach where requests are handled in a single thread.&lt;/p&gt;

&lt;p&gt;With Nginx, one master process can control multiple worker processes. The master maintains the worker processes, while the workers do the actual processing. Because Nginx is asynchronous, each request can be executed by the worker concurrently without blocking other requests. &lt;a href="https://kinsta.com/knowledgebase/what-is-nginx/#:~:text=Nginx%2C%20pronounced%20like%20%E2%80%9Cengine%2D,HTTP%20cache%2C%20and%20load%20balancer." rel="noopener noreferrer"&gt;more info&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  gunicorn:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Gunicorn&lt;/em&gt; is a stand-alone &lt;em&gt;WSGI&lt;/em&gt; web application server that offers a lot of functionality. It natively supports various frameworks with its adapters, making it an extremely easy to use drop-in replacement for many development servers that are used during development.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  supervisor:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;At some point you'll likely find yourself writing a script that needs to run all the time - a "long running script". These are scripts that shouldn't fail if there's an error or ones that should restart when the system reboots.&lt;/p&gt;

&lt;p&gt;To accomplish this, we need something to watch these scripts. Such tools are process watchers. They watch processes and restart them if they fail, and ensure they start on system boot.&lt;/p&gt;

&lt;p&gt;And &lt;em&gt;Supervisord&lt;/em&gt; is a simple and popular choice for doing these tasks and monitoring our process&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  collectstatic
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Using the &lt;em&gt;collectstatic&lt;/em&gt; command, Django looks for all static files in your apps and collects them into a single location that can easily be served in production, i.e. the STATIC_ROOT. In our case, we are telling Django that when we run &lt;code&gt;python3 manage.py collectstatic&lt;/code&gt;, gather all static files into a folder called &lt;code&gt;static&lt;/code&gt; in our project root directory. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it for this article. I hope you found it useful. Feel free to drop your comments below and like the post.&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>nginx</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
