使用apache 部屬flask網頁服務-3 flask部屬WSGI及相關設定

筆記種類
apache2

 確定apache2有運行之後,所以之後把檔案放到linux目錄/var/www裡面

 之後開資料夾權限,777(可讀可寫可執行)因為本人網頁會有寫的功能所以預設能寫

sudo chmod 777 /var/www

在flask根目錄創app.wsgi

import sys,os

sys.path.append('/var/www/web/.venv/lib/python3.10/site-packages/')

sys.path.insert(0,"/var/www/web")


from __init__ import create_app 

application = create_app()

 

一樣在/etc/apache2/sites-available/ 中給777權限,加一個自己的web.conf

 

web.conf

<VirtualHost *:80>
    ServerName redmaple.life
    ServerAlias redmaple.life www.redmaple.life
    WSGIDaemonProcess flaskapp_http user=www-data group=www-data threads=4
    WSGIScriptAlias / /var/www/web/app.wsgi

    <Directory /var/www/web>
        WSGIProcessGroup flaskapp_http
        WSGIApplicationGroup %{GLOBAL}
        Options FollowSymLinks
        AllowOverride All
    </Directory>

	ErrorLog /var/www/web/flaskblog/logs/error.log
	CustomLog /var/www/web/flaskblog/logs/access.log combined
	RedirectMatch 403 ^/cgi-bin/.*$
	Redirect permanent / https://redmaple.life
</VirtualHost>

<VirtualHost *:443>
    ServerName redmaple.life
    ServerAlias redmaple.life www.redmaple.life
    WSGIDaemonProcess flaskapp_https user=www-data group=www-data threads=4
    WSGIScriptAlias / /var/www/web/app.wsgi

    <Directory /var/www/web>
        WSGIProcessGroup flaskapp_https
        WSGIApplicationGroup %{GLOBAL}
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
   
	ErrorLog /var/www/web/flaskblog/logs/error.log
	CustomLog /var/www/web/flaskblog/logs/access.log combined



</VirtualHost>

 

之後跑 sudo a2ensite <site-config>

redmaple-server@redmapleserver:/etc/apache2/sites-available$ sudo a2ensite web.conf
[sudo] password for redmaple-server: 
Enabling site web.
To activate the new configuration, you need to run:
  systemctl reload apache2
redmaple-server@redmapleserver:/etc/apache2/sites-available$ systemctl reload apache2
Job for apache2.service failed.
See "systemctl status apache2.service" and "journalctl -xeu apache2.service" for details.
redmaple-server@redmapleserver:/etc/apache2/sites-available$ sudo apachectl configtest
AH00526: Syntax error on line 4 of /etc/apache2/sites-enabled/web.conf:
Invalid command 'WSGIDaemonProcess', perhaps misspelled or defined by a module not included in the server configuration
Action 'configtest' failed.
The Apache error log may have more information.

會發現少WSGI,flask會需要一依靠WSGI才能到正式的生產環境


 

所以我們安裝WSGI 並且確定他有開啟

redmaple-server@redmapleserver:/etc/apache2/sites-available$ sudo apt-get install libapache2-mod-wsgi-py3
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
....
apache2_invoke: Enable module wsgi



redmaple-server@redmapleserver:/etc/apache2/sites-available$ sudo a2enmod wsgi
[sudo] password for redmaple-server: 
Module wsgi already enabled