5. Django 1.8 Server Build - CentOS 7 hosted on VPS - Install and Configure Django
- Django 1.8 Server Build - CentOS 7 hosted on VPS
- Django 1.8 Server Build - CentOS 7 hosted on VPS - ssh login and firewall
- Django 1.8 Server Build - CentOS 7 hosted on VPS - Apache Install
- Django 1.8 Server Build - CentOS 7 hosted on VPS - Install and Configure MariaDB Database server & PHP
In this chapter, we'll install and configure Django.
mod_wsgi is an Apache module which can host any Python WSGI application, including Django. Django will work with any version of Apache which supports mod_wsgi.
The purpose of mod_wsgi is to implement a simple to use Apache module which can host any Python application which supports the Python WSGI interface (How to use Django with Apache and mod_wsgi).
Let's check current Python version:
$ python Python 2.7.5 (default, Jun 17 2014, 18:11:42) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
The WSGI specification provides a standard and efficient method for dynamic web applications to communicate with web servers. The recommended way to serve Python applications using Apache is to use mod_wsgi module.
It is not installed by default with neither Python nor Apache, so we have to install an additional package.
mod_wsgi provides a method for simply deploying WSGI applications with Apache. WSGI is used to deploy applications written with frameworks and tools like Django, Web.py, Chery.py, and Flask.
$ sudo yum install mod_wsgi ... Installed: mod_wsgi.x86_64 0:3.4-12.el7_0 ...
$ sudo yum install python-pip ... Installed: python-pip.noarch 0:1.5.6-5.el7
Let's install Dajngo:
$ sudo pip install django Downloading/unpacking django Downloading Django-1.8.2-py2.py3-none-any.whl (6.2MB): 6.2MB downloaded Installing collected packages: django Successfully installed django Cleaning up...
[sfvue@sf www]$ sudo mkdir django [sfvue@sf www]$ ls django pics.sfvue.com sfvue.com [sfvue@sf www]$ sudo chown -R sfvue django [sfvue@sf www]$ cd django [sfvue@sf django]$ pwd /srv/www/django [sfvue@sf django]$ django-admin.py startproject djangotest
We may want to create some folders for djangotest.sfvue.com:
[sfvue@sf www]$ pwd /srv/www [sfvue@sf www]$ sudo mkdir -p djangotest.sfvue.com/logs [sfvue@sf www]$ sudo mkdir -p djangotest.sfvue.com/public_html
Note : as we can see from the picture above, we have a project(djangotest) created in an additional subdirectory (djangotest). If we don't want to create a subdirectory, we can add '.' after ' django-admin.py startproject':
[sfvue@sf django]$ django-admin.py startproject myProject .
So, instead of this,
we get this:
[sfvue@sf conf.d]$ pwd /etc/httpd/conf.d [sfvue@sf conf.d]$ sudo cp vhost.conf djangotest.sfvue.com [sfvue@sf conf.d]$ sudo mv djangotest.sfvue.com djangotest.sfvue.com.conf
Let's modify the file, /etc/httpd/conf.d/djangotest.sfvue.com.conf:
[sfvue@sf conf.d]$ sudo vim djangotest.sfvue.com.conf <VirtualHost *:80> ServerAdmin webmaster@sfvue.com ServerName djangotest.sfvue.com DocumentRoot /srv/www/djangotest.sfvue.com/public_html/ ErrorLog /srv/www/djangotest.sfvue.com/logs/error.log CustomLog /srv/www/djangotest.sfvue.com/logs/access.log combined WSGIScriptAlias / /srv/www/django/djangotest/djangotest/wsgi.py <Directory "/srv/www/djangotest.sfvue.com/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost> WSGIPythonPath /srv/www/django/djangotest/ <Directory "/srv/www/django/djangotest/djangotest"> <Files wsgi.py> Require all granted </Files> </Directory>
The WSGIScriptAlias directive tells Apache that for this VirtualHost, all requests below / should be handled by the WSGI script specified. In other words, it tells Apache and mod_wsgi where to find WSGI configuration. The wsgi.py supplied by Django contains the barebone default configuration for WSGI for serving Django application that works just fine.
The first bit in the WSGIScriptAlias line is the base URL path we want to serve our application at (/ indicates the root url), and the second is the location of a "WSGI file" on our system, usually inside of our project package (djangotest in this example).
The WSGIPythonPath line ensures that our project package is available for import on the Python path. In other words, it ensures that import djangotest works.
The last <Directory> piece just ensures that Apache can access our wsgi.py file.
When we have configured our Apache VirtualHost, issue the following command to restart the web server:
$ sudo apachectl restart
Open up a browser with an url: http://djangotest.sfvue.com/:
Then, we can go to admin page:
Let's setup a database.
First, we need to create a db and a user, djangotestdb and djangotestuser, respectively. We will use phpmyadmin via port forwarding from local 8080 port to remote localhost 80:
k@laptop:~$ ssh -L 8080:localhost:80 -l sfvue 45.79.90.218
Click "Go", and it actually does this:
CREATE USER 'djangotestuser'@'localhost' IDENTIFIED BY '***';GRANT ALL PRIVILEGES ON *.* TO 'djangotestuser'@'localhost' IDENTIFIED BY '***' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
Edit /srv/www/django/djangotest/djangotest/settings.py:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'djangotestdb', 'USER': 'djangotestuser', 'PASSWORD': 'K3zHqeArA9S5v3DR', } }
Now we want to restart apache:
$ sudo apachectl restart
Let's install MySQL-python for managy.py can do its work between Python and mySQL:
$ sudo yum install MySQL-python Installed: MySQL-python.x86_64 0:1.2.3-11.el7
$ pwd /srv/www/django/djangotest [sfvue@sf djangotest]$ ls djangotest manage.py $ python manage.py makemigrations No changes detected
$ python manage.py syncdb /usr/lib64/python2.7/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9 warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning) Operations to perform: Synchronize unmigrated apps: staticfiles, messages Apply all migrations: admin, contenttypes, auth, sessions Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying sessions.0001_initial... OK You have installed Django's auth system, and don't have any superusers defined. Would you like to create one now? (yes/no): yes Username (leave blank to use 'sfvue'): Email address: k.hong@aol.com Password: Password (again): Superuser created successfully.
Now, we'll be able to login Django Admin:
As we can see, all of our static contents are broken. Let's work on it.
We need to edit settings.py:
... STATIC_URL = '/static/' STATIC_ROOT = '/srv/www/djangotest.sfvue.com/public_html/static'
Also, we may want to create the directory:
$ sudo mkdir -p /srv/www/djangotest.sfvue.com/public_html/static
Another thing: setting Apache alias:
Alias /static/ /srv/www/djangotest.sfvue.com/public_html/static/
in /etc/httpd/conf.d/djangotest.sfvue.com.conf, and it looks loke this:
<VirtualHost *:80> ServerAdmin webmaster@sfvue.com ServerName djangotest.sfvue.com DocumentRoot /srv/www/djangotest.sfvue.com/public_html/ ErrorLog /srv/www/djangotest.sfvue.com/logs/error.log CustomLog /srv/www/djangotest.sfvue.com/logs/access.log combined WSGIScriptAlias / /srv/www/django/djangotest/djangotest/wsgi.py Alias /static/ /srv/www/djangotest.sfvue.com/public_html/static/ <Directory "/srv/www/djangotest.sfvue.com/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost> WSGIPythonPath /srv/www/django/djangotest/ <Directory "/srv/www/django/djangotest/djangotest"> <Files wsgi.py> Require all granted </Files> </Directory>
Restart Apache:
$ sudo apachectl restart
Run the following to collect the static files into STATIC_ROOT:
$ sudo python manage.py collectstatic You have requested to collect static files at the destination location as specified in your settings: /srv/www/djangotest.sfvue.com/public_html/static This will overwrite existing files! Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes Copying '/usr/lib64/python2.7/site-packages/django/contrib/admin/static/admin/css/base.css' Copying '/usr/lib64/python2.7/site-packages/django/contrib/admin/static/admin/css/rtl.css' ... Copying '/usr/lib64/python2.7/site-packages/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js' Copying '/usr/lib64/python2.7/site-packages/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js' 62 static files copied to '/srv/www/djangotest.sfvue.com/public_html/static'.
Now, if we want to refresh the Django Admin page:
We got good looking page!
Let's create an app called sfvue under django folder:
[sfvue@sf django]$ pwd /srv/www/django [sfvue@sf django]$ django-admin.py startproject sfvue .
Here is the configuration (/etc/httpd/conf.d/sfvue.com.conf):
<VirtualHost *:80> ServerAdmin webmaster@sfvue.com ServerName sfvue.com ServerAlias www.sfvue.com DocumentRoot /srv/www/sfvue.com/public_html/ ErrorLog /srv/www/sfvue.com/logs/error.log CustomLog /srv/www/sfvue.com/logs/access.log combined WSGIScriptAlias / /srv/www/django/sfvue/wsgi.py Alias /static/ /srv/www/sfvue.com/public_html/static/ <Directory "/srv/www/sfvue.com/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> </VirtualHost> WSGIPythonPath /srv/www/django/ <Directory "/srv/www/django/sfvue"> <Files wsgi.py> Require all granted </Files> </Directory>
Continue: 6. Model
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization