Problems with apache2 mod-wsgi configuration

2,069 views
Skip to first unread message

Jian Wu

unread,
Oct 10, 2021, 4:09:55 PM10/10/21
to mod...@googlegroups.com
Dear Graham,


I am following this guide https://asdkazmi.medium.com/deploying-flask-app-with-wsgi-and-apache-server-on-ubuntu-20-04-396607e0e40f to deploy a flask app together with apache2 and mod-wsgi. My intension is being able to perform a request post call from another server over the internet to my server running flask. 

It seems that the author suggests to install mod-wsgi using the below command. I am not sure if that package is supported?

sudo apt-get install libapache2-mod-wsgi-py3

I followed the instruction, and when I typed in my local IP address on a browser I received a message saying “Forbidden you don’t have the permission to access this ressource Apache 2.4.46 (Ubuntu server at 192.168.0.179 port 80).

Hardware: 
- Raspberry Pi 4 with Ubuntu 20.04. My local IP is 192.168.0.179
- Router configuration: As seen in the picture below, I have forwarded port 22 external to internal 22 and external port 81 to internal port 80.

These ports are opened on my Ubuntu server
(env) hd2900@hd2900:/etc/apache2/sites-available$ sudo ufw status
[sudo] password for hd2900: 
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
OpenSSH                    ALLOW       Anywhere                  
Apache Full                ALLOW       Anywhere                  
81/tcp                     ALLOW       Anywhere                  
22/tcp (v6)                ALLOW       Anywhere (v6)             
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Apache Full (v6)           ALLOW       Anywhere (v6)             
81/tcp (v6)                ALLOW       Anywhere (v6) 

My Apache2 configuration file:
(env) hd2900@hd2900:~/Documents/Python/hd2900TakeawayPrint$ cat /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
        WSGIDaemonProcess flaskapp threads=5
        WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
        WSGIApplicationGroup %{GLOBAL}

        <Directory flaskapp>
             WSGIProcessGroup flaskapp
             WSGIApplicationGroup %{GLOBAL}
             Order deny,allow
             Allow from all 
        </Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
#Header set Access-Control-Allow-Origin "*"
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet


My Flask app folder structure:
(env) hd2900@hd2900:~/Documents/Python/hd2900TakeawayPrint$ ls -la
total 32
drwxrwxr-x 4 hd2900 hd2900 4096 Oct 10 18:23 .
drwxrwxr-x 3 hd2900 hd2900 4096 Oct 10 18:04 ..
drwxrwxr-x 4 hd2900 hd2900 4096 Oct 10 18:08 env
-rw-rw-r-- 1 hd2900 hd2900  249 Oct 10 18:23 flaskapp.py
-rw-rw-r-- 1 hd2900 hd2900   96 Oct 10 18:23 flaskapp.wsgi
drwxrwxr-x 8 hd2900 hd2900 4096 Oct 10 18:23 .git
-rw-r--r-- 1 root   root      5 Oct 10 18:05 .gitignore
-rw-rw-r-- 1 hd2900 hd2900   69 Oct 10 18:04 README.md

flaskapp.wsgi content
cat flaskapp.wsgi 
import sys 
sys.path.insert(0, '/var/www/html/flaskapp')

Flaskapp.py content
cat flaskapp.py
# flaskapp.py
# This is a "hello world" app sample for flask app. You may have a different file.
from flask import Flask
app = Flask(__name__)
@app.route('/') 
def hello_world():
   return 'Hello from Flask!' 
if __name__ == '__main__':

I don’t know how to troubleshoot this problem further, and I hope for your advice.

Thanks in advance!

Best regards,
Jian

Graham Dumpleton

unread,
Oct 10, 2021, 5:23:13 PM10/10/21
to mod...@googlegroups.com
For a start, this is wrong:

        <Directory flaskapp>
             WSGIProcessGroup flaskapp
             WSGIApplicationGroup %{GLOBAL}
             Order deny,allow
             Allow from all 
        </Directory>

