Jumat, 22 April 2011

Instalation Apache

apt-get install apache2
The will install the Apache application and all dependent files. After the installation Apache starts automatically and is set to start automatically when the system is booted

If you just want to play around with a Web server and not have a domain name associated with it (like with a test Web server) you're pretty much done. You can verify Apache is working by going to another system on the same network as your Debian server and entering the address of your Debian server in the browser's address line, something like:


http://192.168.1.80

A page should appear with the words "It works!" at the top of the page. This is the default /var/www/index.html file that comes with the apache2 package. You can add your own pages into the /var/www directory.

You may have noticed that the open-ssl package also got intalled. This will allow you to play around with a Web server that has support for SSL (Secure Sockets Layer) for accepting secure transactions. However, since the servers we set up on these pages are not very secure, you wouldn't want to use this SSL server in a production environment. SSL will let you encrypt information that is submitted to the Web server, but if the underlying OS is not secure that sensitivie information can be stolen off the disk. You'd also need to get a certificate from an outfit like Thawte or Verisign in order to use the SSL capabilities of the Web server. (They offer temporary certificates for free that you can use if you want to play around with SSL.)

The main configuration file for Apache is the /etc/apache2/apache2.conf file. Like just about every Linux/UNIX configuration file, it is a plain old ASCII text file so you just use a text editor to make changes to it. Luckily, the defaults in the configuration file are pretty much what we need.

The way Debian's Apache 2.2 package sets things up makes it easy to add (and remove) Web sites (virtual hosts) to your server. The two directories you need to focus on are /etc/apache2/sites-available and /etc/apache2/sites-enabled and these two directories are specific to Debian (and Debian derivatives such as Ubuntu). Actually, the /etc/apache2/sites-enabled directory just contains sym links to the files in the /etc/apache2/sites-available directory. That's how you enable and disable sites, just add and remove sym links in the "-enabled" directory. (Sym links are like shortcuts in Windows.)

There's already a file called default in the /etc/apache2/sites-available directory. And there's already a sym link to it in the /etc/apache2/sites-enabled directory called 000-default which is a special name. The link name ensures it is read first at startup by Apache even when other sym links (for additional Web sites) are added to the directory.

A Single Web Site

If you want to set up a real Web server that hosts one Web site (one domain name) then you don't have to do much. That's because that only the one domain name will have a 'www' DNS record which points to this server. You may want to edit the default configuration file with the command:

nano /etc/apache2/sites-available/default

and enter your e-mail address in place of 'webmaster@localhost' on the line:

ServerAdmin webmaster@localhost

You may also want to add a line just beneath it with your domain name so it has something to use for the server signature. (However, as you'll see below, you may want to disable the server signature function.) Add the line something like:

ServerName www.your-last-name.net

Then press Ctrl-X to exit the editor and save the file. Now you can just load your HTML files and images into the /var/www directory.

Multiple Web Sites

If you want to host multiple Web sites on your server (i.e. use the Virtual Hosts feature of Apache) you'll need to set things up a little differently. In the following examples we're going to set up the following three Web sites: moe.com, larry.com, and curly.com. Because the 'www' DNS records for each of these domains are all going to point to the same IP address (and all use the same port 80) we'll be using Apache's name-based virtual hosts. With name-based virtual hosts, the browser includes the domain name in the HTTP request it sends to the IP address of the Web server. This is how Apache knows which Web site is being requested. Very early browsers didn't support name-based virtual hosts but all current browsers do.

The first thing you need to do is create a "document root" sub-directory for each Web site under the /var/www directory. In our example you would enter the following commands to do this:

mkdir /var/www/moe
mkdir /var/www/larry
mkdir /var/www/curly

Note that the sub-directory names don't have to match the domain names. It just makes it easier to follow if they are.

You also have to do the same for the CGI sub-directories.

mkdir /usr/lib/cgi-bin/moe
mkdir /usr/lib/cgi-bin/larry
mkdir /usr/lib/cgi-bin/curly

Each Web site needs it's own file in the /etc/apache2/sites-available directory and, to be enabled, it's own sym link in the /etc/apache2/sites-enabled directory. In each file you enter that site's "virtual host block."

Since we want to keep the '000-default' sym link and the 'default' file it links to, we'll recreate the 'default' file. So that we can go back to a clean slate later if we need to, we'll save original file by renaming it with the command:

mv /etc/apache2/sites-available/default /etc/apache2/sites-available/_default

Then create a new 'default' file with the command:

nano /etc/apache2/sites-available/default

and enter the following statements into it:

<VirtualHost *:80>
 ServerAdmin stooge@moe.com
 ServerName www.moe.com
 ServerAlias moe.com
 DocumentRoot /var/www/moe/
  # Set Document Root directory options
  <Directory />
    Options FollowSymLinks -Indexes Includes
    # Allow use of .htaccess file
    AllowOverride Limit FileInfo
  </Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/moe/
  # Set CGI-BIN directory options
  <Directory /cgi-bin>
    AllowOverride None
    Options +ExecCGI -Multiviews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
  </Directory>
 CustomLog /var/log/apache2/moe-access.log common
 ErrorLog /var/log/apache2/moe-error.log
</VirtualHost>

Press Ctrl-X to exit the editor saving the file. Create a copy of this file for our second site with the commands:

cd /etc/apache2/sites-available
cp default larry