# Apache+mod_wsgi+Python3.7.4部署Django HTTP应用 **Published by:** [kk](https://paragraph.com/@kk-24/) **Published on:** 2023-03-29 **URL:** https://paragraph.com/@kk-24/apache-mod-wsgi-python3-7-4-django-http ## Content #一、阿里云CentOS7配置 以下以root用户来操作 赋予执行权限!!(root)$ chmod +x /root 安装系统依赖有Python、MySQL(可选)依赖、Elasticsearch对Java的依赖,Django-compressor的压缩需要的依赖bzip2-devel等(root)$ yum -y update (root)$ yum install -y install python-devel zlib-devel mysql-devel libffi-devel openssl-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel java wget gcc make tmux 1.3 安装Python3.7.4最新版本(root)$ wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz #编译python (root)$ tar -zxvf Python-3.7.4.tgz (root)$ cd Python-3.7.4 (root)$ ./configure --prefix=/usr/local/python3 (root)$ make&&make install #添加软连接 (root)$ ln -s /usr/local/python3/bin/python3 /usr/bin/python3 (root)$ ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 注意:加上--enable-optimizations后make的速度非常慢,但是执行Python代码时会有10%-20%的性能提升。二、安装apache/redisyum -y install httpd httpd-devel redis 启动apache三、准备工作安装虚拟环境,复制下虚拟环境的路径,exit退出虚拟环境,然后切换到Python3.7的安装包目录,执行以下命令./configure --prefix=/root/.local/share/virtualenvs/zhihu-k2NuTCjv/ --enable-shared make && make install 注意:Python3.7版本以后都要使用--enable-shared的命令。 然后搜索libpython3.7m.so.1.0find / -name libpython3.7m.so.1.0 /root/.local/share/virtualenvs/zhihu-k2NuTCjv/lib/libpython3.7m.so.1.0 /root/Python-3.7.2/libpython3.7m.so.1.0 注意,是虚拟环境的路径。/root/.local/share/virtualenvs/zhihu-k2NuTCjv/lib/libpython3.7m.so.1.0复制下来,拷贝到/usr/lib64/目录下面cp /root/.local/share/virtualenvs/zhihuuTCjv/lib/libpython3.7m.so.1.0 /usr/lib64/ 此时才能安装mod_wsgi第三方包,进入虚拟环境!!pipenv shell pip3 install mod_wsgi mod_wsgi-express install-module LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so" WSGIPythonHome "/root/.local/share/virtualenvs/zhihu-k2NuTCjv" 修改apache配置vim /etc/httpd/conf/httpd.conf 搜索Example,在226行加入如下命令LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so" 搜索ServerName,改成如下:此时重启apache服务,使用的Python就会变成3.7版本配置apache创建文件并编辑touch /etc/httpd/conf.d/django.conf django.conf文件如下:WSGIPythonHome /root/.local/share/virtualenvs/zhihu-k2NuTCjv WSGIPythonPath /root/zhihu <VirtualHost *:80> ServerName localhost Alias /static/ /root/zhihu/zhihu/staticfiles/ Alias /media/ /root/zhihu/zhihu/media/ <Directory /root/zhihu/zhihu/staticfiles> Require all granted </Directory> <Directory /root/zhihu/zhihu/media> Require all granted </Directory> <Directory /root/zhihu/config> <Files wsgi.py> Require all granted </Files> </Directory> WSGIDaemonProcess zhihu python-path=/root/.local/share/virtualenvs/zhihu-k2NuTCjv WSGIScriptAlias / /root/zhihu/config/wsgi.py </VirtualHost> 保存后重启,部署成功! ## Publication Information - [kk](https://paragraph.com/@kk-24/): Publication homepage - [All Posts](https://paragraph.com/@kk-24/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@kk-24): Subscribe to updates