It should be:

        <Directory /var/www/html/flaskapp>
             WSGIProcessGroup flaskapp
             Require all granted 
        </Directory>

Graham

On 11 Oct 2021, at 7:09 am, Jian Wu <jian...@gmail.com> wrote:

Dear Graham,


I am following this guide https://asdkazmi.medium.com/deploying-flask-app-with-wsgi-and-apache-server-on-ubuntu-20-04-396607e0e40f to deploy a flask app together with apache2 and mod-wsgi. My intension is being able to perform a request post call from another server over the internet to my server running flask. 

It seems that the author suggests to install mod-wsgi using the below command. I am not sure if that package is supported?

sudo apt-get install libapache2-mod-wsgi-py3

I followed the instruction, and when I typed in my local IP address on a browser I received a message saying “Forbidden you don’t have the permission to access this ressource Apache 2.4.46 (Ubuntu server at 192.168.0.179 port 80).

Hardware: 
- Raspberry Pi 4 with Ubuntu 20.04. My local IP is 192.168.0.179
- Router configuration: As seen in the picture below, I have forwarded port 22 external to internal 22 and external port 81 to internal port 80.
<Indsat grafik-1.tiff>

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/2BCCBE0B-7ECB-462D-998D-B0048679D76B%40gmail.com.

Jian Wu

unread,
Oct 11, 2021, 1:19:18 PM10/11/21
to mod...@googlegroups.com
Hello Graham,

Thanks for pointing this out. I have now corrected it so the configuration file looks as below. The problem however still persists. 

cat /etc/apache2/sites-enabled/000-default.conf 
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
        WSGIDaemonProcess flaskapp threads=5
        WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
        WSGIApplicationGroup %{GLOBAL}

        <Directory flaskapp>
             WSGIProcessGroup flaskapp
    Require all granted
        </Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
#Header set Access-Control-Allow-Origin "*"
</VirtualHost>

Best regards,
Jian

Graham Dumpleton

unread,
Oct 11, 2021, 4:37:32 PM10/11/21
to mod...@googlegroups.com
You didn’t use exactly what I gave you. The argument to the Directory directive needs to be the path to the directory holding the WSGI script file, not the name of the mod_wsgi daemon process group.

        <Directory /var/www/html/flaskapp>
             WSGIProcessGroup flaskapp
     Require all granted
        </Directory>

Graham

Jian Wu

unread,
Oct 11, 2021, 4:54:39 PM10/11/21
to mod...@googlegroups.com
Hello Graham,

Sorry I missed the argument in Directory. I have now changed the configuration file as below. 

(env) hd2900@hd2900:~/Documents/Python/hd2900TakeawayPrint$ cat /etc/apache2/sites-enabled/000-default.conf 
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
        WSGIDaemonProcess flaskapp threads=5
        WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
        WSGIApplicationGroup %{GLOBAL}

        <Directory /var/www/html/flaskapp>
             WSGIProcessGroup flaskapp
    Require all granted
        </Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
#Header set Access-Control-Allow-Origin "*"
</VirtualHost>

Furthermore I have ensured that the dynamic link is created as seen below in the path /var/www/html

(env) hd2900@hd2900:/var/www/html$ ll
total 20
drwxr-xr-x 2 root root  4096 Oct 10 18:38 ./
drwxr-xr-x 3 root root  4096 Sep 26 21:54 ../
lrwxrwxrwx 1 root root    19 Oct 10 18:38 flaskapp -> hd2900TakeawayPrint
-rw-r--r-- 1 root root 10918 Sep 26 21:54 index.html

In the hd2900TakeawayPrint folder the flasapp.wsgi is located

