CentOS7.4部署Django+Python3+Apache+Mod_wsgi

安装环境

Remote: CentOS 7.4 x64 (django.example.com)

Python: Python3.6.5

Apache: Apache 2.4.6

Mod_wsgi: 4.6.4

Django: Django 2.0.4

 

 

一. 系统环境配置

1.关闭iptables和selinux

# su - root

# systemctl stop firewalld

# setenforce 0

# vi /etc/sysconfig/selinux

修改

SELINUX=disabled


2.添加本地host DNS

# vi /etc/hosts

127.0.0.1    django.example.com

 

 

二. 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 libffi-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

 

3.安装virtualenv

# pip3.6 install --upgrade pip

# pip3.6 install virtualenv

 

 

 

三. Django环境配置

1. 配置Django virtualenv

# mkdir -p /web/django

# cd /web/django

# virtualenv -p /usr/local/bin/python3.6 .py3env

 

2. 开启virtualenv python3环境

# source .py3env/bin/activate

 

3. 在此环境安装Django相关模块

# pip install Django pymysql django-cors-headers django-filter  django-oauth-toolkit django-oauth2-provider djangorestframework   djangorestframework-jwt oauthlib requests urllib3 coreapi sqlalchemy numpy pandas matplotlib statsmodels django_crontab

 

 

 

四. Apache配置

1. 安装apache package

# yum install httpd httpd-devel -y

 

2.安装mod_wsgi for python3

Tip:这里其实是一个远古巨坑, 网上90%以上资料的会粗心的直接使用yum install mod_wsgi去安装apache mod_wsgi模块, 这样做其实最终mod模块会调用本地默认的python2的所有库文件, 无论你后面如何配置django入口文件, apache都不会使用我们配置的virutalenv下隔离的python3, 导致apache无法调用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 "/web/.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

WSGIPassAuthorization On

WSGIPythonHome "/web/django/.py3env"

Listen 8080
<VirtualHost *:8080>

ServerName django.example.com

Alias /public /web/django/manager
<Directory /web/django/manager>
 Require all granted
</Directory>

<Directory /web/django/myproject>
  <Files wsgi.py>
    Require all granted
  </Files>
</Directory>

WSGIDaemonProcess myproject python-path=/web/django/.py3env/lib/python3.6/site-packages
WSGIScriptAlias / /web/django/myprojectWebService/wsgi.py

</VirtualHost>
 

5.重启apache并设置开机自启动

# systemctl restart httpd

# systemctl enable httpd

 

 

 

五. Django项目配置

1. 保证virtualenv python3环境开启

# source /web/django/.py3env/bin/activate

 

2.创建一个Django项目

# cd /web/django/

# django-admin startproject myproject .

 

3.添加static目录

# vi myproject/settings.py

末行添加:

STATIC_ROOT = os.path.join(BASE_DIR, "public/")

 

4.创建本地SQLlite文件

Tip:这里使用SQLlite代替其他数据库作为我们项目的DB

# ./manage.py makemigrations
# ./manage.py migrate

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

 

5.创建项目管理员账户

# ./manage.py createsuperuser

Username (leave blank to use 'root'): root
Email address: admin@admin.com
Password:
Password (again):
Superuser created successfully.

 

6.生成项目静态文件目录

# ./manage.py collectstatic

 

7.修改wsgi入口文件

# vi myproject/wsgi.py

import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
sys.path.append('/web/django')

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值