Fabio S. Takaki

Deploy Django Project in CentOS 7.2

Mon Apr 4, 2016

529 Words

Today We will deploy my first project created with Django Framework 1.9 in CentOS 7.2. To do that, first I created a droplet with CentOS 7.2 installed. If you don’t know how to create a droplet in Digital Ocean, see this tutorial. Lastly, to deploy the project, we will use Apache with mod_wsgi.

Now, let’s start:

  • First update repositories:
1
yum update -y
  • To get pip, we’ll need to enable the EPEL repository, which as some additional packages. You can do that easily by typing:

    1
    
    sudo yum install epel-release

  • With EPEL enabled, we can install the components we need by typing:

    1
    
    sudo yum install python-pip httpd mod_wsgi 

  • Now, we will install Mysql for database project. So, let’s download and install MySQL repository:

    1
    2
    3
    
    wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
    sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
    yum update

  • After repositories is OK, let’s install MySQL Server and Start it !

    1
    2
    
    yum install mysql-server
    systemctl start mysqld

  • With MySQL installed, we will configure using following command:

    1
    
    mysql_secure_installation

  • Follow steps configuration. Before we download the project from git, let’s add user and put the project in user’s folder. In this way, you can add others projects in others users, using one server. Substitute in any user:

    1
    2
    
    useradd <username>
    passwd <username>

  • After added user, let’s install git and download django project in user folder:

    1
    2
    3
    
    yum install git -y
    cd /home/<username>
    git clone <URL-Repository>

  • Using git clone, you will download all files project into your user’s folder. Now, we use virtualenv and virtualenvwrapper to manage each project virtual enviroment. So let’s install using virtualenvwrapper, because it will install virtualenv automatically:

    1
    
    pip install virtualenvwrapper

  • To bash CentOS recognize virtualenvwrapper commands, let’s add 3 lines at ~/.bash_profile, so use command nano ~/.bash_profile and add this content:

    1
    2
    3
    
    export WORKON_HOME=$HOME/.virtualenvs
    export PROJECT_HOME=$HOME/Devel
    source /usr/bin/virtualenvwrapper.sh

  • After that, we will create our virtualenv and install all requeriments of your project in txt:

    1
    2
    3
    
    mkvirtualenv <nameproject>
    yum install gcc python-devel mysql-devel libevent-devel
    pip install -r requeriments.txt

  • Let’s now create database for your project and import structure by Django migrations:

    1
    2
    3
    4
    5
    
    mysql -u root -p<PASSWORD>
    	create database <databasename>;
    	exit;
    
    python manage.py migrate

  • Create a superuser Administration:

    1
    
    python manage.py createsuperuser

  • Now, we will collect static files, but you should certificate your settings.py STATIC_FILES is configured correctly:

    1
    
    python manage.py collectstatic

  • So, all it’s ok ! We just need to configure Apache with mod_wsgi. To do that, we will create a file configuration at /etc/httpd/conf.d/ with any name:

    1
    
    nano /etc/httpd/conf.d/django.conf

  • And put this content, replacing variables <> with your username or nameproject. The is the name of VirtualEnv you created to your project.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    <VirtualHost *:80>
        ServerName www.mydomain.com
        DocumentRoot /home/<username>/<nameproject>
    
        Alias /static /home/<username>/<nameproject>/static
        <Directory /home/<username>/<nameproject>/static>
            Require all granted
        </Directory>
    
        <Directory /home/<username>/<nameproject>/<nameproject>>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    
        WSGIDaemonProcess <nameproject> python-path=/home/<username>/<nameproject>:/<username>/.envs/<envproject>/lib/python2.7/site-packages
        WSGIProcessGroup <nameproject>
        WSGIScriptAlias / /home/<username>/<nameproject>/<nameproject>/wsgi.py
    
    </VirtualHost>

  • Now we will put some permissions and we finish !

    1
    2
    
    chmod 664 /home/<username>/<nameproject>
    sudo chown :www-data /home/<username>/<nameproject>

  • To finish, we restart Apache !

    1
    
    sudo service apache2 restart