Fabio S. Takaki

Deploy Django Project in CentOS 7.2

Mon Apr 4, 2016

477 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:

    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:

    sudo yum install epel-release

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

    sudo yum install python-pip httpd mod_wsgi 

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

    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 !

    yum install mysql-server
    systemctl start mysqld

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


  • 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:

    useradd <username>
    passwd <username>

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

    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:

    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:

    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:

    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:

    mysql -u root -p<PASSWORD>
    	create database <databasename>;
    python manage.py migrate

  • Create a superuser Administration:

    python manage.py createsuperuser

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

    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:

    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.

    <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 /home/<username>/<nameproject>/<nameproject>>
            <Files wsgi.py>
                Require all granted
        WSGIDaemonProcess <nameproject> python-path=/home/<username>/<nameproject>:/<username>/.envs/<envproject>/lib/python2.7/site-packages
        WSGIProcessGroup <nameproject>
        WSGIScriptAlias / /home/<username>/<nameproject>/<nameproject>/wsgi.py

  • Now we will put some permissions and we finish !

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

  • To finish, we restart Apache !

    sudo service apache2 restart