本文檔適用於:
python3.5
apache2.4
mod_wsgi4.3
部署過程:
安裝Apache
$ sudo apt-get install apache2
啓動apache服務
$ sudo /etc/init.d/apache2 start
安裝mod_wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3
如果項目開發使用的是python2,則不需要加-py3
啓動mod_wsgi
$ sudo a2enmod wsgi
創建.wsgi文件,該文件用於啓動項目,如下
import os import sys import re //進入虛擬環境,相當於souce .venv/bin/active activate_this = '/home/sam/Snipe/.venv/bin/activate_this.py' with open(activate_this) as file_: exec(file_.read(), dict(__file__=activate_this)) //這裏需要導入環境變量 from app import app as application
創建apache配置文件,配置虛擬主機,將配置文件添加到/etc/apache2/sites-available/,如下
<VirtualHost 172.18.215.158:5000> ServerAdmin [email protected] ServerName sysu.snipe DocumentRoot /home/sam/Snipe/src/web/ #將/請求轉發給/home/sam/Snipe/src/web/Snipe.wsgi WSGIScriptAlias / /home/sam/Snipe/src/web/Snipe.wsgi #配置本項目的守護進程 WSGIDaemonProcess Snipe user=sam group=sam threads=10 home=/home/sam/Snipe/src/web/ <Directory /home/sam/Snipe/src/web/> WSGIProcessGroup Snipe Require all granted </Directory> ErrorLog /var/log/apache2/error_snipe.log </VirtualHost>
使用apachectl configtest命令可以查看配置文件是否正確
配置監聽端口,添加Listen 172.18.215.158:5000到配置文件/etc/apache2/ports.conf,使apache監聽172.18.215.158:5000
激活虛擬主機
$ sudo a2ensite /etc/apache2/sites-available/Snipe.conf
重新加載apache
$ sudo /etc/init.d/apache2 reload
打開瀏覽器,輸入172.18.215.158:5000,即可看到項目web端~~~!!!
項目結構
+/home/sam/Snipe/
+.venv
+src/web/
+app/
+__init__.py
+...
+Snipe.wsgi
+manage.py
+.env
+...
+...