将Flask部署到apache2

1.安装mod_wsgi:

sudo apt-get install libapache2-mod-wsgi python-dev
2.启用mod_wsgi

sudo a2enmod wsgi
3.创建Web应用的apache2配置文件

sudo vim /etc/apache2/sites-available/FlaskApp.conf
以下是YourApp.conf文件内容


		ServerName mywebsite.com
		ServerAdmin [email protected]
		WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
		FlaskApp/FlaskApp/>
			Order allow,deny
			Allow from all
		
		Alias /static /var/www/FlaskApp/FlaskApp/static
		FlaskApp/FlaskApp/static/>
			Order allow,deny
			Allow from all
		
		ErrorLog ${APACHE_LOG_DIR}/error.log
		LogLevel warn
		CustomLog ${APACHE_LOG_DIR}/access.log combined
4.启用配置文件

sudo a2ensite FlaskApp

5.创建.wsgi文件

cd /var/www/FlaskApp
sudo vim flaskapp.wsgi 
flaskapp.wsgi文件内容为:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Add your secret key'
现在你的工程目录应该是下面结构:

|--------FlaskApp
|----------------FlaskApp
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------__init__.py
|----------------flaskapp.wsgi
6.重启服务器

service apache2 restart

注意事项:

*应用的启动模块名称必须是__init__.py

参考:https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps


你可能感兴趣的:(将Flask部署到apache2)