<?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: King Tobi</title>
    <description>The latest articles on DEV Community by King Tobi (@austinogiza).</description>
    <link>https://dev.to/austinogiza</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%2F198737%2F9d016e0a-160c-4c76-b289-43fe74653dbd.jpg</url>
      <title>DEV Community: King Tobi</title>
      <link>https://dev.to/austinogiza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/austinogiza"/>
    <language>en</language>
    <item>
      <title>Deploying react and django with nginx and guincorn</title>
      <dc:creator>King Tobi</dc:creator>
      <pubDate>Fri, 21 Jan 2022 22:07:23 +0000</pubDate>
      <link>https://dev.to/austinogiza/deploying-react-and-django-with-nginx-and-guincorn-4h9l</link>
      <guid>https://dev.to/austinogiza/deploying-react-and-django-with-nginx-and-guincorn-4h9l</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1642801058743%2FDuuxMbZhc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1642801058743%2FDuuxMbZhc.png" alt="react djang.png"&gt;&lt;/a&gt;&lt;br&gt;
Deploying react and django with nginx and guincorn&lt;br&gt;
Deploying react with django isn't straight forward but I am here to assist you with that. We will be going through how to deploy both react and django as the main and subdomain using the same server/ubuntu instance. What this means is you will deploy your frontend and backend separately on two different subdomains such as the frontend will be accessed via &lt;a href="http://www.example.com" rel="noopener noreferrer"&gt;www.example.com&lt;/a&gt; while the django backend api will be accessed via &lt;a href="http://www.api.example.com" rel="noopener noreferrer"&gt;www.api.example.com&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Prerequisite knowledge&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Experience with ubuntu&lt;/li&gt;
&lt;li&gt;Experience with GitHub&lt;/li&gt;
&lt;li&gt;Linux based system with ubuntu 16 and above
&lt;strong&gt;Tools&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;GitHub&lt;/li&gt;
&lt;li&gt;AWS/DigitalOcean/Cloudcone i.e a cloud provider&lt;/li&gt;
&lt;li&gt;Domain
**First login to your server using putty and your root access
**Use terminal and run:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apt update &amp;amp;&amp;amp; apt upgrade -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Set hostname for server. I used test-server. You can use whatever you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hostnamectl set-hostname test-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create a limited user and give sudo privileges&lt;/strong&gt;&lt;br&gt;
run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adduser USERNAME .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;br&gt;
 &lt;br&gt;
Enter password and skip through the rest of the questions.&lt;br&gt;
then run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adduser USERNAME sudo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;logout as root by typing ** exit **and log in with your username: ssh username@IP&lt;br&gt;
Pull both frontend and backend code from github using&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;git clone &lt;br&gt;
Installing some important packages&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;** Set Up Nginx Server Blocks**&lt;br&gt;
Creating the First Server Block File For React Frontend&lt;br&gt;
Install NodeJS and npm&lt;br&gt;
Use Current Release&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt-get install nodejs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned above, we will create our first server block config file by copying over the default file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, open the new file you created in your text editor with sudo privileges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ignore the commented lines, and look at the code similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
 listen 80 ;
 listen [::]:80;
root /home/username/react-frontend/build;
 index index.html index.htm;
