<?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>Forem: Sayam Kumar</title>
    <description>The latest articles on Forem by Sayam Kumar (@sayam753).</description>
    <link>https://forem.com/sayam753</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%2F320199%2F5f46ed37-5699-4901-ba46-44519b256bfa.jpeg</url>
      <title>Forem: Sayam Kumar</title>
      <link>https://forem.com/sayam753</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sayam753"/>
    <language>en</language>
    <item>
      <title>GSoC'20 with NumFOCUS</title>
      <dc:creator>Sayam Kumar</dc:creator>
      <pubDate>Sat, 15 Aug 2020 15:05:03 +0000</pubDate>
      <link>https://forem.com/sayam753/gsoc-20-with-numfocus-8hd</link>
      <guid>https://forem.com/sayam753/gsoc-20-with-numfocus-8hd</guid>
      <description>&lt;p&gt;Original post - &lt;a href="https://www.codingpaths.com/gsoc/gsoc-2020-with-numfocus/"&gt;https://www.codingpaths.com/gsoc/gsoc-2020-with-numfocus/&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  GSoC'20 with NumFOCUS
&lt;/h1&gt;

&lt;p&gt;I am super excited to say that I have been selected as a Google Summer of Code student by NumFOCUS for PyMC4. I would like to thank my mentors &lt;a href="https://github.com/twiecki"&gt;Thomas Wiecki&lt;/a&gt; and &lt;a href="https://github.com/ferrine"&gt;Maxim Kochurov&lt;/a&gt; and the entire NumFOCUS community for giving this opportunity.&lt;/p&gt;

&lt;p&gt;My project is about adding &lt;code&gt;Variational Inference Interface&lt;/code&gt; to &lt;a href="https://github.com/pymc-devs/pymc4"&gt;PyMC4&lt;/a&gt;. Variational Inference scales better over larger datasets as compared to the traditional MCMC algorithms. First, I had plans to implement OPVI&lt;sup id="fnref1"&gt;1&lt;/sup&gt; framework as done in PyMC3 this summer. But as corrected by my mentor &lt;a href="https://github.com/ferrine"&gt;Maxim Kochurov&lt;/a&gt;, it would have taken extra time and more debugging because of the difficulty to deal with &lt;code&gt;symbolic graph manipulations&lt;/code&gt; in &lt;a href="https://www.tensorflow.org/"&gt;Tensorflow&lt;/a&gt;. Now, the whole plan is to implement two Variational Inference Algorithms - Mean Field ADVI&lt;sup id="fnref2"&gt;2&lt;/sup&gt; and Full Rank ADVI&lt;sup id="fnref2"&gt;2&lt;/sup&gt; in PyMC4. Mean Field ADVI posits a Spherical Gaussian family and Full Rank ADVI posits a Multivariate Gaussian family to minimize &lt;a href="https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence"&gt;KL divergence&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;All in all, I look forward to a great summer.&lt;/p&gt;

&lt;p&gt;Thank you for reading. 😄&lt;br&gt;
Sayam &lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://arxiv.org/abs/1610.09033"&gt;Operator Variational Inference&lt;/a&gt; Rajesh Ranganath, Jaan Altosaar, Dustin Tran, David M. Blei (2016) ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://arxiv.org/abs/1603.00788"&gt;Automatic Differentiation Variational Inference&lt;/a&gt; Alp Kucukelbir, Dustin Tran, Rajesh Ranganath, Andrew Gelman, David M. Blei (2016). ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>opensource</category>
      <category>github</category>
      <category>bayesian</category>
    </item>
    <item>
      <title>Deploy Django</title>
      <dc:creator>Sayam Kumar</dc:creator>
      <pubDate>Sat, 15 Aug 2020 14:52:37 +0000</pubDate>
      <link>https://forem.com/sayam753/deploy-django-4k0d</link>
      <guid>https://forem.com/sayam753/deploy-django-4k0d</guid>
      <description>&lt;p&gt;Original post - &lt;a href="https://www.codingpaths.com/django/deploy-django/"&gt;https://www.codingpaths.com/django/deploy-django/&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Deploy Django
