我的Flask应用程序在使用Flask服务器进行本地环回时效果很好,但是现在我试图让apache2在我的专用192.168.0.0/24网络(服务器的IP为192.168.0.18)上为该应用程序提供服务。
EDIT3:除了将.wsgi从文档根目录移出之外,我遵循了Dumpleton先生的大部分建议(我计划在正常运行后这样做)。我能够运行hellow world脚本。我的私有LAN上的任何计算机都可以通过服务器的私有IP看到它。但是,当实际尝试使我的网站运行时,将返回404。 /var/log/apache2/error.log中没有错误。我怀疑问题出在我的flaskapp.wsgi脚本中。似乎与用于简单的“ hello world”页面的页面有很大不同。
有什么建议吗?谢谢你堆栈溢出
root@server:/var/www/flaskapp# cat /etc/hosts
127.0.0.1 www.flaskapp.com
127.0.1.1 server
root@server:/etc/apache2/sites-enabled# ls -l
total 0
lrwxrwxrwx 1 root root 32 Jun 29 23:27 flaskapp.conf -> ../sites-available/flaskapp.conf
root@server:/var/www/flaskapp# ls -l
total 8
drwxr-xr-x 8 root root 4096 Jun 30 18:14 flaskapp
-rw-r--r-- 1个根目录1397年6月30日19:02 flaskapp.wsgi
root@server:/var/www/flaskapp/flaskapp# ls -l
total 92
drwxr-xr-x 2 root root 4096 Jun 29 15:19 bin
drwxr-xr-x 2 bin root 4096 Jun 29 15:08 include
-rw-r--r-- 1 root root 64 Jun 30 00:19 __init__.py
drwxr-xr-x 3 bin root 4096 Jun 29 15:08 lib
drwxr-xr-x 2 bin root 4096 Jun 29 15:08 local
-rw-r--r-- 1 bin robobot 10113 Jun 30 19:12 main.py
-rw-r--r-- 1 bin robobot 10066 Jun 28 11:54 main.py.bak
-rw-r--r-- 1 bin root 61 Jun 29 15:08 pip-selfcheck.json
-rw-r--r-- 1 bin robobot 80 Jun 28 16:03 requirements.txt
-rw-r--r-- 1 bin robobot 7525 Jun 25 11:46 rss_gen27.py
-rw-r--r-- 1 bin robobot 7525 Jun 25 11:46 rss_gen27.py.bak
-rw-r--r-- 1 bin robobot 7520 Jun 25 11:42 rss_gen.py
-rw-r--r-- 1 bin robobot 7334 Jun 20 16:01 rss_gen.py.bak
drwxr-xr-x 2 bin robobot 4096 Jun 29 09:24 static
drwxr-xr-x 2 bin robobot 4096 Jun 28 11:53 templates
root@server:/var/www/flaskapp/flaskapp# cat /etc/apache2/sites-enabled/flaskapp.conf
<VirtualHost *:80>
ServerName www.flaskapp.com
ServerAlias flaskapp.com
DocumentRoot /var/www/flaskapp
<Directory /var/www/flaskapp>
<IfVersion < 2.4>
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
</Directory>
WSGIDaemonProcess flaskapp python-path=/var/www/flaskapp:/var/www/flaskapp/flaskapp/lib/python2.7/site-packages
WSGIProcessGroup flaskapp
WSGIScriptAlias / /var/www/flaskapp/flaskapp.wsgi
Alias /static /var/www/flaskapp/flaskapp/static
<Directory /var/www/flaskapp>
<IfVersion < 2.4>
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
root@server:/var/www/flaskapp# cat flaskapp.wsgi
#!/usr/bin/python
activate_this = '/var/www/flaskapp/flaskapp/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
import os
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/flaskapp")
from flaskapp import app as application
application.secret_key = "random string of chars "
root@server:/var/www/flaskapp/flaskapp# cat __init__.py
from flask import Flask
app = Flask(__name__)
import main
main.py:
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def home():
return render_template("index.html")
if __name__ == '__main__':
app.run(debug=True)
答案 0 :(得分:0)
此信用归Dumpleton先生所有...这对我来说很重要,目的是使flask应用程序在专用LAN上得到服务。
root@server:/var/www/flaskapp# pwd
/var/www/flaskapp
root@server:/var/www/flaskapp# ls -l
total 96
drwxr-xr-x 2 root root 4096 Jun 29 15:19 bin
-rw-r--r-- 1 root root 750 Jul 1 00:14 flaskapp.wsgi
drwxr-xr-x 2 bin root 4096 Jun 29 15:08 include
-rw-r--r-- 1 root root 64 Jun 30 00:19 __init__.py
drwxr-xr-x 3 bin root 4096 Jun 29 15:08 lib
drwxr-xr-x 2 bin root 4096 Jun 29 15:08 local
-rw-r--r-- 1 root root 10113 Jun 30 23:34 main.py
-rw-r--r-- 1 bin robobot 10113 Jun 30 23:20 main.py.bak
-rw-r--r-- 1 bin root 61 Jun 29 15:08 pip-selfcheck.json
-rw-r--r-- 1 bin robobot 80 Jun 28 16:03 requirements.txt
-rw-r--r-- 1 bin robobot 7525 Jun 25 11:46 rss_gen27.py
-rw-r--r-- 1 bin robobot 7525 Jun 25 11:46 rss_gen27.py.bak
-rw-r--r-- 1 bin robobot 7520 Jun 25 11:42 rss_gen.py
-rw-r--r-- 1 bin robobot 7334 Jun 20 16:01 rss_gen.py.bak
drwxr-xr-x 2 bin robobot 4096 Jun 29 09:24 static
drwxr-xr-x 2 bin robobot 4096 Jun 28 11:53 templates
root@server:/var/www/flaskapp# cat __init__.py ; cat flaskapp.wsgi
from flask import Flask
app = Flask(__name__)
import main
#!/usr/bin/python
import sys
import logging
import os
this_dir = os.path.dirname(__file__)
sys.path.insert(0, this_dir)
activate_this = '/var/www/flaskapp/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
this_dir = os.path.dirname(__file__)
logging.basicConfig(stream=sys.stderr)
from main import app as application
application.secret_key = "random string here"
Logs are located in /var/log/apache2/error.log
/etc/apache2/sites-enabled/flaskapp.conf:
<VirtualHost *:80>
ServerName www.flaskapp.com
ServerAlias flaskapp.com
DocumentRoot /var/www/flaskapp
<Directory /var/www/flaskapp>
<IfVersion < 2.4>
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
</Directory>
WSGIDaemonProcess flaskapp.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup flaskapp.com
WSGIScriptAlias / /var/www/flaskapp/flaskapp.wsgi
Alias /static /var/www/flaskapp/static
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
root@server:/var/www/flaskapp# cat /etc/hosts
127.0.0.1 www.flaskapp.com
127.0.1.1 server
Originally I thought the secret key was not needed in the .wsgi file and in the file with the routes as well... However I needed the key in both places