当前位置: 首页 > 工具软件 > mod_wsgi > 使用案例 >

CentOS7.4 部署 Django + Python3 + Apache + Mod_wsgi 详细步骤

蒲功
2023-12-01

一. 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

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 venv

2.开启 virtualenv python3 环境

# source venv/bin/activate

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

# pip install django pymysql

三. 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/evnv"

4.配置 apache 配置文件

# vi /etc/httpd/conf/httpd.conf

I 进入编辑模式,末行添加

LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"

esc, shift+; 输入wq保存并退出

# vi /etc/httpd/conf.d/django.conf
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>

WSGIPythonHome "/var/www/html/django/.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/django/myproject/wsgi.py

</VirtualHost>

将上面的配置设置复制进去
我在这里是将django2导入到dajngo中
所以需要修改一下目录路径
我的是在django后面+django2
将myproject改成django2/django2
将.py3env改成自己上面创建的venv

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

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

WSGIPythonHome "/var/www/html/django/venv"

Listen 8080
<VirtualHost *:8080>

ServerName django.example.com

WSGIDaemonProcess django2 python-path=/var/www/html/django/venv/lib/python3.6/site-pachages
WSGIScriptAlias / /var/www/html/django/django2/django2/wsgi.py

</VirtualHost>

esc, shift+; 输入wq保存并退出

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

# systemctl restart httpd

# systemctl enable httpd
#  systemctl stop firewalld

四. Django 项目配置

1.保证 virtualenv python3 环境开启

# source /var/www/html/django/venv/bin/activate

安装上传文件的模块

# deactivate
# yum install lrzsz -y
# source venv/bin/activate

上传文件

# rz 

选择上传的文件django2

# ls 
django2.zip  venv

解压文件

# unzip django2.zip
# cd django2
# cd app1/migrations
# rm -rf 0001_initial.py
# cd ../..
# cd app2/migrations
# rm -rf 0001_initial.py
# cd ../..
# rm -rf db.sqlite3
# python manage.py makemigrations
#  python 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.创建项目管理员账户

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

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

# pyhton manage.py collectstatic
125 static files copied to '/var/www/html/django/django2/static'.

7.修改 wsgi 入口文件

# vi django2/wsgi.py
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
sys.path.append('/var/www/html/django/django2')

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

8.添加ALLOWED_HOSTS

# vi django2t/settings.py
ALLOWED_HOSTS = ['django.example.com']

Update:

ALLOWED_HOSTS = ['django.example.com']
# 或者
ALLOWED_HOSTS = ['*']

9.修改项目属主和权限

# chmod -R 755 /var/www/html

# chown -R apache:apache /var/www/html
# setenforce 0
# systemctl restart httpd

修改.views.py
将里面的本地127.0.0.1:8000改成当前ip路由和端口

原文链接:http://www.showerlee.com/archives/2511 有改动

 类似资料: