天天看點

CentOS7.4 部署 Django + Python3 + Apache + Mod_wsgi 詳細步驟

一. 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: [email protected]
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 有改動