系统上线前一直使用django测试启动项目,当然是不稳定的,只是用作于测试使用的,最后上线,必须选择适合的web服务器,这里选用apache
前提环境
centos-7.3
python-3.5.1
django-1.11.5
apache-2.4.6
mod_wsgi-4.6.5
首先确认python环境及其系统环境默认安装完成,不过多的讲解。
1.准备工作
首先关闭防火墙selinux很多权限都是selinux引起的,部署完成后,按照需求开启。
安装一下必要环境
yum install vim wget gcc gcc+ zlib-devel openssl-devel -y
#关闭防火墙
systemctl stop firewalld
#临时关闭
setenforce 0
#永久关闭
编辑/etc/selinux/config文件,将SELINUX的值设置为disabled
vim /etc/selinux/config
2.安装apache
yum install httpd-devel -y
启动apache
systemctl start httpd
设置开机启动apache
systemctl enable httpd
打开浏览器输入CentOS IP地址
出现如下画面表示apache 安装成功
3 安装mod_wsgi (不要使用yum直接安装,默认安装到python2上)
两种方法(未能成功可能导致丢失mod_wsgi.so文件,错误可能是没有这个文件记得检查)
1 wget :
wget https://codeload.github.com/GrahamDumpleton/mod_wsgi/tar.gz/4.5.24
从github下载下来的文件是4.5.24,解压后就是mod_wsgi-4.5.24,放入你的自定义目录下
tar zxvf 4.5.24
cd mod_wsgi-4.5.24
使用python3.5编译,一定是python3.5,不然后面会报错。(填写你自己的Python3 路径)
./configure --with-python=/usr/local/python35/bin/python3.5
先make 再make insall
make && make install
2 pip3 :
可以使用pip3 直接安装 建议先下载安装包到指定路径下
pip3 install mod_wsgi --download /root
pip3 下载 mod_wsgi的压缩包 mod_wsgi.tar.gz
tar -zxvf mod_wsgi-4.6.5.tar.gz
生成 mod_wsgi-4.6.5
cd mod_wsgi4.6.5
./configure --with-python=/usr/local/python3/bin/python3.5
make
make install
chmod 755 /usr/lib64/httpd/modules/mod_wsgi.so
没有错误的到这一步算是环境搭建完成
安装完成后默认目录下结构为
[root@local httpd]# cd /etc/httpd/
[root@local httpd]# ls
conf conf.d conf.modules.d logs modules run
用最简单配置方式达到效果提高效率
主配置文件是/etc/httpd/conf/httpd.conf。
配置存储在的/etc/httpd/conf.d/目录。
这边以两个项目为例子,同一台机器,不同端口访问方式
主配置文件修改
Listen 80 下可以添加一个 监听8080端口
vi /etc/httpd/conf/httpd.conf
Listen 8080
LoadModule wsgi_module modules/mod_wsgi.so
该配置用来连接django.wsgi,使工程被apache加载。
配置django wsgi
在项目目录下新建wsgi,里面新建django.wsgi,内容如下
import os, sys
sys.path.append("/usr/local/python3/lib/python3.5/site-packages")
os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “project.settings”)
sys.path.append(’/var/www/project/’)
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
DJANGO_SETTINGS_MODULE必须指向你项目的settings.py文件。
在/etc/httpd/conf.d/
中添加配置文件project.conf,内容如下这里
样式有点不会放,示例在这里
<VirtualHost *:80>
WSGIScriptAlias / “/var/www/InsSortware/InsSortware/wsgi.py”
Alias /static/ /var/www/InsSortware/static/
ServerName 192.168
#ServerName example.com
ServerAlias www.anzhuangcx.com
<Directory /var/www/InsSortware/static>
Require all granted
<Directory “/var/www/InsSortware”>
AllowOverride All
Require all granted
ErrorLog /etc/httpd/logs/InsSortware.error.log
LogLevel warn
配置完成后重新启动apache systemctl restart httpd
如果启动失败,出现如下错误
[root@local lib]# systemctl restart httpd
Job for httpd.service failed because the control process exited with error code. See “systemctl status httpd.service” and “journalctl -xe” for details.
缺少mod_wsgi.so这个文件,将其复制过去就可以了
cp /usr/lib64/httpd/modules/mod_wsgi.so /etc/httpd/modules/
tail -f /var/log/httpd/error_log cd 查看httpd错误日志
tail -f /etc/httpd/logs/InsSortware.error.log cd 查看部署的项目错误日志(其中一个)
启动成功后访问固定IP 加 端口出现对应web页面
模糊的地方希望指正,算是自己归纳的apache多项目启动,同时更新,亲测有效成功,可以进一步交流!