Django使用虚拟环境部署到Apache遇到的坑

环境
     CentOS7.2
    Apache2.4.6
    Python3.6.7
    Django2.0.2

1、安装git

sudo yum install git

2、安装pyven

 ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
exec $SHELL -l

3、安装依赖

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel libpcap-devel xz-devel gcc

4、安装Python3.6.7

pyenv install -l
mkdir ~/.pyenv/cache

5、下载https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz保存到~/.pyenv/cache目录下,千万别解压,然后一定要用下面的命令安装,要不然安装mod_wsgi的时候会报很玄幻的错误

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.6.7 -v

6、配置基本环境

pyenv global 3.6.7
yum install httpd httpd-devel
systemctl restart httpd
systemctl enable httpd
pip install virtualenv
mkdir /home/wiikvenv
cd /home/wiikvenv
virtualenv venv
source venv/bin/activate
pip install mod_wsgi

7、最关键的一步,引入对应版本Apache和Python的mod_wsgi.so

mod_wsgi-express module-config
#出现以下两句
LoadModule wsgi_module "/home/wiikvenv/venv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/home/wiikvenv/venv"
#把这两句复制到/etc/httpd/conf/httpd.conf文件的最后
chmod 755 -R /root

8、安装django(在虚拟环境中安装)

pip install django

9、在/etc/httpd/conf/httpd.conf中配置虚拟服务器


<VirtualHost *:8000>
    WSGIScriptAlias / /home/apis/uploadimgs/imgsUpload/wsgi.py
    WSGIDaemonProcess www-data python-path=/home/apis/uploadimgs:/home/apis/uploadimgs/venv/lib/python3.6/site-packages 
    WSGIProcessGroup www-data
	WSGIPassAuthorization On
    #如果使用jwt且在头部做权限验证一定要加上这句,要不然apache会剥离原来的头部
    <Directory /home/apis/uploadimgs>
		<Files wsgi.py>
			Require all granted
		</Files>
    </Directory>
	ErrorLog /var/log/sanlitian/upload_imgs.log
</VirtualHost>

最后注意,要把home的权限改为755
建议直接用mySQL,自带的sqllite因为权限问题有时候用不了

chmod 755 -R /home # 即将使用virtualenv创建的独立环境的父目录权限设置为755
暂无评论