--Install apache2 package on Ubuntu --Enable mod_cgid with the a2enmod command --Install CGI script --Enable the setting with the a2ensite command
Install the apache2 package.
$ sudo apt install apache2
Check the version.
$ /usr/sbin/apachectl -V
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2019-08-14T14:36:32
Server's Module Magic Number: 20120211:88
Server loaded: APR 1.6.5, APR-UTIL 1.6.1
Compiled using: APR 1.6.5, APR-UTIL 1.6.1
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"
Check that it is running by accessing it with curl etc.
$ curl -I http://localhost/
HTTP/1.1 200 OK
Date: Tue, 21 Jan 2020 10:47:37 GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Tue, 21 Jan 2020 10:28:46 GMT
ETag: "2aa6-59ca3df7ac2c0"
Accept-Ranges: bytes
Content-Length: 10918
Vary: Accept-Encoding
Content-Type: text/html
Enable the cgid module with a2enmod cgi or a2enmod cgid.
$ sudo a2enmod cgi
Your MPM seems to be threaded. Selecting cgid instead of cgi.
Enabling module cgid.
To activate the new configuration, you need to run:
systemctl restart apache2
mod \ _cgid -Apache HTTP Server Version 2 \ .4
On some Unix operating systems, forking a process from a multithreaded server can be a very costly operation. The reason is that the new process replicates all the threads of the parent process. To avoid this cost at each CGI startup, mod_cgid forks the child process to run an external daemon to run the CGI script. The primary server uses unix domain sockets to communicate with this daemon.
This module is used instead of mod_cgi whenever multithreaded MPM is chosen at compile time. At the user level, the settings and behavior of this module is exactly the same as mod_cgi. The only exception is the addition of the ScriptSock directive, which specifies the name of the socket for communicating with the CGI daemon.
Create a / var / www / hello directory.
$ sudo mkdir /var/www/hello
Grant permissions to users who edit CGI script files.
$ sudo chown hoge:hoge /var/www/hello
Place the index.cgi file.
$ vim /var/www/hello/index.cgi
The contents of index.cgi. This time, it is CGI by shell script.
#!/usr/bin/sh
echo 'Status: 200 OK'
echo 'Content-Type: text/html;charset=utf-8'
echo ''
echo '<html><body>Hello, world.</body></html>'
Grant execute permission to index.cgi.
$ chmod 755 /var/www/hello/index.cgi
Copy the 000-default.conf file located in the / etc / apache2 / sites-available directory to create a file called hello.conf.
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/hello.conf
Modify the contents of the hello.conf file.
$ sudo vim /etc/apache2/sites-available/hello.conf
Replace the hello.conf file with the following:
hello.conf
<VirtualHost *:80>
# /etc/apache2/sites-available/000-default.Contents copied from conf
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Settings to run CGI
ScriptAlias /hello/ /var/www/hello/
<Directory "/var/www/hello/">
Options ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex index.cgi
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
Currently 000-default is enabled.
$ ls -l /etc/apache2/sites-enabled/ | grep conf
lrwxrwxrwx 1 root root 35 January 21 19:28 000-default.conf -> ../sites-available/000-default.conf
Enable hello.conf with the a2ensite command.
$ sudo a2ensite hello
Enabling site hello.
To activate the new configuration, you need to run:
systemctl reload apache2
Disable 000-default.conf with the a2dissite command.
$ sudo a2dissite 000-default
Site 000-default disabled.
To activate the new configuration, you need to run:
systemctl reload apache2
You can see that hello.conf is enabled.
$ ls -l /etc/apache2/sites-enabled/ | grep conf
lrwxrwxrwx 1 root root 29 January 21 20:03 hello.conf -> ../sites-available/hello.conf
$ sudo systemctl restart apache2
You can check that CGI is working with curl command etc.
$ curl -i http://localhost/hello/
HTTP/1.1 200 OK
Date: Tue, 21 Jan 2020 11:09:41 GMT
Server: Apache/2.4.41 (Ubuntu)
Content-Length: 40
Content-Type: text/html;charset=utf-8
<html><body>Hello, world.</body></html>
Recommended Posts