(env) hd2900@hd2900:~/Documents/Python/hd2900TakeawayPrint$ ll -la
total 32
drwxrwxr-x 4 hd2900 www-data 4096 Oct 10 18:23 ./
drwxrwxr-x 3 hd2900 hd2900   4096 Oct 10 18:04 ../
drwxrwxr-x 4 hd2900 hd2900   4096 Oct 10 18:08 env/
-rwxrwxr-x 1 hd2900 www-data  249 Oct 10 18:23 flaskapp.py*
-rwxrwxr-x 1 hd2900 www-data   96 Oct 10 18:23 flaskapp.wsgi*
drwxrwxr-x 8 hd2900 hd2900   4096 Oct 10 18:23 .git/
-rw-r--r-- 1 root   root        5 Oct 10 18:05 .gitignore
-rwxrwxr-x 1 hd2900 hd2900     69 Oct 10 18:04 README.md*

Still when I from a browser type https://my-global-IP:81 I get the permission error.

Best regards,
Jian



Graham Dumpleton

unread,
Oct 11, 2021, 5:06:01 PM10/11/21
to mod...@googlegroups.com
That symlink is not pointing where you think it is, it is pointing at "/var/www/html/hd2900TakeawayPrint”. You need to specify an absolute pathname for the target when creating the symlink. Even then that may not work as Apache has rules about following symlinks and am not sure whether the default rules allow it or whether you need to enable it. If not enabled, the target directory would need a Directory directive all well to allow access to it.

A further issue is that you seem to be using a Python virtual environment but have not told mod_wsgi to use it. And another potential problem will be whether your home directory is accessible by other users.

Presuming that your home directory is called “/home/yourusername” and it is accessible to others, try using:

        WSGIDaemonProcess flaskapp threads=5 python-home=/home/yourusername/Documents/Python/hd2900TakeawayPrint/env
        WSGIScriptAlias / /home/yourusername/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi
        WSGIApplicationGroup %{GLOBAL}

        <Directory /home/yourusername/Documents/Python/hd2900TakeawayPrint>

             WSGIProcessGroup flaskapp
     Require all granted
        </Directory>

Jian Wu

unread,
Oct 15, 2021, 2:53:16 AM10/15/21
to mod...@googlegroups.com
Thanks again for your comments. I have read the quick configuration guide for mod_wsgi. Below is apache2 configuration file look like. I am now using absolute path to the project, wsgi file and the virtual environment. 

(env) hd2900@hd2900:~$ cat /etc/apache2/sites-enabled/000-default.conf 
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
        WSGIDaemonProcess flaskapp threads=5 python-home=/home/hd2900/Documents/Python/hd2900TakeawayPrint/env
        WSGIScriptAlias / /home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi
        WSGIApplicationGroup %{GLOBAL}

        <Directory /home/hd2900/Documents/Python/hd2900TakeawayPrint>
             WSGIProcessGroup flaskapp
    Require all granted
        </Directory>

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
#Header set Access-Control-Allow-Origin "*"
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Regarding permission to home directory. Right now the home directory is owned by root. Should I execute a chown command to change the user to hd2900 and www-data group?

(env) hd2900@hd2900:/$ ls -la
total 1048660
drwxr-xr-x  20 root root       4096 Apr 22 02:13 .
drwxr-xr-x  20 root root       4096 Apr 22 02:13 ..
lrwxrwxrwx   1 root root          7 Apr 22 01:52 bin -> usr/bin
drwxr-xr-x   5 root root       4096 Sep 29 06:06 boot
drwxr-xr-x  19 root root      19200 Sep  7 20:34 dev
drwxr-xr-x   2 root root       4096 Apr 22 02:13 .disk
drwxr-xr-x 135 root root       8192 Oct 15 07:29 etc
drwxr-xr-x   3 root root       4096 Sep 26 18:13 home
lrwxrwxrwx   1 root root          7 Apr 22 01:52 lib -> usr/lib
drwx------   2 root root      16384 Apr 22 02:13 lost+found
drwxr-xr-x   2 root root       4096 Apr 22 01:52 media
drwxr-xr-x   2 root root       4096 Apr 22 01:52 mnt
drwxr-xr-x   2 root root       4096 Apr 22 01:52 opt
dr-xr-xr-x 298 root root          0 Jan  1  1970 proc
drwx------   5 root root       4096 Sep 26 19:03 root
drwxr-xr-x  35 root root        940 Oct 15 08:06 run
lrwxrwxrwx   1 root root          8 Apr 22 01:52 sbin -> usr/sbin
drwxr-xr-x   9 root root       4096 Sep 27 02:10 snap
drwxr-xr-x   2 root root       4096 Apr 22 01:52 srv
-rw-------   1 root root 1073741824 Apr 22 02:15 swapfile
dr-xr-xr-x  12 root root          0 Jan  1  1970 sys
drwxrwxrwt  17 root root       4096 Oct 15 08:40 tmp
drwxr-xr-x  11 root root       4096 Apr 22 01:52 usr
drwxr-xr-x  15 root root       4096 Sep 26 21:54 var

