Flask blog app with MongoDB via Apache WSGI : Part 3 (Production on CentOS 7)
Continued from Flask blog app with MongoDB via Apache WSGI on Ubuntu 14.
In this page, we'll deploy our Flask blog app (memonimo.com) using Apache on CentOS 7 VPS.
So far, we've worked on our local Ubuntu 14 machine.
Now, it's time to play with CentOS 7 VPS.
We need to install MongoDB since our blog is using it.
First, let's configure the package management system (yum). Create a file ,/etc/yum.repos.d/mongodb-org-2.6.repo, and put in the following configuration information for the MongoDB 2.6 repository:
[mongodb-org-2.6] name=MongoDB 2.6 Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1
Next, we need to install the MongoDB packages and associated tools.
$ sudo yum install -y mongodb-org
We can start the mongod process by issuing the following command:
$ sudo service mongod start Starting mongod (via systemctl): [ OK ]
We can verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading:
MongoDB starting : pid=16297 port=27017 dbpath=/var/lib/mongo 64-bit host=sf ... [initandlisten] db version v2.6.12
Note that <port> is the port configured in /etc/mongod.conf, 27017 by default.
We can optionally ensure that MongoDB will start following a system reboot by issuing the following command:
$ sudo chkconfig mongod on
We can restat/stop the service:
$ sudo service mongod restart $ sudo service mongod stop
Clone repository : FlaskBlog.
Modify config.py:
DEBUG = False
/var/www/flask/memonimo/flaskapp.wsgi:
#!/usr/bin/python import sys import logging logging.basicConfig(stream=sys.stderr) # Dev # sys.path.insert(0,"/var/www/FlaskApp/") # from Blog import app as application # Production sys.path.insert(0,"/var/www/flask") from memonimo import app as application application.secret_key = 'memonimo secret key'
Modify /var/www/flask/memonimo/config.py:
... app = Flask('memonimo') ...
Let's setup Virtualenv:
$ sudo virtualenv venv $ source venv/bin/activate (venv)$ sudo pip install -r requirements.txt ... Installing collected packages: Werkzeug, MarkupSafe, Jinja2, itsdangerous, flask, markdown, flask-markdown, pymongo, gunicorn Running setup.py install for MarkupSafe Running setup.py install for itsdangerous Running setup.py install for flask Running setup.py install for markdown Running setup.py install for flask-markdown Running setup.py install for pymongo
Check with Flask server (Werkzeug):
(venv)$ sudo python __init__.py * Running on http://0.0.0.0:8000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger pin code: 100-555-666
Create a log directory:
$ sudo mkdir -p /var/www/memonimo.com/logs/
The following is the Apache configuration (/etc/httpd/sites-available/memonimo.com.conf):
<VirtualHost *:80> ServerAlias memonimo.com ServerName www.memonimo.com ServerAdmin admin@memonimo.com WSGIDaemonProcess memonimo user=apache group=apache threads=5 python-path=/var/www/flask/memonimo:/var/www/flask/memonimo/venv/lib/python2.7/site-packages WSGIScriptAlias / /var/www/flask/memonimo/flaskapp.wsgi <Directory /var/www/flask/memonimo/> WSGIProcessGroup memonimo WSGIApplicationGroup %{GLOBAL} Order allow,deny Allow from all </Directory> Alias /static /var/www/flask/memonimo/static <Directory /var/www/flask/memonimo/static/> Order allow,deny Allow from all </Directory> ErrorLog /var/www/memonimo.com/logs/error.log LogLevel warn CustomLog /var/www/memonimo.com/logs/access.log combined </VirtualHost>
Let's create a symlink:
$ sudo ln -s /etc/httpd/sites-available/memonimo.com.conf /etc/httpd/sites-enabled/memonimo.com.conf $ ls -la /etc/httpd/sites-enabled ... lrwxrwxrwx 1 root root 44 May 5 12:00 memonimo.com.conf -> /etc/httpd/sites-available/memonimo.com.conf ...
Restart the server:
$ sudo apachectl restart
At memonimo.com on the browser:
IOError: [Errno 13] Permission denied: '/usr/share/httpd/app.log'
This can be fixed by modifying the permission for /usr/share/httpd:
$ sudo chown -R apache:apache httpd
At the first run we get the following install page for setting up our blog:
Hit "Sumit":
At "Add New":
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization