4. Django 1.8 Server Build - CentOS 7 hosted on VPS - Install and Configure MariaDB Database & PHP
In this chapter, we'll install MariaDB Database and PHP.
MariaDB is a relational database management system (RDBMS) and ships by default in CentOS 7. MariaDB is a popular component in contemporary web development tool-chains, and is used to store data for many popular applications, including Wordpress and Drupal.
MariaDB is a community-developed fork of the MySQL relational database management system, and it was forked due to concerns over its acquisition by Oracle.
$ sudo yum install mariadb-server mariadb ... Dependency Installed: libaio.x86_64 0:0.3.109-12.el7 perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 perl-DBD-MySQL.x86_64 0:4.023-5.el7 perl-DBI.x86_64 0:1.627-4.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7 perl-IO-Compress.noarch 0:2.061-2.el7 perl-Net-Daemon.noarch 0:0.48-5.el7 perl-PlRPC.noarch 0:0.2020-14.el7 Complete!
CentOS 7 provides version 10.1.1 of MariaDB. Before we can use MariaDB some configuration is required. If we want to run MariaDB by default when the system boots, execute the following command:
$ sudo /bin/systemctl enable mariadb.service ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
Now you can start the MariaDB daemon (mariadb) with the following command:
$ sudo /bin/systemctl start mariadb.service
At this point, MariaDB should be ready to configure and run. While we shouldn't need to change the configuration file, note that it is located at /etc/my.cnf for future reference. The default values should be fine for a Linode 1GB, but if we decide to adjust them we should first make a backup copy:
$ cp /etc/my.cnf ~/my.cnf.backup
After installing MariaDB, it's recommended that we run mysql_secure_installation, a program that helps secure MariaDB. While running mysql_secure_installation, we will be presented with the opportunity to change the MariaDB root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases and reload privileges. It is recommended that we answer yes to these options. Run the following command to execute the program:
$ sudo mysql_secure_installation /bin/mysql_secure_installation: line 379: find_mysql_client: command not found NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] Y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] Y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] Y ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] Y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] Y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
Next, we'll create a database and grant our users permissions to use databases. First, log in to MariaDB. Enter MariaDB's root password, and we'll be presented with a prompt where we can issue SQL statements to interact with the database.
$ mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 15 Server version: 5.5.41-MariaDB MariaDB Server Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]>
To check the info about our db:
MariaDB [(none)]> show variables where variable_name like "%dir"; +---------------------------+----------------------------+ | Variable_name | Value | +---------------------------+----------------------------+ | aria_sync_log_dir | NEWFILE | | basedir | /usr | | character_sets_dir | /usr/share/mysql/charsets/ | | datadir | /var/lib/mysql/ | | innodb_data_home_dir | | | innodb_log_group_home_dir | ./ | | lc_messages_dir | | | plugin_dir | /usr/lib64/mysql/plugin/ | | slave_load_tmpdir | /var/tmp | | tmpdir | /var/tmp | +---------------------------+----------------------------+ 10 rows in set (0.00 sec)
To exit, ^D:
MariaDB [(none)]> Bye
With Apache and MariaDB installed we are now ready to move on to installing PHP to provide scripting support for our web pages.
PHP makes it possible to produce dynamic and interactive pages using our own scripts and web development frameworks. Furthermore, many popular web applications like WordPress are written in PHP. If we want to be able to develop our websites using PHP, we must first install it.
$ sudo yum install php php-pear php-mysql ... Installed: php.x86_64 0:5.4.16-23.el7_0.3 php-mysql.x86_64 0:5.4.16-23.el7_0.3 php-pear.noarch 1:1.9.4-21.el7 ...
Since need support for MariaDB or MySQL in PHP, we installed the php5-mysql package as well.
Once PHP5 is installed we'll need to tune the configuration file located in /etc/php.ini to enable more descriptive errors, logging, and better performance. Make sure that the following values are set, and relevant lines are uncommented (comments are lines beginning with a semi-colon [;]):
$ sudo vim /etc/php.ini error_log = /var/log/php/error.log
Then, we need to create the log directory for PHP and give the Apache user ownership:
$ sudo mkdir /var/log/php $ sudo chown apache /var/log/php
After making changes to PHP, restart Apache by issuing the following command:
$ sudo /bin/systemctl restart httpd
We now have a fully functioning LAMP stack on our Linode!
Let's go to our DocumentRoot and create a php page.
tree-subdomain.png$ cd /srv/www/sfvue.com/public_html/ $ sudo vim info.php <?php phpinfo() ?>
Then, restart Apache:
$ sudo apachectl restart
Open a browser and check if the php page is working.
info-php.pngphpMyAdmin is a Web based MySQL browser written in php.
Installing phpMyAdmin requires access to the Fedora Projects EPEL Repositories. So, let's get the latest Extra Packages for Enterprise Linux (EPEL):
$ sudo rpm -iUvh http://mirror.pnl.gov/epel/7/x86_64/e/epel-release-7-5.noarch.rpm Retrieving http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm warning: /var/tmp/rpm-tmp.PHe9ED: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:epel-release-7-5 ################################# [100%]
Make sure our package repositories and installed programs are up to date by issuing the following commands:
$ sudo yum update
Now we want to install phpMyAdmin:
$ sudo yum install phpMyAdmin ... Installed: phpMyAdmin.noarch 0:4.4.9-1.el7 ...
By default, phpMyAdmin is configured to only permit access from the localhost (127.0.0.1).
If we want to use it remotely (as we'll discuss this in later part of this section), we do not have to do anything. Just make sure php is installed.
Actually, at the bottom of Apache server's main conf file (/etc/httpd/conf/httpd.conf) has the following line that lets Apache include /etc/httpd/conf.d/phpMyAdmin.conf:
IncludeOptional conf.d/*.conf
So, all we need to do is to restart our server:
$ sudo systemctl restart httpd
But if we want to have an access to phpMyAdmin from a specific ip, then we will need to edit the configuration file and add the IP Address of our home or work computer. There are two 127.0.0.1s for each version. The 127.0.0.1 means it allows only local access, and we may want to change it so that we can access it from home or from work.
Let's see if it works:
$ sudo apachectl restart
As mentioned earlier, we probably use phpMyAdmin from our local machine. To do that, we want to do forwarding our local 8080 port to remote 80 port (local port forwarding / outgoing ssh tunnel - Linux Secure Shell (SSH) IV : SSH Tunnel (Local Port Forwarding)):
$ ssh -L 8080:localhost:80 -l sfvue 45.79.90.218
where the -l flag is for user name (in our case, it's sfvue), and the last argument is the host-ip (45.79.90.218).
Let's try to access it via sfvue.com:
phpmyadmin-forbidden.pngphpMyAdmin allows us to access only through localhost by default. Allowing phpMyAdmin to anyone other than localhost should be considered dangerous unless properly secured by SSL.
So, we may want to access localhost using local port 8080 which is forwarded to remote localhost port 80:
local-8080-phpmyadmin.pngAs we can see, we can only login to phpMyAdmin only if we SSH login with port forwarding!
LoginPhpAdmin.png
Also note that the port 8080 may have already taken, for example, by Jenkins. If that's the case, we may want to use other port for our port forward setting like this:
$ ssh -4 -L 9001:localhost:80 -l sfvue 45.79.90.218 sfvue@45.79.90.218's password: Last login: Tue Dec 15 11:22:20 2015 from 108-239-135-40.lightspeed.frokca.sbcglobal.net
where -4 to use ipv4 rather than ipv6.
Now, on our local machine, we type http://localhost:9001/phpmyadmin into Browser:
phpmyadmin9001.pngphpmyadmin-9001-after-login.png
Continue : 5. Install and Configure Django
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization
Django 1.8
Introduction - Install Django and Project Setup
Creating and Activating Models
Hello World A - urls & views
Hello World B - templates
Hello World C - url dispatcher
Hello World D - Models and SQLite Database
MVC - Hello World
Hello World on a Shared Host A
Hello World on a Shared Host B
Hello World - Admin Site Setup
virtualenv
Creating test project on virtualenv
Test project's settings.py
Creating Blog app and setting up models
Blog app - syncdb A
Blog app - syncdb B
Blog app - views and urls
Blog app - templates
Blog app - class based templates
Image upload sample code - local host
Authentication on Shared Host using FastCGI
User Registration on Shared Host A
User Registration with a Customized Form on Shared Host B
Blogs on Shared Host
Serving Django app with uWSGI and Nginx
Image upload sample code - shared host
Managing (Deploying) Static files (CSS, Images, Javascript) on Shared Host
Forum application on a Shared Host
Django Python Social Auth : Getting App ID (OAuth2) - Facebook, Twitter, and Google
Django: Python social auth, Facebook, Twitter, and Google Auth
Django: Python social auth, Facebook, Twitter, and Google Auth with Static files
...
Django 1.8 hosted on Linode VPS ==>
1. Setup CentOS 7 hosted on VPS
1B. Setup CentOS 7 hosted on VPS (multi-domain hosting setup) - Name server and Zone File settings (from GoDaddy to Linode)
2. ssh login and firewall
3. Apache Install
4. Install and Configure MariaDB Database server & PHP
5. Install and Configure Django
6. Model
7. Model 2 : populate tables, list_display, and search_fields
8. Model 3 (using shell)
9. Views (templates and css)
10. Views 2 (home page and more templates)
11. TinyMCE
12. TinyMCE 2
13. ImageField/FileField : Serving image/video files uploaded by a user
14. User Authentication 1 (register & forms)
15. User Authentication 2 (login / logout)
16. User Authentication 3 (password reset) - Sent from Email (gmail) setup etc.
17. User Authentication 4 (User profile & @login_required decorator)
18. User Authentication 5 (Facebook login)
19. User Authentication 6 (Google login)
20. User Authentication 7 (Twitter login)
21. User Authentication 8 (Facebook/Google/Twitter login buttons)
22. Facebook open graph API timeline fan page custom tab 1
23. Facebook Open Graph API Timeline Fan Page Custom Tab 2 (SSL certificate setup)
24. Facebook open graph API timeline fan page custom tab 3 (Django side - urls.py, settings.py, and views.py)
...
A sample production site Django 1.8.7: sfvue.com / einsteinish.com ==>
A sample production app (sfvue.com) with virtualenv and Apache
2. Upgrading to Django 1.8.7 sfvue.com site sample with virtualenv and Apache
(*) Django 1.8.7 einsteinish.com site - errors and fixes
Django 1.8.12 pytune.com site - local with Apache mod_wsgi
Django 1.8.12 pytune.com site - local with Nginx and uWSGI
Django 1.8.12 pytune.com site - deploy to AWS with Nginx and uWSGI
Django Haystack with Elasticsearch and Postgres
Django Compatibility Cheat Sheet
Python tutorial
Python Home
Introduction
Running Python Programs (os, sys, import)
Modules and IDLE (Import, Reload, exec)
Object Types - Numbers, Strings, and None
Strings - Escape Sequence, Raw String, and Slicing
Strings - Methods
Formatting Strings - expressions and method calls
Files and os.path
Traversing directories recursively
Subprocess Module
Regular Expressions with Python
Regular Expressions Cheat Sheet
Object Types - Lists
Object Types - Dictionaries and Tuples
Functions def, *args, **kargs
Functions lambda
Built-in Functions
map, filter, and reduce
Decorators
List Comprehension
Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism
Hashing (Hash tables and hashlib)
Dictionary Comprehension with zip
The yield keyword
Generator Functions and Expressions
generator.send() method
Iterators
Classes and Instances (__init__, __call__, etc.)
if__name__ == '__main__'
argparse
Exceptions
@static method vs class method
Private attributes and private methods
bits, bytes, bitstring, and constBitStream
json.dump(s) and json.load(s)
Python Object Serialization - pickle and json
Python Object Serialization - yaml and json
Priority queue and heap queue data structure
Graph data structure
Dijkstra's shortest path algorithm
Prim's spanning tree algorithm
Closure
Functional programming in Python
Remote running a local file using ssh
SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table
SQLite 3 - B. Selecting, updating and deleting data
MongoDB with PyMongo I - Installing MongoDB ...
Python HTTP Web Services - urllib, httplib2
Web scraping with Selenium for checking domain availability
REST API : Http Requests for Humans with Flask
Blog app with Tornado
Multithreading ...
Python Network Programming I - Basic Server / Client : A Basics
Python Network Programming I - Basic Server / Client : B File Transfer
Python Network Programming II - Chat Server / Client
Python Network Programming III - Echo Server using socketserver network framework
Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn
Python Coding Questions I
Python Coding Questions II
Python Coding Questions III
Python Coding Questions IV
Python Coding Questions V
Python Coding Questions VI
Python Coding Questions VII
Python Coding Questions VIII
Python Coding Questions IX
Python Coding Questions X
Image processing with Python image library Pillow
Python and C++ with SIP
PyDev with Eclipse
Matplotlib
Redis with Python
NumPy array basics A
NumPy Matrix and Linear Algebra
Pandas with NumPy and Matplotlib
Celluar Automata
Batch gradient descent algorithm
Longest Common Substring Algorithm
Python Unit Test - TDD using unittest.TestCase class
Simple tool - Google page ranking by keywords
Google App Hello World
Google App webapp2 and WSGI
Uploading Google App Hello World
Python 2 vs Python 3
virtualenv and virtualenvwrapper
Uploading a big file to AWS S3 using boto module
Scheduled stopping and starting an AWS instance
Cloudera CDH5 - Scheduled stopping and starting services
Removing Cloud Files - Rackspace API with curl and subprocess
Checking if a process is running/hanging and stop/run a scheduled task on Windows
Apache Spark 1.3 with PySpark (Spark Python API) Shell
Apache Spark 1.2 Streaming
bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...
Flask app with Apache WSGI on Ubuntu14/CentOS7 ...
Selenium WebDriver
Fabric - streamlining the use of SSH for application deployment
Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App
Neural Networks with backpropagation for XOR using one hidden layer
NLP - NLTK (Natural Language Toolkit) ...
RabbitMQ(Message broker server) and Celery(Task queue) ...
OpenCV3 and Matplotlib ...
Simple tool - Concatenating slides using FFmpeg ...
iPython - Signal Processing with NumPy
iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github
iPython and Jupyter Notebook with Embedded D3.js
Downloading YouTube videos using youtube-dl embedded with Python
Machine Learning : scikit-learn ...
Django 1.6/1.8 Web Framework ...