Install and configure Ubuntu Apache and set CGI

Source: Internet
Author: User
Tags fully qualified domain name

14:40:26 | category:

Linux | label: Linux Ubuntu Apache
| Large font size, medium/small subscription

To learn about Python CGI, you need to install Apache under Ubuntu. Therefore, we learned how to install and configure Apache on ubuntu11.04 today.
1. install Apache
There are two ways to install the tar file: Download the tar file from the Apache website and install it on your computer. I used the second method for convenience.
Ubuntu provides the powerful apt-Get install command. Enter the following command on the terminal: Sudo apt-Get install apache2

In this way, you can install Apache conveniently and quickly. However, the inconvenience is that you do not know where Apache is installed.

Ii. Start and Stop the apache service
The Apache startup and stop files are:/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 in restarting the service. When I restart the service, the error "cocould not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName" will be reported ". I searched the internet and said that ServerName IP: Port and Listen IP: Port are inconsistent in httpd. conf, that is, ports are inconsistent. Therefore, add "ServerName" to the httpd. conf file.
Localhost: 80.
Httpd. conf is an Apache configuration file. It will be described later in the/etc/apache2 directory.
Before modifying the httpd. conf file, you must modify the permissions of the file. Otherwise, you do not have the permission to modify the file. The command is:Sudo chmod 777/etc/apache2/httpd. conf
By the way, you can view the Apache version by running the following command:Apache2ctl-V
The result is displayed as follows:
Server version: Apache/2.2.17 (Ubuntu)
Server built: Nov 3 2011 02:13:18

Iii. Apache configuration file
The configuration file for common Apache releases is:/etc/apache2/httpd. conf, but it is found that this file is empty. In fact, this file is configured by the user, which will be discussed later.
Strictly speaking, the Apache configuration file of Ubuntu is/etc/apache2/apache2.conf. Apache will automatically read the configuration information of this file at startup, while some other configuration files, for example, httpd. conf is included through the include command. You can find these in apache2.conf.
Include row:
● Dynamic module configuration
Include mod-enabled/*. Load
Include mod-enabled/*. conf
● User Configuration
Include httpd. conf
● Port listening Configuration
Include ports. conf
● General configuration statement snippets
Include Conf. d/
● VM configuration commands
Include sites-enabled/
We can put all the settings in apache2.conf, httpd. conf, or any configuration file. This division is a good habit.

1. web document root directory
The most important thing after installing Apache is to know where the root directory of the Web document is. For Ubuntu, the default value is/var/www. So how do we know? There is no description in apache2.conf, and httpd. conf is empty. It must be in other configuration files. Later, the search found that the content in/etc/apache2/sites-enabled/000-default contains the following content:
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>
In DocumentRoot, set the root directory to/var/www.

2. sites-enabled and sites-available Directories
The sites-enabled directory was just found in apache2.conf, and there is a sites-available directory under/etc/apache2. What are these two directories? In fact, the sites-available Directory stores the real configuration file, while the sites-enabled directory stores only some symbolic connections pointing to the files here.
Ls-lCommand to view that 000-default in/etc/apache2/sites-enabled points to/etc/apache2/sites-available/default. The VM configuration commands are stored here.
Although sites-available is used to store valid content, it does not work.Ln
The command is linked to enabled before it can take effect.

3. Directory of mod-enabled and mod-available
The two directories are of the same nature as the above mentioned sites-enabled and sites-available. Here, the configuration files and links of the apachegon function module are stored.

4. Ports. conf settings
The port used by Apache is set here. To adjust the default port settings, we recommend that you edit this file. You can also delete the include Prots. conf line in apache2.conf and set the desired port in httpd. conf.

Iv. cgi Configuration
1. Create a cgi-bin directory under the root directory, that is,/var/www/cgi-bin /. CGI programs are stored in the cgi-bin directory, and cannot be stored in other directories.

2. Open/etc/apache2/sites-enabled/000-default and 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>

To:

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>

Modify/usr/lib/cgi-bin/to/var/www/cgi-bin/, and add addhandler CGI-script CGI. If options does not have execcgi, remember to add it.

3. Restart the apache service:Sudo/etc/init. d/apache2 restart

4. If you cannot access the CGI script, modify the/etc/apache2/mod-enabled/cigd. Load file. In the original loadmodule cgid_module/usr/lib/apache2/modules/mod_cgid.so, add:

Addhandler CGI-script. cgi. pl. py. SH and then restart the apache service.

5. Modify CGI program permissions. The CGI program attribute must be set to run (755). If the directory of the CGI-related HTML file is to be written by the CGI program, its permission must be set to writable (666 ). The command is:Sudo chmod 755 ~ /Simple. cgi

At this point, enter the corresponding URL in the browser to access the CGI program smoothly, such as http: // localhost/cgi-bin/simple. cgi. If not, it is probably a problem with the CGI program. You can find the error by searching for the log. Apache logs are stored in/var/log/apache2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.