LinuxApacheWeb server (continued)

Source: Internet
Author: User
LinuxApacheWeb server (continued)-Linux Enterprise Application-Linux server application information. The following is a detailed description. 5. Open up personal homepage space for users

If we use the LINUX system to set up a WEB server, we can not only store the company's home page, but also provide a personal homepage space for every employee of the company.

First, open an account on LINUX for employees who need personal homepage space. In this way, it has a user home directory "/home/user account name ".

Addusr User Account Name
Passwd User Account Name


Create a directory "public_html" in the user's home directory and set the corresponding permissions for it.

Cd ~ User Account Name
Mkdir public_html
Chmod 755 public_html


Make sure that the UserDir command in the srm. conf file sets the public_html directory.

Ask the employee to upload his/her personal homepage to the public_html directory in his/her home directory.


Now you can use http://www.company.com /~ The user account name is used to access the employee's personal homepage.

6. Use Apache to implement virtual host services

6.1 what is a VM Service

A virtual host service virtualizes a machine into multiple WEB servers. For example, a company wants to provide host hosting services, which provides WEB services for other enterprises. Therefore, it certainly does not prepare a physical server for each enterprise, but uses a large server with powerful functions and then uses a virtual host, it provides WEB Services for multiple enterprises. Although all WEB services are provided by this server, visitors can see that they have the same WEB services on different servers.

Specifically, we can use the VM service to store the home pages of www.company1.com and www.company2.com of two different companies on the same host. Visitors only need to enter the domain name of the company to access the homepage content they want.

You can use Apache to set up the VM service in two ways: IP address-based virtual host and name-based virtual host. The following describes their implementation methods. So that you can select the most appropriate implementation method in specific applications.

6.2 set up and implement IP address-based VM Service

Prerequisites

In this way, you need to set the IP alias on the machine, that is, bind multiple

IP addresses are used to serve multiple VM instances. To use this function, make sure that the IP alias settings must be supported in your Linux kernel. Otherwise, you must re-compile the kernel.

The following is a service setting with two virtual hosts for your reference.

2. configuration steps

Assume that the machine we use to implement the VM service already provides the WEB Service for ourselves. Now we will provide the VM service for www.company1.com, a new company.


Planned IP Address: Apply for a new IP address for the VM. (Assume that the local IP address is 202.101.2.1)

Www.company1.com 202.101.2.2

2) Enable the ISP to resolve the domain name.

3) set an IP alias for the NIC:

/Sbin/ifconfig eth0: 0 202.101.2.2 netmask 255.255.255.0

4) reset "/etc/httpd/conf/httpd. conf" and add the following to the file:


ServerAdmin webmaster@yourdomain.com
DocumentRoot/home/httpd/www.company1.com
ServerName www.company1.com
ErrorLog/var/log/httpd/www.company1.com/error.log


5) create a directory.

Mkdir/home/httpd/www.company1.com
Mkdir/var/log/httpd/www.company1.com/error.log

6) store the corresponding homepage content in the corresponding directory.

3. unfavorable factors

There is a serious deficiency in the implementation of such virtual hosts, that is, each time a virtual host is added, an IP address must be added. Because the IP address space is very tight, it is usually impossible to obtain so many IP addresses. In a sense, this is also a waste of IP addresses.

6.3 set and implement name-based VM Service

The name-based virtual host service is a suitable solution. Because it does not require more IP addresses and is easy to configure, there is no need for special hardware and software support. Most modern browsers support the implementation of such virtual hosts. Of course, this means that some early client browsers may not support the implementation of such virtual hosts.

There is no reason to use the IP address-based VM service instead of the name-based VM service. To configure the name-based VM service, you need to modify the configuration file: "/etc/httpd/conf/httpd. conf". Add the following content to this configuration file.

NameVirtualHost 202.101.2.1

ServerAdmin webmaster@yourdomain.com
DocumentRoot/home/httpd/www.company1.com
ServerName www.company1.com
ErrorLog/var/log/httpd/www.company1.com/error.log


ServerAdmin webmaster@yourdomain.com
DocumentRoot/home/httpd/www.company2.com
ServerName www.company2.com
ErrorLog/var/log/httpd/www.company2.com/error.log


That is, add NameVirtualHost 202.101.2.1 Based on the IP address-based configuration. In this example, two virtual host services are specially set to reflect the need to be added once.

Finally, you can create a directory to put the home page content in the corresponding directory.

VII. Configure CGI

7.1 What is CGI

CGI is a language-independent Gateway Interface Specification. It can be implemented in any popular application development language, including C, C ++, Perl, Shell scripts, and even Java.

The WEB server obtains a URL from the client, which tells the WEB server that a CGI external application must be run. Then the WEB server starts the application and waits for it to complete and returns the output result. Finally, it transmits the output results of this application to the WEB client at the other end.

7.2 configure Apache for CGI

So how can Apache handle CGI requests? We must inform Apache of where to store the CGI program through the corresponding configuration process, and specify the extension of the CGI program. The following describes the settings and steps one by one.


