> CentOS > CentOS服务器 > 环境配置 >

Centos7下使用Apache部署Django项目

一、python配置
   1、安装python3.6.5源及依赖包
# yum install epel-release –y
 
# yum groupinstall "Development tools" –y
 
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel –y
 
   2、编译安装python3.6.5以及pip package manager
# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz --no-check-certificate
 
# tar xf Python-3.6.5.tar.xz
 
# cd Python-3.6.5
 
# ./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
 
# make && make altinstall
 
#有些服务器安装后不会自动添加环境变量,需要手动添加,方法如下:有环境变量的可以跳过
 
# vi /etc/profile
 
在最后一行添加
 
export PATH=$PATH:/usr/local/bin
修改后立即生效
 
# source /etc/profile
 
 
 
 
 
 
  3、安装 virtualenv
# pip3.6 install --upgrade pip(更新pip,  可以不用更新)
 
# pip3.6 install virtualenv
 
 二、Django环境配置
    1、配置 django virtualenv
# mkdir -p /var/www/html/django
 
# cd /var/www/html/django
 
# virtualenv -p /usr/local/bin/python3.6 .py3env (.py3env是一个文件夹名字,可以自定义)
 
   2、开启virtualenv python3环境并在此环境安装django相关模块(virtualenv python3环境)
# source .py3env/bin/activate
 
# pip install django pymysql
 
# deactivate(退出virtualenv python3 虚拟环境)
 
 三、Apache配置
   1、安装apache package(在真实环境中安装)
# yum install httpd httpd-devel -y
 
   2、安装 mod_wsgi for python3(在使用的虚拟环境中安装)
# pip install mod_wsgi
 
   3、导出apache所需的mod_wsgi模块
# mod_wsgi-express install-module
 
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
 
WSGIPythonHome "/var/www/html/.py3env"
 
    4、配置apache配置文件
# vi /etc/httpd/conf/httpd.conf
 
末行添加:
 
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
 
 # vi /etc/httpd/conf.d/django.conf
 
Alias /static /var/www/html/项目名/static
<Directory /var/www/html/项目名/static>
Require all granted
</Directory>
 
<Directory /var/www/html/项目名/项目>
 <Files wsgi.py>
   Require all granted
 </Files>
</Directory>
 
WSGIPythonHome "/var/www/html/项目名/.py3env"
 
Listen 8080
<VirtualHost *:8080>
 
ServerName django.example.com
 
WSGIDaemonProcess myproject python-path=/var/www/html/django/.py3env/lib/python3.6/site-pachages
WSGIScriptAlias / /var/www/html/项目名/项目/wsgi.py
 
</VirtualHost>
 
 
    5、重启apache并设置开机自启动
# systemctl restart httpd
 
# systemctl enable httpd
 
deactivate(退出virtualenv python3 虚拟环境)
 
 四、Django项目配置
1、上传django项目(django2),在退出虚拟环境下上传,之后的操作都在虚拟环境
django2压缩包,注意:不能有数据库,缓存文件
 
# yum install lrzsz –y
 
# rz
 
#unzip django2.zip
 
解压之后可以删除django2.zip
 
 2、创建表格      注意:在虚拟环境下进行(# source .py3env/bin/activate)
# cd django2 (切换到django2目录)
 
# python manage.py makemigrations
 
# python manage.py migrate
 
3、创建项目管理员账户(可以暂时不用)
# ./manage.py createsuperuser
 
 4、生成项目静态文件目录
# python manage.py collectstatic
 
 5、修改wsgi入口文件(注意:一定要确定wsgi.py文件的位置)
# vi django2/wsgi.py
 
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "项目名.settings")
sys.path.append('/var/www/html/项目')
 
from django.core.wsgi import get_wsgi_application
 
application = get_wsgi_application()
 
 
6、添加ALLOWED_HOSTS
# vi django2/settings.py
 
添加:
 
ALLOWED_HOSTS = ['django.example.com',’ip地址’]
 
 7、修改项目属主和权限
# chmod -R 755 /var/www/html
 
# chown -R apache:apache /var/www/html
 
 补充
关闭centos7的防火墙
 
# firewall-cmd --state(查看防火墙的状态)
 
# systemctl stop firewalld(关闭防火墙)
 
# systemctl start firewalld(开启防火墙)
 
临时关闭selinux防火墙 输入setenforce 0 永久关闭可使用: vi /etc/selinux/config 改:SELINUX=enforcing  为:SELINUX=disabled (输入getenforce查看状态)
 


(责任编辑:IT)