Apache 2 + wsgi + flask无法加载Ubuntu 20.04 Azure VM上的主页(或-产生“504网关超时”)

kiz8lqtg  于 2023-06-30  发布在  Apache
关注(0)|答案(1)|浏览(120)

我正在尝试使用apache2 + wsgi在Azure Ubuntu 20.04 VM(标准E20as v5)上部署flask应用程序。
flask应用程序位于/var/www/html/FLASKAPP.web.tool/,Python venv位于/var/www/html/FLASKAPP.web.tool/flaskapp_venv。Python版本是3.9.17。
我可以毫无问题地启动Apache:

  1. apache2.service - The Apache HTTP Server
  2. Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
  3. Active: active (running) since Mon 2023-06-26 06:49:27 UTC; 8s ago
  4. Docs: https://httpd.apache.org/docs/2.4/
  5. Process: 1708 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SU>
  6. Main PID: 1712 (apache2)
  7. Tasks: 57 (limit: 193324)
  8. Memory: 29.0M
  9. CGroup: /system.slice/apache2.service
  10. ├─1712 /usr/sbin/apache2 -k start
  11. ├─1715 /usr/sbin/apache2 -k start
  12. └─1716 /usr/sbin/apache2 -k start
  13. Jun 26 06:49:27 ServerName systemd[1]: Starting The Apache HTTP Server...
  14. Jun 26 06:49:27 ServerName systemd[1]: Started The Apache HTTP Server.

但是当在浏览器中键入VM的IP时,无论是HTTP还是HTTPS,我过去都会得到“504 GATEWAY TIMEOUT”,但现在(因为我扩展了apache conf文件中的Timeout参数),浏览器只是不断尝试加载页面而没有任何消息(甚至在10分钟后也没有...)。
这是错误日志中的错误:

  1. [Mon Jun 26 07:25:00.309224 2023] [wsgi:error] [pid 2989:tid 140305911248640] [client <IP>:58226] Timeout when reading response headers from daemon process 'flaskapp': /var/www/html/FLASKAPP.web.tool/app.wsgi

至于VM的网络,我认为它是开放的,因为:

  • 我添加了两个入站端口规则:一个用于端口80,一个用于端口443,协议=“任意”,源=“任意”,目的地=“任意”,操作=“允许”。
  • 在安装我的应用之前,当我在浏览器中输入VM的IP时,Apache默认主页就会出现。(我现在从/var/www/html/中删除了它)

这是应用配置文件/etc/apache2/sites-available/flaskapp.com.conf(这是/etc/apache2/sites-available/中唯一的文件,我删除了默认文件):

  1. <VirtualHost *:443>
  2. ServerName flaskapp.com
  3. ServerAlias www.flaskapp.com
  4. ServerAdmin Admin@localhost
  5. DocumentRoot "/var/www/html/FLASKAPP.web.tool/"
  6. WSGIProcessGroup flaskapp
  7. WSGIDaemonProcess flaskapp request-timeout=600 processes=2 threads=20 user=www-data group=www-data python-home=/var/www/html/FLASKAPP.web.tool/ python-path=/var/www/html/FLASKAPP.web.tool/:/var/www/html/FLASKAPP.web.tool/flaskapp_venv/lib/python3.9/site-packages/
  8. WSGIScriptAlias / /var/www/html/FLASKAPP.web.tool/app.wsgi
  9. WSGIApplicationGroup %{GLOBAL}
  10. <Directory "/var/www/html/FLASKAPP.web.tool/">
  11. AllowOverride All
  12. Require all granted
  13. </Directory>
  14. CustomLog /var/log/apache2/flaskapp.com-access_log combined
  15. ErrorLog /var/log/apache2/flaskapp.com-error_log
  16. LogLevel warn
  17. SSLEngine on
  18. SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
  19. SSLCipherSuite ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
  20. SSLHonorCipherOrder on
  21. SSLCertificateFile /etc/ssl/certs/wildcard.institution.com.crt
  22. SSLCertificateKeyFile /etc/ssl/private/wildcard.institution.com.key
  23. SSLCACertificateFile /etc/ssl/certs/institution-ca-bundle.crt
  24. </VirtualHost>
  25. <VirtualHost *:80>
  26. ServerName flaskapp.com
  27. ServerAlias www.flaskapp.com
  28. DocumentRoot "/var/www/html/FLASKAPP.web.tool/"
  29. WSGIProcessGroup flaskapp
  30. # WSGIDaemonProcess flaskapp processes=2 threads=20 user=www-data python-home=/var/www/html/FLASKAPP.web.tool/ python-path=/var/www/html/FLASKAPP.web.tool/:/var/www/html/FLASKAPP.web.tool/flaskapp_venv/lib/python3.9/site-packages/
  31. WSGIScriptAlias / /var/www/html/FLASKAPP.web.tool/app.wsgi
  32. WSGIApplicationGroup %{GLOBAL}
  33. <Directory "/var/www/html/FLASKAPP.web.tool/">
  34. AllowOverride All
  35. Require all granted
  36. </Directory>
  37. CustomLog /var/log/apache2/flaskapp.com-access_log combined
  38. ErrorLog /var/log/apache2/flaskapp.com-error_log
  39. LogLevel warn
  40. </VirtualHost>