When I access my local IP from a web browser 192.168.0.179 I still get 403 forbidden error in that I don’t have permission to access the ressources. Is there a log file somewhere allowing me to check the cause of the 403 forbidden error as this will make debugging easier?

Thanks!


Graham Dumpleton

unread,
Oct 15, 2021, 3:25:51 AM10/15/21
to mod...@googlegroups.com
What do you get for:

  ls -lasd /home/hd2900

Graham

Jian Wu

unread,
Oct 15, 2021, 3:36:42 AM10/15/21
to mod...@googlegroups.com
hd2900@hd2900:~/Documents/Python/hd2900TakeawayPrint$ ls -lasd /home/hd2900
4 drwxr-x--- 17 hd2900 hd2900 4096 Oct 11 22:38 /home/hd2900

Graham Dumpleton

unread,
Oct 15, 2021, 3:54:30 AM10/15/21
to mod...@googlegroups.com
The problem is that the Apache user cannot see into your home directory.

One solution is to do:

    chmod o+rx /home/hd2900

Presuming permissions of directories/files underneath that are okay, might work.

For more information see:


Graham

Jian Wu

unread,
Oct 15, 2021, 4:11:25 AM10/15/21
to mod...@googlegroups.com
I just executed the command and now the permission on the hd2900 user is

hd2900@hd2900:/home$ ls -la
total 12
drwxr-xr-x  3 root   root   4096 Sep 26 18:13 .
drwxr-xr-x 20 root   root   4096 Apr 22 02:13 ..
drwxr-xr-x 17 hd2900 hd2900 4096 Oct 11 22:38 hd2900

The Document folder inside also to me seems to have the right permission

hd2900@hd2900:~$ ls -la
total 92
drwxr-xr-x 17 hd2900 hd2900 4096 Oct 11 22:38 .
drwxr-xr-x  3 root   root   4096 Sep 26 18:13 ..
-rw-------  1 hd2900 hd2900 4900 Oct 15 10:02 .bash_history
-rw-r--r--  1 hd2900 hd2900  220 Sep 26 18:06 .bash_logout
-rw-r--r--  1 hd2900 hd2900 3771 Sep 26 18:06 .bashrc
drwx------ 15 hd2900 hd2900 4096 Oct 11 22:38 .cache
drwx------ 11 hd2900 hd2900 4096 Sep 26 18:50 .config
drwx------  3 hd2900 hd2900 4096 Sep 26 18:50 .dbus
drwxr-xr-x  2 hd2900 hd2900 4096 Sep 26 18:13 Desktop
drwxr-xr-x  3 hd2900 hd2900 4096 Oct 10 17:15 Documents
drwxr-xr-x  2 hd2900 hd2900 4096 Sep 26 18:13 Downloads
drwx------  3 hd2900 hd2900 4096 Oct 11 22:36 .gnupg
drwxr-xr-x  3 hd2900 hd2900 4096 Sep 26 18:13 .local
drwx------  4 hd2900 hd2900 4096 Oct 11 22:38 .mozilla
drwxr-xr-x  2 hd2900 hd2900 4096 Sep 26 18:13 Music
drwxr-xr-x  2 hd2900 hd2900 4096 Sep 26 18:13 Pictures
-rw-r--r--  1 hd2900 hd2900  807 Sep 26 18:06 .profile
drwxr-xr-x  2 hd2900 hd2900 4096 Sep 26 18:13 Public
-rw-------  1 hd2900 hd2900    7 Oct 10 18:05 .python_history
drwx------  2 hd2900 hd2900 4096 Oct 10 18:03 .ssh
-rw-r--r--  1 hd2900 hd2900    0 Sep 26 18:18 .sudo_as_admin_successful
drwxr-xr-x  2 hd2900 hd2900 4096 Sep 26 18:13 Templates
drwxr-xr-x  2 hd2900 hd2900 4096 Sep 26 18:13 Videos

