Ubuntu | | LinuxMint Configuring the Apache virtual Host

Source: Internet
Author: User

2015-09-15

Article transferred from https://linux.cn/article-3164-1.html

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.

4. 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. Welcome to unixmen1.local website
    4. </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. Welcome to unixmen2.local website
    4. </body>

Save and close the file.

5. 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< Span class= "pun" >-default./etc/apache2/< Span class= "PLN" >sites-available/ Unixmen1.. conf
  2. sudo cp /etc/apache2/sites-available/$- 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 -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.

Ubuntu | | LinuxMint Configuring the 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.