server_name your_IP domain.com www.domain.com;
location / {
 try_files $uri /index.html =404;
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then Enable the file by linking to the sites-enabled dir&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test NGINX config&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart Nginx Server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create the Second Server Block File which will be for django&lt;/strong&gt;&lt;br&gt;
First Install some dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;run sudo apt install virtualenv ufw
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change to your virtualenv&lt;br&gt;
run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;virtualenv venv -p python3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Back out to the directory that has manage.py&lt;br&gt;
Run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py collectstatic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Install Gunicorn&lt;br&gt;
run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install gunicorn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Check to see if gunicorn can host your django project. Change **DjangoProject **to whatever your project is called&lt;br&gt;
run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gunicorn - bind 0.0.0.0:8000 DjangoProject.wsgi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Create gunicorn systemd file&lt;br&gt;
run&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/systemd/system/gunicorn.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Paste the following and be sure to update your project name, path and username accordingly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=username
Group=www-data
WorkingDirectory=/home/username/DjangoProject
ExecStart=/home/username/venv/bin/gunicorn - access-logfile - - workers 3 - bind unix:/home/username/DjangoProject/DjangoProject.sock DjangoProject.wsgi:application
[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step : Run the following commands to enable gunicorn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl status gunicorn
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have our initial server block configuration, we can use that as a basis for our second file. Copy it over to create a new file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/test.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the new file with sudo privileges in your editor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/test.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste the following and be sure update your own IP, username, path and project name&lt;br&gt;
This covers http, https will be covered below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {
 listen 80;
 server_name IP;
 location = /favicon.ico { access_log off; log_not_found off; }
 location /static/ {
 root /home/username/DjangoProject;
 }
 location / {
 include proxy_params;
 proxy_pass http://unix:/home/username/DjangoProject/DjangoProject.sock;
 }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Link and test nginx config&lt;br&gt;
Link:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/django-project/etc/nginx/sites-enabled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Test:&lt;br&gt;
&lt;br&gt;
 &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Reload Nginx and Gunicorn&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart gunicorn
sudo systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to avoid a possible hash bucket memory problem that can arise from adding additional server names, we will also adjust a single value within our **/etc/nginx/nginx.conf **file. Open the file now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/nginx/nginx.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the file, find the** server_names_hash_bucket_size &lt;strong&gt;directive. Remove the **#&lt;/strong&gt; symbol to uncomment the line:&lt;br&gt;
&lt;strong&gt;/etc/nginx/nginx.conf&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http {
 . . .
 server_names_hash_bucket_size 64;
 . . .
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now, let's activate SSL and bind the domains to their respective proxies&lt;/strong&gt;&lt;br&gt;
Install Certbot and it's Nginx plugin with apt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install certbot python3-certbot-nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Certbot is now ready to use, but in order for it to automatically configure SSL for Nginx, we need to verify some of Nginx's configuration.&lt;br&gt;
Obtain an SSL Certificate&lt;br&gt;
First we obtain SSL certificate for the react url&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo certbot - nginx -d example.com -d www.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs certbot with the - nginx plugin, using -d to specify the domain names we'd like the certificate to be valid for.&lt;br&gt;
If that's successful, certbot will ask how you'd like to configure your HTTPS settings.&lt;br&gt;
Output&lt;br&gt;
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1–2] then [enter] (press 'c' to cancel):
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Select your choice then hit ENTER. The configuration will be updated, and Nginx will reload to pick up the new settings. certbot will wrap up with a message telling you the process was successful and where your certificates are stored:&lt;br&gt;
Let's finish by testing the renewal process.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Verify Certbot Auto-Renewal&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You can query the status of the timer with systemctl:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl status certbot.timer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test the renewal process, you can do a dry run with certbot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo certbot renew - dry-run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see no errors, you're all set. When necessary, Certbot will renew your certificates and reload Nginx to pick up the changes. &lt;br&gt;
If the automated renewal process ever fails, Let's Encrypt will send a message to the email you specified, warning you when your certificate is about to expire.&lt;br&gt;
Now, repeat the same SSL process for the django domain&lt;br&gt;
Credits:&lt;br&gt;
Credits to&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://dev.to/xarala221"&gt;Ousseynou Diop&lt;/a&gt; for the react deployment guide&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/satssehgal" rel="noopener noreferrer"&gt;Satssehgal&lt;/a&gt;for the django deployment guide&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.digitalocean.com/community" rel="noopener noreferrer"&gt;Digitalocean&lt;/a&gt;for all their articles&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
  </channel>
</rss>
