Centos7系統部署django專案

南下45°發表於2018-10-10

一. 首先在Centos7系統下安裝Apache伺服器
安裝方法參考
https://blog.csdn.net/super_master_sean/article/details/82907227

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

4安裝 mod_wsgi for python3,這個安裝要在虛擬環境下安裝

pip install mod_wsgi 

5匯出 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"

6.配置 apache 配置檔案

vi /etc/httpd/conf/httpd.conf
末行新增:  LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
建立django.conf配置檔案

建立django.conf配置檔案

vi /etc/httpd/conf.d/django.conf

把下面內容拷貝到配置檔案中

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>

此處的django2為工程的一級目錄,如圖:
在這裡插入圖片描述

四 django專案上傳
1.保證 virtualenv python3 環境開啟

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

2.上傳django2專案到django資料夾中並解壓,進入django2中 生成專案靜態檔案目錄:

python manage.py  collectstatic

3修改django2專案中的wsgi.py檔案

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()

4.新增ALLOWED_HOSTS
在django2路徑下執行下面命令

vi  django2/settings.py

ALLOWED_HOSTS = ['django.example.com']

5.修改專案屬主和許可權

chmod -R 755 /var/www/html

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

相關文章