Setting up a Web server in Ubuntu Linux

Source: Internet
Author: User
Tags command line http request log mkdir php file root directory linux

Apache is a powerful Web server. Today, countless Apache servers running on Linux on the Internet are providing powerful support for the growing prosperity of the web world. This article will show readers how to quickly build an Apache Web server in an Ubuntu Linux system.

Although Ubuntu is a burgeoning Linux branch, the Ubuntu organization provides a wealth of support software for Apache, which can be obtained from the release CD or easily downloaded from the official site. As a result, Ubuntu is ideal for Web server platforms.

First, install Apache

Next, let's start by introducing how to install Apache. The specific installation command looks like this:

$ sudo apt-get install apache2

Then run Apache, and the command looks like this:

$ sudo/etc/init.d/apache2 Restart

Apache will create a new directory during installation:/var/www, which is the root directory where documents are stored in the server. All documents placed in this directory can be accessed as long as the IP address of the localhost/or machine is entered in the address bar of the browser.

Second, the installation of PHP

PHP is a popular server-side scripting language that is typically combined with MySQL or postgres to manage Web content, blogs, and forums. Here's how to install it, but it's also very simple to install, and the command looks like this:

$ sudo apt-get install LIBAPACHE2-MOD-PHP5

Restart Apache to load the module installed above:

$ sudo/etc/init.d/apache2 Restart

To verify that the PHP module is loaded correctly, we can create a php file and then try to access the file through the Web server. In addition, we know that PHP has built a phpinfo function that can give details of its environment. So we can also use the following command to check the work of PHP:

sudo sh-c "echo" >/var/www/info.php "

Then, type http://localhost/info.php in the browser address bar, and then enter, and you should be able to see a face and give the details of the PHP you just installed. It is important to note that if the browser does not display the page in this process, it prompts you to download the file, which means that Apache does not load the PHP module correctly. The solution is to add the following line of command to the/etc/apache2/apache2.conf or/etc/apache2/mods-enabled/php5.conf file:

AddType application/x-httpd-php. php. phtml. php3

After adding the above command line, in order to ensure that Apache re-read the configuration file shutdown, we can turn it off by following the command, and then start again:

$ sudo/etc/init.d/apache2 Stop

$ sudo/etc/init.d/apache2 Start

Third, configure dynamic virtual host

In general, we have multiple Web sites hosted on a Web server, and each site has its own virtual server. For Apache, it supports both a name-based virtual server and an ip-based server.

For ip-based virtual servers, each site has a separate IP address, which has the disadvantage of using too many IP addresses, but the IPV4 address is now depleted and is not recommended, and is usually used when SSL is required.

For a name-based virtual server, multiple Web sites share an IP address. In this case, you usually decide which site to send to by the HTTP request header. To do this, we need to create a separate configuration for each virtual server, naming the directory as the root of the Web site and the host. However, if this is the case, every time we add a new virtual server, we need to modify the Apache configuration and reboot, which is really annoying!

Happily, if you use dynamic virtual host technology, you can join a virtual host at any time without having to reconfigure or start Apache. This technique is to use a module called Vhost_alias. We can enable the module by establishing a symbolic link in the Apache2 enabled module directory, which is as follows:

$ sudo ln-s/etc/apache2/mods-available/vhost_alias.load \

/etc/apache2/mods-enabled/vhost_alias.load

To make Vhost_alias work, we also need to modify/etc/apache2/apache2.conf to turn off the regular name (canonical names), modify the configuration of the log file, and set the location for our virtual host. Here is an example:

Get host name #从 "Host:" Header

Usecanonicalname off

# This log format can extract the host name from the first field

Logformat "%V%h%l%u%t"%r "%s%b" Vcommon

Customlog/var/log/apache2/access_log Vcommon

# contains host names in the file name path of the return request

Virtualdocumentroot/var/www/vhosts/%0/web

Virtualscriptalias/var/www/vhosts/%0/cgi-bin

Next, create a directory that holds the virtual host, as follows:

$ sudo mkdir/var/www/vhosts

Create a new skeleton virtual server, as shown in the following command:

$ sudo mkdir-p/var/www/vhosts/skeleton/cgi-bin

$ sudo cp-a/var/www/apache2-default/var/www/vhosts/skeleton/web

Restart the apache2 so that the above configuration takes effect, as follows:

$ sudo/etc/init.d/apache2 Restart

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.