Until PythonCGI runs on UbuntuServer18.04 + Apache2

Background

--Windows 10 on your desktop is crazy --Django could not be used ――But I want to make it look like a web application!

___That's it! Let's make it CGI! ___ I decided to start with a very pathetic process. When I actually tried it, it got stuck in a delicate place, so I will introduce ___ that it should work for the time being if you follow this street.

Preparation

--Assign a fixed IP address to the server --If you are at home, it should be assigned from the router settings ――I don't know the mobile phone or the company --Linking the network with DDNS (Description of MyDNS.JP) --It's not good to access with 117.125.xxx.xxx --Very dangerous as it exposes the global IP address ――It looks like that --Python3 installed

Apache 2 preparation

Installation

sudo apt install apache2

There is no problem with this one line. If you have selected "LAMP Server" when installing Ubuntu Server, the installation has already been completed and you can ignore it.

Enable cgi module

user@hostname:~# sudo a2enmod cgid
Enabling module cgid.
To activate the new configuration, you need to run:
  systemctl restart apache2
user@hostname:~# sudo systemctl restart apache2

If the activation is completed successfully, / usr / lib / cgi-bin will be recognized as the document root of the de CGI. As it is, the location is too different from the normal document root / var / www / html and it is troublesome, so change the document root.

Change document root #optional

Create a new file.

user@hostname:~# sudo vi /etc/apache2/conf-available/cgi-bin.conf

Contents

/etc/apache2/conf-available/cgi-bin.conf


<Directory "/var/www/cgi-bin">
    Options +ExecCGI
    AddHandler cgi-script .cgi .py
</Directory>

Once created, Apache2 needs to recognize the configuration file.

user@hostname:~# mkdir /var/www/cgi-bin
user@hostname:~# sudo a2enconf cgi-bin
Enabling conf cgi-bin.
To activate the new configuration, you need to run:
  service apache2 reload
user@hostname:~# sudo systemctl restart apache2

Coding notes

--Shebang is required --Otherwise it will not be recognized normally --Don't forget to change Shebang if the environment changes between the development environment and the server

If you get an error

403 Forbidden

The permissions of the file may be incorrect. Open the file location in the terminal and set the permissions.

user@hostname:~# sudo chmod 755 xxx.cgi

500 Internal Server Error (script error)

Probably the most confusing error code. Here are two things that are most common to look at the error log.

1. Python module is not installed

If ModuleNotFoundError: No module named'hoge' is displayed, just press pip install hoge. If you can't install it with the pip command, you can search it from Github.

2. An error occurs when Japanese is entered

In the example that I actually encountered, if only half-width characters were used, they would be displayed without problems, but if full-width characters were included, an error would occur. It is difficult to deal with it in the script as it was garbled at the time of reading the file by any means. So change the Apache2 settings.

/etc/apache2/conf-available/charset.conf


#One line postscript
AddDefaultCharset UTF-8

In the case of the author, this has been resolved.

502 Bad Gateway (no response)

--Is the router port open? --Are you able to cancel the firewall settings? - sudo ufw allow 80 --Is Apache running?

Let's re-examine these possibilities.

Recommended Posts

Until PythonCGI runs on UbuntuServer18.04 + Apache2
Until OSD335X runs U-boot on a custom board (memo)
Until docker-compose up on CentOS7
Apache installation fails on CentOS 8.2