下面是Apache配置文件/etc/apache2/apache2.conf

  1. # This is the main Apache server configuration file. It contains the
  2. # configuration directives that give the server its instructions.
  3. # See http://httpd.apache.org/docs/2.4/ for detailed information about
  4. # the directives and /usr/share/doc/apache2/README.Debian about Debian specific
  5. # hints.
  6. #
  7. #
  8. # Summary of how the Apache 2 configuration works in Debian:
  9. # The Apache 2 web server configuration in Debian is quite different to
  10. # upstream's suggested way to configure the web server. This is because Debian's
  11. # default Apache2 installation attempts to make adding and removing modules,
  12. # virtual hosts, and extra configuration directives as flexible as possible, in
  13. # order to make automating the changes and administering the server as easy as
  14. # possible.
  15. # It is split into several files forming the configuration hierarchy outlined
  16. # below, all located in the /etc/apache2/ directory:
  17. #
  18. # /etc/apache2/
  19. # |-- apache2.conf
  20. # | `-- ports.conf
  21. # |-- mods-enabled
  22. # | |-- *.load
  23. # | `-- *.conf
  24. # |-- conf-enabled
  25. # | `-- *.conf
  26. # `-- sites-enabled
  27. # `-- *.conf
  28. #
  29. #
  30. # * apache2.conf is the main configuration file (this file). It puts the pieces
  31. # together by including all remaining configuration files when starting up the
  32. # web server.
  33. #
  34. # * ports.conf is always included from the main configuration file. It is
  35. # supposed to determine listening ports for incoming connections which can be
  36. # customized anytime.
  37. #
  38. # * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
  39. # directories contain particular configuration snippets which manage modules,
  40. # global configuration fragments, or virtual host configurations,
  41. # respectively.
  42. #
  43. # They are activated by symlinking available configuration files from their
  44. # respective *-available/ counterparts. These should be managed by using our
  45. # helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See
  46. # their respective man pages for detailed information.
  47. #
  48. # * The binary is called apache2. Due to the use of environment variables, in
  49. # the default configuration, apache2 needs to be started/stopped with
  50. # /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not
  51. # work with the default configuration.
  52. # Global configuration
  53. #
  54. #
  55. # ServerRoot: The top of the directory tree under which the server's
  56. # configuration, error, and log files are kept.
  57. #
  58. # NOTE! If you intend to place this on an NFS (or otherwise network)
  59. # mounted filesystem then please read the Mutex documentation (available
  60. # at <URL:http://httpd.apache.org/docs/2.4/mod/core.html#mutex>);
  61. # you will save yourself a lot of trouble.
  62. #
  63. # Do NOT add a slash at the end of the directory path.
  64. #
  65. #ServerRoot "/etc/apache2"
  66. #
  67. # The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
  68. #
  69. #Mutex file:${APACHE_LOCK_DIR} default
  70. #
  71. # The directory where shm and other runtime files will be stored.
  72. #
  73. DefaultRuntimeDir ${APACHE_RUN_DIR}
  74. #
  75. # PidFile: The file in which the server should record its process
  76. # identification number when it starts.
  77. # This needs to be set in /etc/apache2/envvars
  78. #
  79. PidFile ${APACHE_PID_FILE}
  80. #
  81. # Timeout: The number of seconds before receives and sends time out.
  82. #
  83. Timeout 600
  84. #
  85. # KeepAlive: Whether or not to allow persistent connections (more than
  86. # one request per connection). Set to "Off" to deactivate.
  87. #
  88. KeepAlive On
  89. #
  90. # MaxKeepAliveRequests: The maximum number of requests to allow
  91. # during a persistent connection. Set to 0 to allow an unlimited amount.
  92. # We recommend you leave this number high, for maximum performance.
  93. #
  94. MaxKeepAliveRequests 100
  95. #
  96. # KeepAliveTimeout: Number of seconds to wait for the next request from the
  97. # same client on the same connection.
  98. #
  99. KeepAliveTimeout 5
  100. # These need to be set in /etc/apache2/envvars
  101. User ${APACHE_RUN_USER}
  102. Group ${APACHE_RUN_GROUP}
  103. #
  104. # HostnameLookups: Log the names of clients or just their IP addresses
  105. # e.g., www.apache.org (on) or 204.62.129.132 (off).
  106. # The default is off because it'd be overall better for the net if people
  107. # had to knowingly turn this feature on, since enabling it means that
  108. # each client request will result in AT LEAST one lookup request to the
  109. # nameserver.
  110. #
  111. HostnameLookups Off
  112. # ErrorLog: The location of the error log file.
  113. # If you do not specify an ErrorLog directive within a <VirtualHost>
  114. # container, error messages relating to that virtual host will be
  115. # logged here. If you *do* define an error logfile for a <VirtualHost>
  116. # container, that host's errors will be logged there and not here.
  117. #
  118. ErrorLog ${APACHE_LOG_DIR}/error.log
  119. #
  120. # LogLevel: Control the severity of messages logged to the error_log.
  121. # Available values: trace8, ..., trace1, debug, info, notice, warn,
  122. # error, crit, alert, emerg.
  123. # It is also possible to configure the log level for particular modules, e.g.
  124. # "LogLevel info ssl:warn"
  125. #
  126. LogLevel warn
  127. # Include module configuration:
  128. IncludeOptional mods-enabled/*.load
  129. IncludeOptional mods-enabled/*.conf
  130. # Include list of ports to listen on
  131. Include ports.conf
  132. # Sets the default security model of the Apache2 HTTPD server. It does
  133. # not allow access to the root filesystem outside of /usr/share and /var/www.
  134. # The former is used by web applications packaged in Debian,
  135. # the latter may be used for local directories served by the web server. If
  136. # your system is serving content from a sub-directory in /srv you must allow
  137. # access here, or in any related virtual host.
  138. <Directory />
  139. Options FollowSymLinks
  140. AllowOverride None
  141. Require all denied
  142. </Directory>
  143. <Directory /usr/share>
  144. AllowOverride None
  145. Require all granted
  146. </Directory>
  147. <Directory /var/www/>
  148. Options Indexes FollowSymLinks
  149. AllowOverride None
  150. Require all granted
  151. </Directory>
  152. #<Directory /srv/>
  153. # Options Indexes FollowSymLinks
  154. # AllowOverride None
  155. # Require all granted
  156. #</Directory>
  157. # AccessFileName: The name of the file to look for in each directory
  158. # for additional configuration directives. See also the AllowOverride
  159. # directive.
  160. #
  161. AccessFileName .htaccess
  162. #
  163. # The following lines prevent .htaccess and .htpasswd files from being
  164. # viewed by Web clients.
  165. #
  166. <FilesMatch "^\.ht">
  167. Require all denied
  168. </FilesMatch>
  169. #
  170. # The following directives define some format nicknames for use with
  171. # a CustomLog directive.
  172. #
  173. # These deviate from the Common Log Format definitions in that they use %O
  174. # (the actual bytes sent including headers) instead of %b (the size of the
  175. # requested file), because the latter makes it impossible to detect partial
  176. # requests.
  177. #
  178. # Note that the use of %{X-Forwarded-For}i instead of %h is not recommended.
  179. # Use mod_remoteip instead.
  180. #
  181. LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
  182. LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
  183. LogFormat "%h %l %u %t \"%r\" %>s %O" common
  184. LogFormat "%{Referer}i -> %U" referer
  185. LogFormat "%{User-agent}i" agent
  186. # Include of directories ignores editors' and dpkg's backup files,
  187. # see README.Debian for details.
  188. # Include generic snippets of statements
  189. IncludeOptional conf-enabled/*.conf
  190. # Include the virtual host configurations:
  191. IncludeOptional sites-enabled/*.conf
  192. # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
  193. ServerName 127.0.0.1

/var/www/html/FLASKAPP.web.tool/app.wsgi

  1. #!/var/www/html/FLASKAPP.web.tool/flaskapp_venv/bin/python3.9
  2. import sys
  3. sys.path.insert(0, '/var/www/html/FLASKAPP.web.tool')
  4. activate_this = '/var/www/html/FLASKAPP.web.tool/flaskapp_venv/bin/activate_this.py'
  5. with open(activate_this) as file_:
  6. exec(file_.read(), dict(__file__=activate_this))
  7. from main import app as application

这是/var/www/html/FLASKAPP.web.tool/main.py

  1. LOTS OF CODE...
  2. if __name__ == '__main__':
  3. app.run()

我将/var/www/html/的所有权更改为www-data,因此ls -l /var/www/html/现在返回

  1. total 4
  2. drwxr-xr-x 7 www-data www-data 4096 Jun 26 06:56 FLASKAPP.web.tool

但没有用。
我将感激任何帮助。

r1zhe5dt

r1zhe5dt1#

我在应用程序配置文件中将python-home=/var/www/html/FLASKAPP.web.tool/更改为python-home=/var/www/html/FLASKAPP.web.tool/flaskapp_venv,问题就解决了。然而,我后来遇到了另一个issue

相关问题