Create a directory for storing CGI programs

Creating a centralized CGI program directory is just the beginning of creating a CGI Environment. To improve security

The centralized CGI program directory should be stored outside the DocumnetRoot directory, so that visitors cannot directly access the CGI program.

Step 1: create a directory outside the DocumentRoot directory and store all CGI programs in this directory. For example, you can create a/home/httpd/public/apps directory as the base camp for CGI programs.

Step 2: Create an alias for the CGI program directory, that is, edit the configuration file httpd. conf, and add:

ScriptAlias/cgi-bin // home/httpd/public/apps

After this is done, when the customer accesses the ingress.

Step 3: set appropriate permissions for the CGI directory. Generally, only Apache is allowed to read and execute but not write.

Allow user access to cgi-bin

We have introduced in section 4.5 how to open up personal homepage space for users, but if your

If a user needs to make his home page more dynamic, he will often apply for cgi-bin access service from you. The following describes two cgi-bin access methods provided by the Apache server.

1) Use Directory or DirectoryMatch container

When the UserDir command is assigned a directory name in the srm. conf configuration file, Apache

Use it as the top-level directory of the WWW site. For example:

UserDir Public_html

When Apache receives www.xxx.com /~ The user request is sent to the/home/user/Public_html homepage.

To add CGI support for each user, add the following configuration in the Apache configuration file access. conf:


Options ExecCGI
AddHandler cgi-script. cgi. pl


Note: You can change DirectoryMatch to Directory.

In this method, the Apache server converts www.xxx.com /~ The user/cgi-bin request is translated into/home/user/Public_html/cgi-bin/and allows execution of any cgi program with the correct extension (. CGI or. pl.

Use the ScriptAliasMatch command

You can also add CGI support for each user by using the ScriptAliasMatch command. For example:

ScriptAliasMatch ~ ([A-z] +)/cgi-bin/(. *)/home/$1/public_html/cgi-bin/$2

This command matches the user name with $1, where $1 and ~ ([A-z] +) equal. Match any content following/cgi-bin/with $2, where $2 is equal.

This configuration also implements /~ The user/cgi-bin/xxx. cgi request is interpreted:

/Home/user/Public_html/cgi-bin/xxx. cgi

If you want to interpret this request as follows:

/Home/httpd/public/apps/xxx. cgi

How can I set it? Yes, it should be:

ScriptAliasMatch ~ ([A-z] +)/cgi-bin/(. *)/home/httpd/public/apps/$2

7.3 environment variables provided by Apache for CGI

The Apache server provides many environment variables that can be used for compiling CGI programs. Understanding these variables will also help you write CGI programs that fully utilize Apache. Therefore, we will also give a brief introduction here.

Server Variables

Server variables are set by Apache to notify CGI programs about Apache. Enabling

With these variables, the CGI program can determine different information about the server: Apache version, administrator's e-mail address, and so on.


SERVER_SOFTWARE

This variable is the Version number of Apache on the WWW server. Its value is like Apache/Version, for example, Apache/1.3;


GATEWAY_INTERFACE
The value of this variable is the version number of the current CGI specification. Its value is in the form of CGI/1.1;


SERVER_ADMIN
If the httpd. conf file contains the e-mail address of the site administrator, this variable will store this e-mail address;


DOCUMENT_ROOT

This variable is stored in the value specified by the DocumentRoot command of the accessed WWW site.


Customer request variable

Apache provides many environment variables related to client requestors. The following are some of the most common variables that are selectively introduced.


SERVER_NAME

This variable tells the CGI program which host it accesses. This value can be an IP address or a complete host name;


HTTP_ACCEPT
This variable is assigned a list of MIME types acceptable to the customer, for example, HTTP_ACCEPT = image/gif;

HTTP_ACCEPT_CHARSET
This variable is assigned to a character set acceptable to the customer, for example:
HTTP_ACCEPT_CHARASET = iso-8859-1., *, UTF-8;

HTTP_ACCEPT_LANGUAGE
This variable is assigned the language acceptable to the customer, for example, HTTP_ACCEPT_LANGUAGE = en;

HTTP_ACCEPT_AGENT
This variable specifies the browser type and operating system in which the request is sent;

HTTP_PORT: the service port;

REMOTE_HOST: the IP address or IP name of the client;

REMOTE_PORT: the port number of the client;

7.4 tips

Over the past few years, the Gateway Interface (CGI) has become a de facto standard for server-side application development. However, over time, we found that many WWW server systems were not doing well under the heavy CGI burden. This is because the CGI specification has a bottleneck: whenever the customer system requests the CGI application, the WWW server must start a new CGI process until the process ends after the task is completed. This works normally when the load is not high. However, when the load is high, a large number of processes will become performance bottlenecks.

So now there are new standards to make up for this deficiency. FastCGI is a promising new open standard.

You can add the mod_fastcgi.c module to Apache to support FastCGI.
Related Article

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.