ダン・クァン・ミン Blog

はじめまして

Setup Apache2 Virtual Host on Ubuntu

Create the Directory Structure

The first step that we are going to take is to make a directory structure that will hold the site data that we will be serving to visitors.

sudo mkdir -p /var/www/example.local/public_html

Grant Permissions

Now we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directories, we can change the ownership by doing this:

sudo chown -R $USER:$USER /var/www/example.local/public_html

We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:

sudo chmod -R 755 /var/www

Create Demo Pages for Each Virtual Host

vim /var/www/example.local/public_html/index.html

In this file, create a simple HTML document that indicates the site it is connected to. My file looks

<html>
  <head>
    <title>Welcome to example.local!</title>
  </head>
  <body>
    <h1>Success!  The example.local virtual host is working!</h1>
  </body>
</html>

Enable mod rewrite(Optional)

sudo a2enmod rewrite
sudo service apache2 restart

Create New Virtual Host Files

Create the First Virtual Host File

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.local.conf

Open and Edit the file.

sudo vim /etc/apache2/sites-available/example.local.conf

Sample

<VirtualHost *:80>
    ServerAdmin admin@example.local
    ServerName example.local
    ServerAlias www.example.local
    DocumentRoot /var/www/example.local/public_html
    <Directory /var/www/example.local/public_html>
      AllowOverride all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Enable the New Virtual Host Files

We can use the a2ensite tool to enable each of our sites like this:

sudo a2ensite example.local.conf

When you are finished, you need to restart Apache to make these changes take effect:

sudo service apache2 restart

Set Up Local Hosts File (Optional)

If you haven’t been using actual domain names that you own to test this procedure and have been using some example domains instead, you can at least test the functionality of this process by temporarily modifying the hosts file on your local computer. If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:

sudo vim /etc/hosts

For the domains that I used in this guide, assuming that my VPS IP address is 111.111.111.111, I could add the following lines to the bottom of my hosts file:

127.0.0.1   localhost
127.0.1.1   guest-desktop
111.111.111.111 example.local

Test your Results

Now that you have your virtual hosts configured, you can test your setup easily by going to the domains that you configured in your web browser: http://example.local

Comments