Then going one level further down the Python folder also looks good

hd2900@hd2900:~/Documents$ ls -la
total 12
drwxr-xr-x  3 hd2900 hd2900 4096 Oct 10 17:15 .
drwxr-xr-x 17 hd2900 hd2900 4096 Oct 11 22:38 ..
drwxrwxr-x  3 hd2900 hd2900 4096 Oct 10 18:04 Python

Then I have a doubt with respect on group user for the hd2900TakeawayPrint folder. When I set it up I changed the group to www-data. I am wondering if the group should also be hd2900?

hd2900@hd2900:~/Documents/Python$ ls -la
total 12
drwxrwxr-x 3 hd2900 hd2900   4096 Oct 10 18:04 .
drwxr-xr-x 3 hd2900 hd2900   4096 Oct 10 17:15 ..
drwxrwxr-x 4 hd2900 www-data 4096 Oct 10 18:23 hd2900TakeawayPrint

Then finally inside the actual project folder

hd2900@hd2900:~/Documents/Python/hd2900TakeawayPrint$ ls -la
total 32
drwxrwxr-x 4 hd2900 www-data 4096 Oct 10 18:23 .
drwxrwxr-x 3 hd2900 hd2900   4096 Oct 10 18:04 ..
drwxrwxr-x 4 hd2900 hd2900   4096 Oct 10 18:08 env
-rwxrwxr-x 1 hd2900 www-data  249 Oct 10 18:23 flaskapp.py
-rwxrwxr-x 1 hd2900 www-data   96 Oct 10 18:23 flaskapp.wsgi
drwxrwxr-x 8 hd2900 hd2900   4096 Oct 10 18:23 .git
-rw-r--r-- 1 root   root        5 Oct 10 18:05 .gitignore
-rwxrwxr-x 1 hd2900 hd2900     69 Oct 10 18:04 README.md

Now when I enter my local IP from a web browser 192.168.0.179 I get a different error as below

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.


Apache/2.4.46 (Ubuntu) Server at 192.168.0.179 Port 80

Graham Dumpleton

unread,
Oct 15, 2021, 4:14:44 AM10/15/21
to mod...@googlegroups.com
If you are getting an internal server error then it is likely an issue with your code now. Check the Apache error log for why. Also read more generally the document:


and pay attention to things like assumptions on what the working directory is and use of relative paths.

Graham

Jian Wu

unread,
Oct 15, 2021, 4:37:48 AM10/15/21
to mod...@googlegroups.com
A snippet from the error log is as below. From what I can see it is complaining about the flask app.wsgi file and a module not found error. 

