欢迎访问www.showerlee.com, 您的支持就是我前进的动力.

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

showerlee 2018-04-16 11:11 Django 阅读 (22,611) 2条评论

安装环境

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 /var/www/html/django

# cd /var/www/html/django

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

2. 开启virtualenv python3环境

# source .py3env/bin/activate

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

# pip install django pymysql

四. 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 "/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

WSGIPythonHome "/var/www/html/django/.py3env"

Listen 8080
<VirtualHost *:8080>

ServerName django.example.com

Alias /static /var/www/html/django/static
<Directory /var/www/html/django/static>
 Require all granted
</Directory>

<Directory /var/www/html/django/myproject>
  <Files wsgi.py>
    Require all granted
  </Files>
</Directory>

WSGIDaemonProcess myproject python-path=/var/www/html/django/.py3env/lib/python3.6/site-packages
WSGIScriptAlias / /var/www/html/django/myproject/wsgi.py

</VirtualHost>

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

# systemctl restart httpd

# systemctl enable httpd

五. Django项目配置

1. 保证virtualenv python3环境开启

# source /var/www/html/django/.py3env/bin/activate

2.创建一个Django项目

# cd /var/www/html/django/

# django-admin startproject myproject .

3.添加static目录

# vi myproject/settings.py

末行添加:

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

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('/var/www/html/django')

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

8.添加ALLOWED_HOSTS

# vi myproject/settings.py

Update:

ALLOWED_HOSTS = ['django.example.com']

9.修改项目属主和权限

# chmod -R 755 /var/www/html

# chown -R apache:apache /var/www/html

查看最终目录下的生成的项目文件

# ls -l

-rwxr-xr-x 1 apache apache 38912 Apr 16 15:04 db.sqlite3
-rwxr-xr-x 1 apache apache   541 Apr 16 14:50 manage.py
drwxr-xr-x 3 apache apache  4096 Apr 16 15:21 myproject
drwxr-xr-x 3 apache apache  4096 Apr 16 15:05 static

最终浏览器访问django项目

Tip:保证windows本地添加django服务器的HOST域名

django测试页面

项目主页, 输入之前创建的管理员账号密码

项目后台

Finished...

正文部分到此结束
版权声明:除非注明,本文由(showerlee)原创,转载请保留文章出处!
本文链接:http://www.showerlee.com/archives/2511

继续浏览:APACHEdjangomod_wsgipython3

2条大神的评论

loading
  1. 沙发
    a2019年5月25日下午9:29 回复

    ;-) 良心文章啊,必须支持一下

    :grin: :grin: 好羞射,文章真的好赞啊,顶博主!

  2. 板凳
    chilli2020年7月30日下午6:45 回复

    您好, 目前我们遇到了一个问题, 完成这边步骤的时候出现了问题, 能帮忙看下是否有解决方案吗? 非常感谢

    问题:centos7安装mod_wsgi for python3并导出apache所需的mod_wsgi模块

发表评论

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif