1. 安装Python3
由于CentOS 7自带的是Python 2.7,而且一大批内置的应用比如yum等等都依赖于python2.7,因此无法将它直接卸载。使用
yum -y install epel-release
or
yum -y install http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
yum -y install python34 python34-pip
pip3 install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple
可以安装Python3.4的环境,然后使用
$ python3
启动Python 3.4的环境。
2. 安装编译环境
CentOS 7使用yum默认安装的是Python 2.7的mod_wsgi,wsgi只能使用编译时指定的Python版本。如果想要使用Python3.5,就必须要重新编译mod_wsgi。首先安装编译环境:
yum -y groupinstall development
yum -y install zlib-devel openssl-devel sqlite-devel bzip2-devel python-devel openssl-devel libffi-devel openssl-perl libjpeg-turbo-devel zlib-devel giflib ncurses-devel gdbm-devel xz-devel tkinter readline-devel tk tk-devel
yum -y install httpd-devel python34-devel
yum remove mod_wsgi
下载mod_wsgi的源代码包,解压缩后编译:
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.9.tar.gz
tar zxvf ./4.5.9.tar.gz
cd mod_wsgi-4.5.9
./configure --with-python=/usr/bin/python3;make;make install
3. 配置Apache
首先配置Apache加载mod_wsgi,键入以下命令:
cd /etc/httpd/conf.modules.d/
vi 10-wsgi.conf
输入:
LoadModule wsgi_module modules/mod_wsgi.so
修改一下Apache配置程序:
vi /etc/httpd/conf/httpd.conf
在最后增加以下的配置信息:
# VirtualHost configuration
#
# Load config files in the "/etc/httpd/conf.vhost.d" directory,if any.
IncludeOptional conf.vhost.d/*.conf
然后我们然后让Apache加载我的Flask应用程序:
cd /etc/httpd/conf.vhost.d/
vi 00-ipapp.conf
<VirtualHost *>
ServerName 60.205.58.20
WSGIDaemonProcess ipapp user=devel group=apache threads=5 python-home=/home/devel/ipapp/venv
WSGIScriptAlias /ip /home/devel/ipapp/ip.wsgi
<Directory /home/devel/ipapp>
WSGIProcessGroup ipapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Require all granted
</Directory>
</VirtualHost>
最后,使用systemctl restart httpd
重启Apache搞定。