&lt;/h1&gt;

&lt;p&gt;In this blog, we will learn how to deploy Django Application with uWSGI and nginx on CentOS’7. Django is the most popular python based backend framework with the aim of rapid web development. Before installing anything, I recommend to read my &lt;a href="https://dev.to/sayam753/initial-server-setup-django-deploy-5bmp"&gt;previous post&lt;/a&gt; where I have discussed about initial server setup with ssh keys.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Installing the prerequisites
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Installing python3.6
&lt;/h3&gt;

&lt;p&gt;First install the latest packages from EPEL and RPM. EPEL(Extra Packages for Enterprise Linux) is an open source repository that contains the latest packages for Red Hat Linux distributions. RPM is also an open source package management system from Red Hat. After all this, lets install python3 –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; epel-release
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; https://centos7.iuscommunity.org/ius-release.rpm
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum update
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; python36u python36u-libs python36u-devel python36u-pip python-devel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Upgrading pip and installing virtualenv
&lt;/h3&gt;

&lt;p&gt;Pip is the most popular python package installer. Virtual environments are used for separating the different versions of any package for different projects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pip3.6 &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--upgrade&lt;/span&gt; pip
&lt;span class="nb"&gt;sudo &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;virtualenv virtualenvwrapper
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring the shell
&lt;/h3&gt;

&lt;p&gt;We will use Env directory to hold all our virtual environments. This can be configured in &lt;code&gt;.bashrc&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"export WORKON_HOME=~/Env"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"source /usr/bin/virtualenvwrapper.sh"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, open &lt;code&gt;/usr/bin/virtualenvwrapper.sh&lt;/code&gt; with either vim or nano. Find the line –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and replace python with python3.6 as –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VIRTUALENVWRAPPER_PYTHON="$(command \which python3.6)"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, lets reflect these changes –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Configuring Django project
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Creating virtual environments
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;mkvirtualenv env_1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The environment env_1 gets automatically activated. The same can be verified by –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;which pip
&lt;span class="c"&gt;# Output: ~/Env/env_1/bin/pip&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Copying Django project from local to remote
&lt;/h3&gt;

&lt;p&gt;Since, we have activated our virtual env, now we will copy our django project to remote server using scp. If you have uploaded it on github, just install git via yum and then git clone the project. But before doing this, lets grab all the requirements of the project. If your project already contains a requirements file, then you can skip this part. “cd” into your project’s directory and after activating virtual env in your local machine, use the following command to list out requirements in requirements.txt file in your local terminal-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;pip freeze &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, to copy the project, use the following command in your local terminal. Do remember to put your ip and user_name configured in previous post. Write the complete path of your django project from root in local machine. This will copy the project in the home directory of the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;scp &lt;span class="nt"&gt;-r&lt;/span&gt; /path/to/project user_name@your_ip_here:~
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, connect to your server and activate the virtual env.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh user_name@your_ip_here
workon env_1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let’s install all the requirements for the project. Use the path to requirements.txt file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; /path/to/requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing MySQL from rpm
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;rpm &lt;span class="nt"&gt;-ivh&lt;/span&gt; https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Check if Mysql repo has been enabled –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;yum repolist all | &lt;span class="nb"&gt;grep &lt;/span&gt;mysql | &lt;span class="nb"&gt;grep &lt;/span&gt;enabled
&lt;span class="c"&gt;# Output : enabled&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Install and enable mysql –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="nb"&gt;install &lt;/span&gt;mysql-community-server
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start mysqld
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;mysqld
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status mysqld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Copy the mysql temporary root password from the command given below and paste this while secure installation of mysql. Change the root password and hit Enter for default actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; /var/log/mysqld.log | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'temporary password'&lt;/span&gt;
mysql_secure_installation
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have successfully installed mysql and now, we need a database to run our project. First, open the mysql interface and enter the root password –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;mysql &lt;span class="nt"&gt;-u&lt;/span&gt; root &lt;span class="nt"&gt;-p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, in mysql, create a database –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;mysql&amp;gt; CREATE DATABASE first_db&lt;span class="p"&gt;;&lt;/span&gt;
mysql&amp;gt; SHOW DATABASES&lt;span class="p"&gt;;&lt;/span&gt;
mysql&amp;gt; &lt;span class="nb"&gt;exit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After this, we need to install a client to communicate with mysql –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; mysql-connector-python.x86_64 mysql-community-devel.x86_64 mysql-cluster-community-client.x86_64 mysql-shell.x86_64 mysql-router.x86_64 gcc
pip &lt;span class="nb"&gt;install &lt;/span&gt;mysqlclient &lt;span class="c"&gt;# inside the virtual environment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Changing settings.py file
&lt;/h3&gt;

