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:
|
|
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
andvirtualenvwrapper
to manage each project virtual enviroment. So let’s install usingvirtualenvwrapper
, because it will installvirtualenv
automatically:1
pip install virtualenvwrapper
To bash CentOS recognize
virtualenvwrapper
commands, let’s add 3 lines at ~/.bash_profile, so use commandnano ~/.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
withmod_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 envproject is the name of VirtualEnv you created to your project.
1<VirtualHost *:80>
2 ServerName www.mydomain.com
3 DocumentRoot /home/<username>/<nameproject>
4
5 Alias /static /home/<username>/<nameproject>/static
6 <Directory /home/<username>/<nameproject>/static>
7 Require all granted
8 </Directory>
9
10 <Directory /home/<username>/<nameproject>/<nameproject>>
11 <Files wsgi.py>
12 Require all granted
13 </Files>
14 </Directory>
15
16 WSGIDaemonProcess <nameproject> python-path=/home/<username>/<nameproject>:/<username>/.envs/<envproject>/lib/python2.7/site-packages
17 WSGIProcessGroup <nameproject>
18 WSGIScriptAlias / /home/<username>/<nameproject>/<nameproject>/wsgi.py
19
20</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