Current thread 0x0000ffffbdad6430 (most recent call first):
<no Python frame>
[Fri Oct 15 10:02:17.274152 2021] [mpm_event:notice] [pid 3977:tid 281473864000560] AH00492: caught SIGWINCH, shutting down gracefully
[Fri Oct 15 10:02:22.991922 2021] [mpm_event:notice] [pid 930:tid 281473364440112] AH00489: Apache/2.4.46 (Ubuntu) mod_wsgi/4.7.1 Python/3.9 configured -- resuming normal operations
[Fri Oct 15 10:02:22.998339 2021] [core:notice] [pid 930:tid 281473364440112] AH00094: Command line: '/usr/sbin/apache2'
[Fri Oct 15 10:02:47.363546 2021] [wsgi:error] [pid 931:tid 281473321513216] [remote 192.168.0.187:60639] mod_wsgi (pid=931): Failed to exec Python script file '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:02:47.363813 2021] [wsgi:error] [pid 931:tid 281473321513216] [remote 192.168.0.187:60639] mod_wsgi (pid=931): Exception occurred processing WSGI script '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:02:47.374521 2021] [wsgi:error] [pid 931:tid 281473321513216] [remote 192.168.0.187:60639] Traceback (most recent call last):
[Fri Oct 15 10:02:47.374764 2021] [wsgi:error] [pid 931:tid 281473321513216] [remote 192.168.0.187:60639]   File "/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi", line 3, in <module>
[Fri Oct 15 10:02:47.374817 2021] [wsgi:error] [pid 931:tid 281473321513216] [remote 192.168.0.187:60639]     from flaskapp import app as application
[Fri Oct 15 10:02:47.374919 2021] [wsgi:error] [pid 931:tid 281473321513216] [remote 192.168.0.187:60639] ModuleNotFoundError: No module named 'flaskapp'
[Fri Oct 15 10:02:47.523466 2021] [wsgi:error] [pid 931:tid 281473329967360] [remote 192.168.0.187:60640] mod_wsgi (pid=931): Failed to exec Python script file '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:02:47.523662 2021] [wsgi:error] [pid 931:tid 281473329967360] [remote 192.168.0.187:60640] mod_wsgi (pid=931): Exception occurred processing WSGI script '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:02:47.523945 2021] [wsgi:error] [pid 931:tid 281473329967360] [remote 192.168.0.187:60640] Traceback (most recent call last):
[Fri Oct 15 10:02:47.524076 2021] [wsgi:error] [pid 931:tid 281473329967360] [remote 192.168.0.187:60640]   File "/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi", line 3, in <module>
[Fri Oct 15 10:02:47.524104 2021] [wsgi:error] [pid 931:tid 281473329967360] [remote 192.168.0.187:60640]     from flaskapp import app as application
[Fri Oct 15 10:02:47.524175 2021] [wsgi:error] [pid 931:tid 281473329967360] [remote 192.168.0.187:60640] ModuleNotFoundError: No module named ‘flaskapp'

The wsgi file has 3 lines 