&lt;p&gt;With everything installed, let’s change some settings for the project –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano ~/project_name/project_name/settings.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add the following line to the last of the file. As we will be using nginx to deploy the application, this line tells django to place our static files in ‘static’ directory. This helps nginx to easily serve these static files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;STATIC_ROOT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE_DIR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"static/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Do not forget to change the default database configurations to –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;'default'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'ENGINE'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'django.db.backends.mysql'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;'NAME'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'first_db'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;'USER'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'root'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;'PASSWORD'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'your-root-password'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;'HOST'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;'PORT'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And also add your ip in the allowed hosts –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;ALLOWED_HOSTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'your_ip_here'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Opening port 8000
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;firewall-cmd &lt;span class="nt"&gt;--permanent&lt;/span&gt; &lt;span class="nt"&gt;--add-service&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;http
&lt;span class="nb"&gt;sudo &lt;/span&gt;firewall-cmd &lt;span class="nt"&gt;--permanent&lt;/span&gt; &lt;span class="nt"&gt;--add-port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;8000/tcp
&lt;span class="nb"&gt;sudo &lt;/span&gt;firewall-cmd &lt;span class="nt"&gt;--complete-reload&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;firewall-cmd &lt;span class="nt"&gt;--list-all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Running the application
&lt;/h3&gt;

