Ubuntu set Apache Virtual host

Source: Internet
Author: User

A virtual host is often used for Web services that provide multiple domain names on a single IP address. This is useful if someone wants to run multiple Web sites on a single VPS with a single IP address. In this tutorial, let me show you how to set up a virtual host on Ubuntu 14.04 lts Apache Web server. Please note that this tutorial is for the 32-bit version of Ubuntu14.04 only.

I do not guarantee that it can also work in other lower Ubuntu versions or Ubuntu derivative versions (although the process may be similar).

Scheme

In this tutorial, I will use Ubuntu 14.04 32-bit LTS and build 2 test sites named "Unixmen1.local" and "unixmen2.local" respectively. My test machine is 192.168.1.250/ 24 and Server.unixmen.local. You can change the virtual domain name according to your needs.

Install Apache Web server

Before installing the Apache server, let's update our Ubuntu server:

    1. sudo apt-get update

Then, use the following command to install the Apache network server:

    1. sudo apt-get install apache2

After installing the Apache server, let us test whether the Web server is working properly through this URL http//Your server's IP address/

As you can see, the Apache server is already working.

Set up a virtual host

1. Create a virtual directory

Now, let's continue installing the virtual host. As I mentioned earlier, I'm going to create a new 2 virtual host named "Unixmen1.local" and "unixmen2.local", respectively.

Create a public folder to hold the data for both virtual hosts.

First, let's create a directory for the unixmen1.local site:

    1. sudo mkdir-p/var/www/unixmen1.local/public_html

Next, create a directory for the for unixmen2.local site:

    1. sudo mkdir-p/var/www/unixmen2.local/public_html

2. Setting up owners and permissions

The above directory now has only root permissions. We need to modify the ownership of these 2 directories to the average user, not just the root user.

    1. sudo chown-r $USER: $USER/var/www/unixmen1.local/public_html/
    2. sudo chown-r $USER: $USER/var/www/unixmen2.local/public_html/

The "$USER" variable points to the current logged-on user.

Set read and Write permissions to the Apache Web page root directory (/VAR/WWW) and its subdirectories so that everyone can read files from the directory.

    1. sudo chmod-r 755/var/www/

In this way, we create some folders to hold the network-related data and assign the necessary permissions and the owning user.

3. Create a sample page for a virtual host

Now, let's add a sample page to the site. In the first step, let's create a sample page for the virtual host unixmen1.local.

Create a sample page for the unixmen1.local virtual host,

    1. sudo vi/var/www/unixmen1.local/public_html/index.html

Add the following content:

    1. <title>www.unixmen1.local</title>
    2. <body>
    3. </body>

Save and close the file.

Similarly, add the sample page to the second virtual host.

    1. sudo vi/var/www/unixmen2.local/public_html/index.html

Add the following content:

    1. <title>www.unixmen2.local</title>
    2. <body>
    3. </body>

Save and close the file.

4. Create a virtual host configuration file

By default, Apache has a default virtual host file called 000-default.conf. We will copy the contents of the 000-default.conf file into our new virtual host configuration file.

    1. sudo cp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/unixmen1.local.conf
    2. sudo cp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/unixmen2.local.conf

Ensure that the end of the virtual host configuration file contains the. conf extension.

Now, modify the unximen1.local.conf file to meet your needs.

    1. sudo vi/etc/apache2/sites-available/unixmen1.local.conf

Make related changes directly in the UNIXMEN1 site (note: Comment lines that begin with "#" can be ignored.) )。

  1. <virtualhost *:80>
  2. # The SERVERNAME directive sets the request scheme, hostname and port that
  3. # The server uses to identify itself. This was used when creating
  4. # redirection URLs. In the context of the virtual hosts, the ServerName
  5. # specifies hostname must appear in the request ' s Host:header to
  6. # match this virtual host. For the default virtual host (this file) this
  7. # value was not decisive as it was used as a last resort host regardless.
  8. # However, must set it for any further virtual host explicitly.
  9. #ServerName www.example.com
  10. ServerAdmin [email protected]
  11. ServerName unixmen1.local
  12. Serveralias www.unixmen1.local
  13. Documentroot/var/www/unixmen1.local/public_html
  14. # Available Loglevels:trace8, ..., Trace1, debug, info, notice, warn,
  15. # error, Crit, alert, Emerg.
  16. # It's also possible to configure the LogLevel for particular
  17. # modules, e.g.
  18. #LogLevel Info Ssl:warn
  19. Errorlog ${apache_log_dir}/error.log
  20. Customlog ${apache_log_dir}/access.log combined
  21. # for more configuration files from conf-available/, which is
  22. # enabled or disabled at a global level, it's possible to
  23. # include a line for only one particular virtual host. For example the
  24. # following line enables the CGI configuration for this host only
  25. # after it had been globally disabled with "a2disconf".
  26. #Include conf-available/serve-cgi-bin.conf
  27. </VirtualHost>

Similarly, modify the second host file.

    1. sudo vi/etc/apache2/sites-available/unixmen2.local.conf

Make the related changes present at the Unixmen2 site.

  1. <virtualhost *:80>
  2. # The SERVERNAME directive sets the request scheme, hostname and port that
  3. # The server uses to identify itself. This was used when creating
  4. # redirection URLs. In the context of the virtual hosts, the ServerName
  5. # specifies hostname must appear in the request ' s Host:header to
  6. # match this virtual host. For the default virtual host (this file) this
  7. # value was not decisive as it was used as a last resort host regardless.
  8. # However, must set it for any further virtual host explicitly.
  9. #ServerName www.example.com
  10. ServerAdmin [email protected]
  11. ServerName unixmen2.local
  12. Serveralias www.unixmen2.local
  13. Documentroot/var/www/unixmen2.local/public_html
  14. # Available Loglevels:trace8, ..., Trace1, debug, info, notice, warn,
  15. # error, Crit, alert, Emerg.
  16. # It's also possible to configure the LogLevel for particular
  17. # modules, e.g.
  18. #LogLevel Info Ssl:warn
  19. Errorlog ${apache_log_dir}/error.log
  20. Customlog ${apache_log_dir}/access.log combined
  21. # for more configuration files from conf-available/, which is
  22. # enabled or disabled at a global level, it's possible to
  23. # include a line for only one particular virtual host. For example the
  24. # following line enables the CGI configuration for this host only
  25. # after it had been globally disabled with "a2disconf".
  26. #Include conf-available/serve-cgi-bin.conf
  27. </VirtualHost>

After you modify the virtual host file, disable the default virtual host configuration (000.default.conf), and then enable the new virtual host configuration as shown below.

    1. sudo a2dissite 000-default.conf
    2. sudo a2ensite unixmen1.local.conf
    3. sudo a2ensite unixmen2.local.conf

Finally, restart the Apache server.

    1. sudo service apache2 restart

That's it. Now we have successfully configured the Apach virtual host on our Ubuntu server

Testing a virtual Host

Edit/etc/hosts file,

    1. sudo vi/etc/hosts

Add the virtual domain name as shown below at the end of the file.

    1. 192.168.1.250 unixmen1.local
    2. 192.168.1.250 unixmen2.local

Save and close the file.

Open your browser and access http://unixmen1.local or http://unixmen2.local. You will see the sample page we created earlier.

Unixmen1.local test page:

Unixmen2.local test Page

If you want to access these sites from your remote system, you need to add the actual domain name records to your DNS server. However, I do not have a real domain name and DNS server, I just want to pass my local system test, then it just works as I wish.

cheers!

Ubuntu set Apache Virtual host

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.