Centos6.10 + apache + python3 + Django 1.11 + mod_wsgi web服务部署
python3 + django 1.11 已安装好,不做过多的说明
1、安装包存放目录
/root/Downloads/python_django_apache
apr-1.6.5.tar.bz2
apr-util-1.6.1.tar.bz2
httpd-2.4.37.tar.gz
mod_wsgi-4.6.5.tar.gz
pcre-8.39.tar.bz2
setuptools-28.3.0.tar.gz
2、解压安装
1.安装setuptools
wget https://pypi.python.org/packages/6b/dd/a7de8caeeffab76bacf56972b3f090c12e0ae6932245abbce706690a6436/setuptools-28.3.0.tar.gz
tar xzf setuptools-28.3.0.tar.gz
cd setuptools-28.3.0/
python setup.py install
2.安装pip
wget https://pypi.python.org/packages/e7/a8/7556133689add8d1a54c0b14aeff0acb03c64707ce100ecd53934da1aa13/pip-8.1.2.tar.gz
tar xzf pip-8.1.2.tar.gz
cd pip-8.1.2/
python setup.py install
3.安装apr
wget http://apache.fayea.com/apr/apr-1.5.2.tar.bz2
tar xjf apr-1.6.3.tar.bz2
cd apr-1.6.3/
./configure --prefix=/usr/local/apr make && make install
4.安装apr-util
wget http://apache.fayea.com/apr/apr-util-1.5.4.tar.bz2
tar xjf apr-util-1.6.1.tar.bz2
cd apr-util-1.6.1/
./configure --with-apr=/usr/local/apr/
make && make install
cd ../
5.安装pcre
yum -y install gcc-c++
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.bz2
tar xjf pcre-8.39.tar.bz2
cd pcre-8.39/
./configure --prefix=/usr/local/pcre
make && make install
6.安装https(apache)
# yum -y install perl
wget http://apache.fayea.com/httpd/httpd-2.4.37.tar.bz2
tar xjf httpd-2.4.37.tar.bz2
cd httpd-2.4.37/
./configure --prefix=/usr/local/httpd --with-pcre=/usr/local/pcre
make && make install
注:在/etc/profile 文件末尾加 export PATH=/usr/local/httpd/bin:$PATH 配置环境变量
. /etc/profile 或 source /etc/profile 使配置文件生效
7.安装mod_wsgi
用pip install mod_wsgi 命令直接也可安装
wget https://pypi.python.org/packages/28/a7/de0dd1f4fae5b2ace921042071ae8563ce47dac475b332e288bc1d773e8d/mod_wsgi-4.5.7.tar.gz
tar xzf mod_wsgi-4.5.7.tar.gz
cd mod_wsgi-4.5.7/
./configure --with-apxs=/usr/local/httpd/bin/apxs --with-python=/root/miniconda3/bin/python
注: 这里make编译时遇到坑 正常make编译后apache配置文件调用不到mod_wsgi.so
可用 LD_RUN_PATH=/usr/local/lib make 命令 然后make install
make && make install
chmod 755 /usr/local/httpd/modules/mod_wsgi.so(安装好之后应该是自动就将该文件改成755 的)
软件安装到此结束,下面期待配置文件
3、配置文件
1,apache配置文件
PATH vim /usr/local/httpd/conf/httpd.conf
52 行 改成项目发布端口
52 Listen 8866
很多LoadModule 后面加
LoadModule wsgi_module "/usr/local/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so" 或 LoadModule wsgi_module modules/mod_wsgi.so
mod_wsgi-express install-module 命令 可以输出
LoadModule wsgi_module "/usr/local/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
SGIPythonHome /root/miniconda3
193 行 去掉注释 改成要发布的ip或域名
193 ServerName 113.200.105.35:8866
476 行 去掉注释
475 # Virtual hosts
476 Include conf/extra/httpd-vhosts.conf
WSGIPythonHome /root/miniconda3 最后加pyhtonhome
2,配置httpd-vhosts.conf项目文件
PATH vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
注释以前所有的 配置自己的项目路径
<VirtualHost *:8866>
DocumentRoot "/root/opinion/OpinionMonitor/com/fanjiedata/opinion/src/workflow/publish/public_sentiment_web/public_sentiment_web"
Alias /static/ /root/opinion/OpinionMonitor/com/fanjiedata/opinion/src/workflow/publish/public_sentiment_web/static/
<Directory "/root/opinion/OpinionMonitor/com/fanjiedata/opinion/src/workflow/publish/public_sentiment_web/static">
Options FollowSymlinks
AllowOverride none
Require all granted
</Directory>
WSGIScriptAlias / "/root/opinion/OpinionMonitor/com/fanjiedata/opinion/src/workflow/publish/public_sentiment_web/public_sentiment_web/wsgi.py"
WSGIPassAuthorization On # 转发Authorization请求头
<Directory "/root/opinion/OpinionMonitor/com/fanjiedata/opinion/src/workflow/publish/public_sentiment_web/public_sentiment_web">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
3,配置django项目中的 wsgi.py文件
vim /root/opinion/OpinionMonitor/com/fanjiedata/opinion/src/workflow/publish/public_sentiment_web/public_sentiment_web/wsgi.py
import sys
sys.path.append('/root/opinion/OpinionMonitor/com/fanjiedata/opinion/src/workflow/publish/public_sentiment_web)
加上这两句
4,配置环境变量
vim /etc/profile
export PATH=/usr/local/httpd/bin:$PYTHONHOME:$PATH
export PYTHONPATH=/root/miniconda3:/root/miniconda3/:/root/miniconda3/lib/python3.6/site-packages
export PYTHONHOME="/root/miniconda3"
加上这三句 . /etc/profile 或 source /etc/profile 使配置文件生效
配置文件就告一段落
4、项目启动
apachectl start 启动
apachectl stop 停止
apachectl restart 重启
5、项目部署总结,遇到特殊问题的解决方案
第一次在centos中部署apache + django,中间踩过很多的坑,终于完成了工作任务
1,安装包中的坑,不能调用到 mod_wsgi.so
2,配置文件中的坑,访问权限问题
[wsgi:warn] [pid 2894] (13)Permission denied: mod_wsgi (pid=2894): Unable to stat Python home /root/anaconda3. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
解决方式:
将/root或/root/anaconda3整个目录权限改成755: chmod 755 -R /root
项目部署结束