&lt;p&gt;First, flush out the initial migrations and delete the sqlite database. “cd” into your project’s directory and use the following commands –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;find &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-path&lt;/span&gt; &lt;span class="s2"&gt;"*/migrations/*.py"&lt;/span&gt; &lt;span class="nt"&gt;-not&lt;/span&gt; &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s2"&gt;"__init__.py"&lt;/span&gt; &lt;span class="nt"&gt;-delete&lt;/span&gt;
find &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nt"&gt;-path&lt;/span&gt; &lt;span class="s2"&gt;"*/migrations/*.pyc"&lt;/span&gt;  &lt;span class="nt"&gt;-delete&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; db.sqlite3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Run the migrations to sync up with database –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py collectstatic
python manage.py makemigrations
python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, finally we can run the server and see the application accessible globally –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py runserver 0.0.0.0:8000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Go to the web browser and enter your_ip:8000 to access the django application.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Setting up uWSGI and nginx
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Configuring uWSGI globally
&lt;/h3&gt;

&lt;p&gt;Store all the configuration files to /etc/uwsgi/sites. You should use your project name for all configurations –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;uwsgi
&lt;span class="nb"&gt;sudo mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /etc/uwsgi/sites
&lt;span class="nb"&gt;cd&lt;/span&gt; /etc/uwsgi/sites
&lt;span class="nb"&gt;sudo &lt;/span&gt;nano project_name.ini
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add the following lines to the .ini file. Do remember to use your project and user name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;uwsgi]
project &lt;span class="o"&gt;=&lt;/span&gt; project_name
username &lt;span class="o"&gt;=&lt;/span&gt; user_name
base &lt;span class="o"&gt;=&lt;/span&gt; /home/%&lt;span class="o"&gt;(&lt;/span&gt;username&lt;span class="o"&gt;)&lt;/span&gt;

chdir &lt;span class="o"&gt;=&lt;/span&gt; %&lt;span class="o"&gt;(&lt;/span&gt;base&lt;span class="o"&gt;)&lt;/span&gt;/%&lt;span class="o"&gt;(&lt;/span&gt;project&lt;span class="o"&gt;)&lt;/span&gt;
home &lt;span class="o"&gt;=&lt;/span&gt; %&lt;span class="o"&gt;(&lt;/span&gt;base&lt;span class="o"&gt;)&lt;/span&gt;/Env/env_1
module &lt;span class="o"&gt;=&lt;/span&gt; %&lt;span class="o"&gt;(&lt;/span&gt;project&lt;span class="o"&gt;)&lt;/span&gt;.wsgi:application

master &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true
&lt;/span&gt;processes &lt;span class="o"&gt;=&lt;/span&gt; 5

uid &lt;span class="o"&gt;=&lt;/span&gt; %&lt;span class="o"&gt;(&lt;/span&gt;username&lt;span class="o"&gt;)&lt;/span&gt;
socket &lt;span class="o"&gt;=&lt;/span&gt; /run/uwsgi/%&lt;span class="o"&gt;(&lt;/span&gt;project&lt;span class="o"&gt;)&lt;/span&gt;.sock
chown-socket &lt;span class="o"&gt;=&lt;/span&gt; %&lt;span class="o"&gt;(&lt;/span&gt;username&lt;span class="o"&gt;)&lt;/span&gt;:nginx
chmod-socket &lt;span class="o"&gt;=&lt;/span&gt; 660
vacuum &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ctrl+x to exit and press y to save the changes. Base and home contain the full path for the home directory and virtual environment respectively. We have created a master process for loading our app server. Here, we have used Unix Socket. This socket uses uWSGI protocol which helps nginx to reverse proxy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/systemd/system/uwsgi.service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add the following lines. Do remember to use your user name in ExecStartPre of Service section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;Unit]
&lt;span class="nv"&gt;Description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;uWSGI Emperor service

&lt;span class="o"&gt;[&lt;/span&gt;Service]
&lt;span class="nv"&gt;ExecStartPre&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/bin/bash &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'mkdir -p /run/uwsgi; chown user_name:nginx /run/uwsgi'&lt;/span&gt;
&lt;span class="nv"&gt;ExecStart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/bin/uwsgi &lt;span class="nt"&gt;--emperor&lt;/span&gt; /etc/uwsgi/sites
&lt;span class="nv"&gt;Restart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;always
&lt;span class="nv"&gt;KillSignal&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;SIGQUIT
&lt;span class="nv"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;notify
&lt;span class="nv"&gt;NotifyAccess&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;all

&lt;span class="o"&gt;[&lt;/span&gt;Install]
&lt;span class="nv"&gt;WantedBy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;multi-user.target
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;[Unit] section describes our service. [Service] section manages various applications. [Install] section ties up multi-user system state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring Nginx
&lt;/h3&gt;

&lt;p&gt;Installing Nginx -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;yum &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/nginx/nginx.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add the following lines. Do remember to use your user name and project name in root and uwsgi_pass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;server &lt;span class="o"&gt;{&lt;/span&gt;
    listen 8000&lt;span class="p"&gt;;&lt;/span&gt;
    server_name localhost&lt;span class="p"&gt;;&lt;/span&gt;

    location &lt;span class="o"&gt;=&lt;/span&gt; favicon.ico &lt;span class="o"&gt;{&lt;/span&gt; access_log off&lt;span class="p"&gt;;&lt;/span&gt; log_not_found off&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    location /static/ &lt;span class="o"&gt;{&lt;/span&gt;
        root /home/user_name/project_name&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    location / &lt;span class="o"&gt;{&lt;/span&gt;
        include uwsgi_params&lt;span class="p"&gt;;&lt;/span&gt;
        uwsgi_pass unix:/run/uwsgi/project_name.sock&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Above, we have set up a server block, with an open port to listen from. We have also specified the static file location and passed all the traffic to unix socket. Make sure the syntax of nginx file is correct and change permissions of the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nginx &lt;span class="nt"&gt;-t&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;usermod &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nt"&gt;-G&lt;/span&gt; user_name nginx
&lt;span class="nb"&gt;chmod &lt;/span&gt;710 /home/user_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Start and enable the nginx and uwsgi.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start nginx
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start uwsgi
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;nginx
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;uwsgi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can directly access the django application from the ip with an open port.&lt;br&gt;
Thanks for reading!&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Initial Server Setup - Django Deploy</title>
      <dc:creator>Sayam Kumar</dc:creator>
      <pubDate>Sat, 15 Aug 2020 14:41:50 +0000</pubDate>
      <link>https://forem.com/sayam753/initial-server-setup-django-deploy-5bmp</link>
      <guid>https://forem.com/sayam753/initial-server-setup-django-deploy-5bmp</guid>
      <description>&lt;p&gt;Original Post - &lt;a href="https://www.codingpaths.com/django/initial-server-setup/"&gt;https://www.codingpaths.com/django/initial-server-setup/&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Initial Server Setup
&lt;/h1&gt;

&lt;p&gt;In this blog, we will be going through initial server setup on Centos7. This is generally recommended before diving straight into production. Centos is an open source Linux distribution under RHEL(Red Hat Enterprise Linux). The reason why Centos is preferred over Ubuntu is because of its stability. Its updates can take about more than 7-8 years to come.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Configuring Centos 7
&lt;/h2&gt;

&lt;p&gt;After purchasing a server, login into it using the ssh command in the terminal. Remember to use your public ip and the password given by the administrator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh root@your_ip_here
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you are getting a warning like “LC_CTYPE: cannot change locale (UTF-8): No such file or directory”, then enter the following command in your local terminal by logging out. The LC variables determine the language of encoding the characters. So, we need to export this variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;logout
export &lt;/span&gt;&lt;span class="nv"&gt;LC_ALL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"en_US.UTF-8"&lt;/span&gt;
ssh root@your_ip_here
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Updating centos
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;yum update
&lt;span class="nb"&gt;sudo &lt;/span&gt;yum upgrade
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting up hostname
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;hostnamectl set-hostname centos-server
&lt;span class="nb"&gt;hostname&lt;/span&gt; &lt;span class="c"&gt;# to check the hostname&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting up hostname in the hosts file
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/hosts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Add your ip followed by tab and then type centos-server which is the hostname. Then hit CTRL+x to exit and enter to save the changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Adding a new user
&lt;/h2&gt;

&lt;p&gt;Root has the most privileges in the OS. It can be destructive to operate the server under root user. To limit the scope, we will be creating a new user. In future, if any need arises, we will change the permissions for this user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;adduser user_name
passwd user_name &lt;span class="c"&gt;#setting up password for new user&lt;/span&gt;
gpasswd &lt;span class="nt"&gt;-a&lt;/span&gt; user_name wheel &lt;span class="c"&gt;#adding sudo privileges&lt;/span&gt;
&lt;span class="nb"&gt;logout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Securing the server
&lt;/h2&gt;

&lt;p&gt;There are bots all around trying to find vulnerabilities in the servers. Till now, we have used password based authentication which is highly exploitable. These bots try brute force attacks to enter our server. So to fix this, we will disable password based authentication and setup ssh keys. These ssh keys will be stored in the local machine and the server. After this whenever we try to ssh into our server, it will analyse the keys and give access. So, setting up ssh keys for authentication in the local machine in home directory. Hit enter for default actions –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; rsa &lt;span class="c"&gt;#generating keys&lt;/span&gt;
ssh-copy-id user_name@your_ip_here &lt;span class="c"&gt;#coping the keys to the server&lt;/span&gt;
&lt;span class="nb"&gt;logout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can ssh login without password for the new user –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh user_name@your_ip_here
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Change configuration in &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt;, thereby making our server more secure –&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PermitRootLogin no
PasswordAuthentication no
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/ssh/sshd_config
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart sshd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Up next – &lt;a href="https://dev.to/sayam753/deploy-django-4k0d"&gt;Deploy Django Applications on Centos&lt;/a&gt;&lt;br&gt;
Thanks for reading!&lt;/p&gt;

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