当前位置: > CentOS > CentOS服务器 > 环境配置 >

CentOS 6.8 下安装部署flask应用记录

时间:2018-04-27 11:41来源:linux.it.net.cn 作者:IT
一、安装Apache
1.1直接运行:
yum install httpd -y
 
1.2将Apache设置为开机启动及启动、停止命令:
 
 
二、安装Python 3.6.5:
Centos 6.8上自带的python是2.6.6,而且yum在线更新会提示没有可用的更新。因此需要手动下载3.6.5版本进行安装。
注意:由于linux上很多的软件都是依赖python的,所以旧版的python千万不能卸载掉,例如yum就是依赖python的。只能安装一个新版本的python,让两者共存,然后在实际使用过程中再使用新版本的python。
 
2.1安装之前的准备工作:
先查询以下有没有安装以下模块:
rpm -q openssl-devel
rpm -q gcc
以下是安装方法(安装编译环境):
yum -y install gcc(编译源码包的时候用到)
yum -y install openssl-devel(安装pip的时候用到
 
2.2下载Python 3.6.5安装包:
https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
 
2.3下载好了之后解压:
tar -xzvf Python-3.5.3.tgz
 
2.4解压好了之后开始配置安装路径:
首先新建一个文件夹用于存放python3
mkdir /usr/bin/python3.6.5    此为ezgame实际使用目录
 
然后进入解压的python-3.5文件夹执行下列操作(注意加上参数--enable-shared):
./configure --enable-shared --prefix=/usr/bin/python3.6.5
 
2.5开始编译
make
如果编译过程中报错,就不用继续下一步了,请执行 make clean 清除掉编译的文件
 
2.6编译好之后开始安装编译好的文件
make install
 
2.7由于系统中默认有一个2.6版本的python,所以我们要把新版本的python做一个软连接放在 /usr/bin中
先删掉原来的python:
rm -y /usr/bin/python
或者备份下:
$ mv /usr/bin/python /usr/bin/python.bak
注意:python2.6千万不要删,删了yum就无法恢复使用了!
 
再创建一个软连接:
ln -s /var/local/python3/bin/python3.5 /usr/bin/python
 
这时输入python -V 就可以看到python的版本已经是3.5.3版本
然后再创建一个python3的命令,这样用python和python3都可以直接进入python环境:
ln -s /usr/bin/python /usr/bin/python3
 
同时为了pip可以方便的安装python模块,也要把pip创建软连接到/usr/bin中:
ln -s /var/local/python3/bin/pip3 /usr/bin/pip
 
好了,以上就完成了所有的安装,可以顺便更新一下pip:
pip install --upgrade pip
 
2.8.由于yum是依赖于老版本的Python,所以安装了新版之后,yum就无法使用了,解决方法:
vim /usr/bin/yum
进入到yum中修改yum的注释,
将 #!/usr/bin/python
修改为#!/usr/bin/python2.6
OK保存退出。
完成!
 
创建python虚拟环境
Python自带虚拟环境
# if是Debian/Ubuntu, 需先apt-get install python3-venv
 
#先在终端进入想要创建的环境环境的目录
python -m venv pyenv #venv模块只有python3才有,2木有
 
#激活虚拟环境
 
pyenv/Scripts/activate.bat #windows
source pyenv/bin/activate #linux
 
 
#退出虚拟环境
 
pyenn/Scripts/deactivate.bat #windows
pyenv/bin/deactivate #linux
 
 
需求文件requirements.txt的创建及使用
python项目中必须包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号。以便新环境部署。

在虚拟环境中使用pip生成:

(venv) $ pip freeze >requirements.txt
 
安装或升级包后,最好更新这个文件。
 
在新的虚拟环境中可以运行如下命令生成一个新的副本:
(venv) $ pip install -r requirements.txt
 
 
 
三、安装mod_wsgi
yum install mod_wsgi -y
 
四、按照网络教程,配置apache 的 http.conf 文件以及wsgi文件:
 
4.1 http.conf配置大概有两种写法,稍有差别,应该都可以使用,以下是其一:
在<Directory "/var/www/html">     这个节点后面,注意是与这个节点并列,不是这个节点的里面。增加如下代码:
WSGIScriptAlias /test /var/www/flasktest/flask.wsgi
 
<Directory /var/www/flasktest>
  Order allow,deny
  Allow from all
</Directory>
其中 /test 是在浏览器访问时的后缀
 
http.conf 的另一种配置方法是:
<VirtualHost *:80>
    ServerName bnu1.crawlapp.com
    WSGIScriptAlias /flask /var/www/flasktest/flask.wsgi
<Directory /var/www/flasktest>
  Order allow,deny
  Allow from all
</Directory>
</VirtualHost>
 
 
 
为了测试,可以先只配置 wsgi 文件,看看 apache+wsgi是否能正常访问,此时的wsgi配置可以如下:
 
def application(environ,start_response):
   status='200 OK'
   output='Hello wsgi!'
   response_headers=[('Content-type','text/plain'),('Content-Length',str(len(output)))]
   start_response(status,response_headers)
   return[output]
 
配置完上述两个文件后,可以重启下httpd 服务,然后访问测试,如果能返回“Hello wsgi!”则说明配置正确。此时可以引入flask应用:
 
1、修改wsgi文件,完整内容如下(这里的views就是当前目录的views.py文件,看到网上很多文章还要import sys,再append当前目录,其实如果在同一目录下的话就不需要了):
 
from views import app as application
 
2、在wsgi同级目录下增加 views.py文件,其完整内容如下:
 
from flask import Flask
 
app = Flask(__name__)
 
@app.route('/')
def index():
   return "Hello Flask"
 
if __name__ == '__main__':
    app.run(host="0.0.0.0")
 
 
配置完 Apache + wsgi + flask,访问的时候可能会出现如下错误:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.

Apache/2.2.15 (CentOS) Server at 124.206.192.133 Port 8000
 
此时可以看看Apache的日志文件,可能的原因是 Apache 默认还是使用 2.6.6版本的python,另外mod_wsgi的版本可能也要升级到与python3匹配。
至此发现centos自带的apache、wsgi版本都较低,需要升级.....!!!!!
 
升级过程:
1、去apache官网下载最新的 apache、apr-1.6.3.tar.gz、apr-util-1.6.1.tar.gz
 
2、删除旧版的apache(2.2.15):
yum -y remove httpd
yum -y remove apr-1.3.9-5.el6_2.x86_64
 
3、在开始编译安装之前,需要先安装相关的开发包组以及其他的一些依赖包,这些包有下面的这些:
# 安装开发包组
[root@localhost src]# yum groupinstall "Development tools"
 
# 安装相应的devel包
[root@localhost src]# yum install openssl-devel pcre-devel expat-devel
 
当然,如果一开始不知道缺少哪些包的话,可以先进行编译安装,然后根据错误提示再将缺少的包安装上,这样也是可以的。
 
4、因为httpd 是依赖与apr的,所以应该先编译apr 以及apr-util,然后再安装 httpd。
我们的源码文件存放的路径是/usr/local/src,我们编译安装的路径是 /usr/local/apr,/usr/local/apr-util,/usr/local/httpd24.
 
# 编译 apr
[root@localhost apr-1.6.2]# ./configure --prefix=/usr/local/apr
 
# 安装 apr
[root@localhost apr-1.6.2]# make -j 2 && make install
 
# 编译apr-util
[root@localhost apr-util-1.6.0]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
 
#安装apr-util
[root@localhost apr-util-1.6.0]# make -j 2 && make install
 
安装apr-util过程中可能报错,如果跟expat相关,可以先安装expat库(成功后继续安装apr-util):
yum install expat-devel
 
 
5、首先给httpd 创建默认的系统用户,由于之前已经安装过apache,通过命令查看apache用户和apache组已经有了,此处不用再建用户和组:
 
cat /etc/password
cat /etc/group
 
6、编译安装httpd ,可以自己指定需要开启的功能列表,然后指定程序安装的路径,我们这里指定的安装路径还是 /usr/local/httpd24
# --prefix  指定程序需要安装的路径
# --enable  指定启用的模块
# --with-apr 指定apr的路径
# --with-apr-util  指定apr-util 的路径
[root@localhost httpd-2.4.27]# ./configure --prefix=/usr/local/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
 
# 安装 httpd
[root@localhost httpd-2.4.27]# make -j 2 && make install
 
ezgame在执行make安装的时候有报错:
configure: error: pcre-config for libpcre not found. PCRE is required and availa
解决办法:
yum  install pcre-devel  -y
 
接下来报了另一个错:
/libapr-1.la -lrt -lcrypt -lpthread -lcrypt
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ParserCreat
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_GetErrorCod
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetUserData
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ErrorString
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetEntityDe
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ParserFree'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetElementH
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_StopParser'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_Parse'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetCharacte
collect2: ld returned 1 exit status
make[2]: *** [htpasswd] 错误 1
make[2]: Leaving directory `/usr/local/src/httpd-2.4.33/support'
make[1]: *** [all-recursive] 错误 1
make[1]: Leaving directory `/usr/local/src/httpd-2.4.33/support'
make: *** [all-recursive] 错误 1
 
找了半天资料,大概有三种说法:
一是把apr和apr-util拷贝到 解压后的 httpd/srclib路径下,然后重新编译、安装,测试依然报错。
 
另外一种方法(没有测试,因为在/usr/lib/ 下没找到libm.a、libm.so等):
1、apache的编译选项上增加 --libdir=/usr/lib64 参数,如:
./configure --prefix=/home/apache2 --enable-modules=most --enable-mods-shared=max --enable-proxy --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer --enable-rewrite --libdir=/usr/lib64
 
2、重新链接lib库
mv /usr/lib/libm.a /usr/lib/libm.a.bak
mv /usr/lib/libm.so /usr/lib/libm.so.bak
mv /usr/lib/libexpat.so /usr/lib/libexpat.so.bak
ln -s /usr/lib64/libm.a /usr/lib/libm.a
ln -s /usr/lib64/libm.so /usr/lib/libm.so
ln -s /usr/lib64/libexpat.so /usr/lib/libexpat.so
 
第三种说法(ezgame测试有效):
终于在弯弯的一个论坛找到了答案。原来是apr apr-util 用了最新的1.6版本(之前1.6版本在centos7编译安装apache没有问题)
降低了版本,用回1.5版本正常解决
apr-1.5.2.tar.gz apr-util-1.5.2.tar.gz
下载链接: http://archive.apache.org/dist/apr/
 
 
升级完Apache后,配置与之前的版本稍有不同,重新配置:
 
1、启用rewrite:
LoadModule rewrite_module modules/mod_rewrite.so
 
2、启用Virtual hosts
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
 
3、配置 Virtual hosts,在/usr/local/httpd24/conf/extra 路径下的 httpd-vhosts.conf文件里:
 
<VirtualHost *:80>
    ServerName bnu1.crawlapp.com
    WSGIScriptAlias /flask /var/www/flasktest/flask.wsgi
<Directory /var/www/flasktest>
  Order allow,deny
  Allow from all
</Directory>
</VirtualHost>
 
4、添加:
LoadModule wsgi_module modules/mod_wsgi.so
 
5、Apache2.4默认拒绝所有连接,修改http.conf文件:
<Directory />
    AllowOverride none
    Require all denied
</Directory>
 
apache安装完之后用 service httpd start 的方式无法启动服务,会提示无法识别的服务,此时需要手动注册httpd服务:
1、
cp /usr/local/httpd24/bin/apachectl /etc/rc.d/init.d/httpd
 
2、
ln -s /etc/init.d/httpd /etc/rc.d/rc5.d/S85httpd
3、编辑 /etc/rc.d/init.d/httpd,添加以下注释信息:
# chkconfig: 345 85 15
# description: Activates/Deactivates Apache Web Server
4、执行chkconfig --add httpd
 
接下来编译安装mod_wsgi4.6.4版本:
1、
./configure --with-apxs=/usr/local/httpd24/bin/apxs --with-python=/usr/bin/python3.6.5/bin/python3.6
 
2、
make
执行make后最终报错:
src/server/wsgi_python.h:24:20: fatal error: Python.h: No such file or directory
...........
...........
make: *** [src/server/mod_wsgi.la] 错误 1
最后做了下面两步操作后make成功:
安装之后的python3开发库在/usr/bin/python3.6.5/lib目录下,系统是无法找到的,启用python3.6.5开发库可以配置系统库加载目录,命令如下:
echo "/usr/bin/python3.6.5/lib"  >> /etc/ld.so.conf.d/python3.6.5.conf
ldconfig
./configure --with-apxs=/usr/local/httpd24/bin/apxs --with-python=/usr/bin/python3.6.5/bin/python3.6
 ##一定要是python3.6,不能是python3,一开始写的是python3导致make失败



(责任编辑:IT)
------分隔线----------------------------