The Ubuntu Apache configuration and the cgi configuration method

  • 2020-05-14 05:42:55
  • OfStack

In learning Python CGI, you need to install Apache under Ubuntu, so today we learned the installation and configuration of Apache on Ubuntu11.04.

1. Apache installation

There are two ways to install it: one is to download the tar files from the Apache website and install them on your own computer; The other is installed directly over the network. I used the second method for convenience.
Ubuntu provides the powerful apt-get install command, which is entered at the terminal: sudo apt-get install apache2
This makes it easy and fast to install Apache. However, there is an inconvenience here, you do not know where Apache has been installed.

2. Start and stop the Apache service

Apache start and stop the file: / etc/init d/apache2
Start command: sudo apache2ctl-k start
Stop command: sudo apache2ctl-k stop
Restart command: sudo apache2ctl - k restart (sudo/etc/init d/apache2 restart)
There is a problem with rebooting. When I restart the service, I get an error: "Could not reliably determine the server s fully domain name, using 127.0.1.1 for ServerName". I searched 1 on the Internet and said httpd. ServerName IP: Port and Listen IP: Port are not 1, that is, the port is not 1. So add "ServerName localhost:80" to the httpd.conf file.
httpd.conf is a configuration file of Apache in the /etc/apache2 directory, which will be introduced later.
In modified httpd. conf documents before also need to modify the file permissions, otherwise there is no permission to modify, command to: sudo chmod 777 / etc/apache2 / httpd conf
By the way, to view the version of Apache, the command: apache2es1064en-v will display:
Server version: Apache/2.2.17 (Ubuntu)
Server built: Nov 3 2011 02:13:18

3. Configuration file for Apache

Ordinary Apache distribution configuration file: / etc apache2 / httpd conf, but found that the file is empty, actually this is the user to configure, said later.
Strictly speaking, Ubuntu Apache configuration file is/etc/apache2 / apache2 conf, Apache at startup will automatically read the file information, and the other 1 some configuration files, such as httpd. conf etc., by Include instructions included. These Include lines can be found in apache2.conf:
● dynamic module configuration
Include mods-enabled/*.load
Include mods-enabled/*.conf
● user's own configuration
Include httpd.conf
Configuration of port listening
Include ports.conf
● 1 generic configuration statement fragment
Include conf.d/
● virtual host configuration instructions
Include sites-enabled/
We can easily put all the Settings in apache2.conf or httpd.conf or any one of the configuration files. This division is a good habit.

1.Web document root
One of the most important things to do after installing Apache is to know where the root of the Web document is. For Ubuntu, the default is /var/www. So how do we know? apache2.conf is not described, httpd.conf is also empty, it must be in some other configuration file. Search later found to be in the/etc/apache2 / sites - enabled / 000 - default, there are:


 DocumentRoot /var/www
  <Directory />
 Options FollowSymLinks +ExecCGI
 AllowOverride None
  </Directory>
  <Directory /var/www/>
 Options Indexes FollowSymLinks MultiViews
 AllowOverride None
 Order allow,deny
 allow from all
  </Directory>

Here in DocumentRoot, I'm going to set the root to /var/www

2. sites-enabled and sites-available directories
The sites-enabled directory has just been found in apache2.conf, and there is another sites-available directory under /etc/apache2. What are these two directories for? Actually, sites - available inside is the real configuration files, and sites - enabled directory to store just 1 point to the file link here, through the ls - l command to view to/etc/apache2 / sites - 000 - default enabled is point/etc/apache2 / sites - available/default. Here stored is the virtual host configuration instructions.
Although sites-available is for storing valid content, it doesn't work and only works if you link to enabled using the ln command.

3. mods-enabled and mods-available contents
The properties of these two directories are similar to those mentioned above for sites-enabled and sites-available1, where configuration files and links for apachegon function modules are stored.

4. ports. conf Settings
The port used by Apache is set here. If you need to adjust the default port Settings, you are advised to edit this file. Another modification is to remove the line Include prots.conf1 in apache2.conf and set the port you want to use in httpd.conf.

4. CGI configuration

1. First, create a directory of cgi-bin in the root directory, i.e. /var/www/ cgi-bin /. The cgi program is located in this directory, cgi-bin, and cannot be placed in any other directory.
2. Open/etc apache2 / sites - enabled / 000 - default, find the following content:


  ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  <Directory "/usr/lib/cgi-bin/">
  AllowOverride None
  Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  Order allow,deny
  Allow from all
  </Directory>

Is amended as:


  ScriptAlias /cgi-bin/ /var/www/cgi-bin/
  <Directory "/var/www/cgi-bin">
  AllowOverride None
  Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  Order allow,deny
  Allow from all
  AddHandler cgi-script cgi
  </Directory>

Where /usr/lib/ cgi-bin/was changed to /var/www/ cgi-bin /, and AddHandler cgi-script cgi was added. If Options doesn't have ExecCGI, remember 1 has to be added.

3. Restart Apache service: sudo/etc/init d/apache2 restart

4. If you still cannot access cgi script, to modify/etc/apache2 / mods - enabled/cigd load file. In the original LoadModule cgid_module usr/lib apache2 / modules/mod_cgid so, plus:

AddHandler cgi script cgi pl py sh, restart the Apache service.

5. Modify the permissions of the cgi program. The CGI program property 1 must be set to runnable (755), and permission 1 must be set to writable (666) if the directory of HTML files associated with CGI is to be written by the CGI program. The order is: sudo chmod 755 ~/ simple.cgi

At this point, with the corresponding input via a browser url should be able to smooth access to the cgi program, such as http: / / localhost/cgi - bin/simple cgi. If not, it is probably the cgi program itself. You can find the error by looking for Log. Apache log placed under/var log/apache2.


Related articles: