Configure the apache2 web server.

Ubuntu use the apache2 as default web server, install the apache with commmand:

apt install apache2
Open the http://localhost in the web browser will see the message "it works" page.
We want to setup a virtual host for example.com, open the document root directory /var/www and create directory:
mkdir example.com
create a test index.html in example.com directory:
<html>
<head>
  <title> My web site! </title>
</head>
<body>
  <p> This is example.com home!</p>
</body>
</html>
set the permission of example.com directory as:
chmod -R 0755 example.com
Open the /etc/apache2/sites-available dir and create a file exmaple.com.conf:
<VirtualHost *:80>
	ServerAdmin my_user_name@localhost

	DocumentRoot /var/www/example.com
        ServerName  example.com
        ServerAlias wwww.example.com
	<Directory />
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Require all granted
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Require all granted
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Delete the 000-default.conf or replace the lines with the lines from the example.com.conf, this is for when there are many virtual hosts the http://localhost will open the default one.
To activate the new virtual host run:
sudo a2ensite .conf
Restart the apache2 with:
service apache2 reload
Open the the new site http://www.example.com in a web browser.

Enable cgi-bin for scripts:

The default directory for cgi-bin is /usr/lib/cgi-bin and you can put any where, first enable the cgi modules from the /etc/apache2-moades-avaliable:
a2ensite cgid.load a2ensite cgi
To enable globally for all domains open /etc/apache2/apache2.conf and add these lines in the end:
###################################################################
#########     Adding capaility to run CGI-scripts #################
ServerName localhost
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py
Or can enable individually for each site, add the AddHandler line to the domain sites conf in the /etc/sites-available/ just below the options of cgi-bin code:
 
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/var/www/example.com/cgi-bin">
      AllowOverride None
      Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
      AddHandler cgi-script .cgi .pl .py
      Require all granted
        </Director>


<< Previous Next >>