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:1sudo yum install epel-releaseWith EPEL enabled, we can install the components we need by typing:
1sudo yum install python-pip httpd mod_wsgiNow, we will install Mysql for database project. So, let’s download and install MySQL repository:
1 2 3wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm yum updateAfter repositories is OK, let’s install MySQL Server and Start it !
1 2yum install mysql-server systemctl start mysqldWith MySQL installed, we will configure using following command:
1mysql_secure_installationFollow 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 2useradd <username> passwd <username>After added user, let’s install git and download django project in user folder:
1 2 3yum 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
virtualenvandvirtualenvwrapperto manage each project virtual enviroment. So let’s install usingvirtualenvwrapper, because it will installvirtualenvautomatically:1pip install virtualenvwrapperTo bash CentOS recognize
virtualenvwrappercommands, let’s add 3 lines at ~/.bash_profile, so use commandnano ~/.bash_profileand add this content:1 2 3export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME/Devel source /usr/bin/virtualenvwrapper.shAfter that, we will create our virtualenv and install all requeriments of your project in txt:
1 2 3mkvirtualenv <nameproject> yum install gcc python-devel mysql-devel libevent-devel pip install -r requeriments.txtLet’s now create database for your project and import structure by Django migrations:
1 2 3 4 5mysql -u root -p<PASSWORD> create database <databasename>; exit; python manage.py migrateCreate a superuser Administration:
1python manage.py createsuperuserNow, we will collect static files, but you should certificate your
settings.pySTATIC_FILES is configured correctly:1python manage.py collectstaticSo, all it’s ok ! We just need to configure
Apachewithmod_wsgi. To do that, we will create a file configuration at/etc/httpd/conf.d/with any name:1nano /etc/httpd/conf.d/django.confAnd 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 2chmod 664 /home/<username>/<nameproject> sudo chown :www-data /home/<username>/<nameproject>To finish, we restart Apache !
1sudo service apache2 restart