hd2900@hd2900:~/Documents/Python/hd2900TakeawayPrint$ cat flaskapp.wsgi 
import sys 
sys.path.insert(0, '/var/www/html/flaskapp’)

I then checked the folder permission for the symbolic link and noticed that it only has the root permission. 

hd2900@hd2900:/var/www/html$ ls -la
total 20
drwxr-xr-x 2 root root  4096 Oct 10 18:38 .
drwxr-xr-x 3 root root  4096 Sep 26 21:54 ..
lrwxrwxrwx 1 root root    19 Oct 10 18:38 flaskapp -> hd2900TakeawayPrint
-rw-r--r-- 1 root root 10918 Sep 26 21:54 index.html

I proceed by changing the owner for this symbolic link to hd2900:www-data

hd2900@hd2900:/var/www/html$ sudo chown -h hd2900:www-data flaskapp 
hd2900@hd2900:/var/www/html$ ls -la
total 20
drwxr-xr-x 2 root   root      4096 Oct 10 18:38 .
drwxr-xr-x 3 root   root      4096 Sep 26 21:54 ..
lrwxrwxrwx 1 hd2900 www-data    19 Oct 10 18:38 flaskapp -> hd2900TakeawayPrint
-rw-r--r-- 1 root   root     10918 Sep 26 21:54 index.html

Then restarting the apache server and entered the IP address in the browser I see the same error. When I revisit the error log I get the same type of error, similar to what I see for the first time.

[Fri Oct 15 10:32:35.270714 2021] [mpm_event:notice] [pid 930:tid 281473364440112] AH00492: caught SIGWINCH, shutting down gracefully
[Fri Oct 15 10:32:40.592700 2021] [mpm_event:notice] [pid 926:tid 281473787962416] AH00489: Apache/2.4.46 (Ubuntu) mod_wsgi/4.7.1 Python/3.9 configured -- resuming normal operations
[Fri Oct 15 10:32:40.596985 2021] [core:notice] [pid 926:tid 281473787962416] AH00094: Command line: '/usr/sbin/apache2'
[Fri Oct 15 10:33:01.184413 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61800] mod_wsgi (pid=927): Failed to exec Python script file '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:33:01.184693 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61800] mod_wsgi (pid=927): Exception occurred processing WSGI script '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:33:01.201534 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61800] Traceback (most recent call last):
[Fri Oct 15 10:33:01.201805 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61800]   File "/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi", line 3, in <module>
[Fri Oct 15 10:33:01.201850 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61800]     from flaskapp import app as application
[Fri Oct 15 10:33:01.209206 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61800] ModuleNotFoundError: No module named 'flaskapp'
[Fri Oct 15 10:33:01.281697 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61801] mod_wsgi (pid=927): Failed to exec Python script file '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:33:01.281892 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61801] mod_wsgi (pid=927): Exception occurred processing WSGI script '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:33:01.282232 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61801] Traceback (most recent call last):
[Fri Oct 15 10:33:01.282477 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61801]   File "/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi", line 3, in <module>
[Fri Oct 15 10:33:01.282532 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61801]     from flaskapp import app as application
[Fri Oct 15 10:33:01.282626 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61801] ModuleNotFoundError: No module named 'flaskapp'
[Fri Oct 15 10:33:03.732232 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61803] mod_wsgi (pid=927): Failed to exec Python script file '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:33:03.732438 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61803] mod_wsgi (pid=927): Exception occurred processing WSGI script '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:33:03.732749 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61803] Traceback (most recent call last):
[Fri Oct 15 10:33:03.732880 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61803]   File "/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi", line 3, in <module>
[Fri Oct 15 10:33:03.732909 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61803]     from flaskapp import app as application
[Fri Oct 15 10:33:03.732981 2021] [wsgi:error] [pid 927:tid 281473719673088] [remote 192.168.0.187:61803] ModuleNotFoundError: No module named 'flaskapp'
[Fri Oct 15 10:33:03.792557 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61804] mod_wsgi (pid=927): Failed to exec Python script file '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:33:03.792732 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61804] mod_wsgi (pid=927): Exception occurred processing WSGI script '/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi'.
[Fri Oct 15 10:33:03.793037 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61804] Traceback (most recent call last):
[Fri Oct 15 10:33:03.793165 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61804]   File "/home/hd2900/Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi", line 3, in <module>
[Fri Oct 15 10:33:03.793196 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61804]     from flaskapp import app as application
[Fri Oct 15 10:33:03.793268 2021] [wsgi:error] [pid 927:tid 281473753489664] [remote 192.168.0.187:61804] ModuleNotFoundError: No module named 'flaskapp'

Jian Wu

unread,
Oct 15, 2021, 5:03:55 AM10/15/21
to mod...@googlegroups.com
Hello Graham,

Now it works! As you mentioned in one of your previous mail the symbolic link does not do what I think it does, so I changed the wsgi file to the below, where I just imported the absolute path to my flask project directory.

hd2900@hd2900:~$ cat Documents/Python/hd2900TakeawayPrint/flaskapp.wsgi 
import sys 
#sys.path.insert(0, '/var/www/html/flaskapp')
sys.path.insert(0,'/home/hd2900/Documents/Python/hd2900TakeawayPrint')
from flaskapp import app as application

Once again thanks for all the help. This is second time I am setting up a webserver and I most likely will need the support from this forum should I set up additional webserver in the future.

Also thanks to Bill McCarter for sharing his version of how he implemented a similar setup of flask with Ubuntu server.

Best regards,
Jian
Reply all
Reply to author
Forward
0 new messages