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:
- sudo mkdir -p /var/www/unixmen1. Local/public_html
Next, create a directory for the for unixmen2.local site:
- 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.
- sudo chown-r $USER : $USER /var/www/unixmen1 local/public_html/
- 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.
- 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,
- sudo VI/ var/www/unixmen1. Local/public_html/index. HTML
Add the following content:
- <title>www.unixmen1.local</title>
- <body>
- Welcome to unixmen1.local website
- </body>
Save and close the file.
Similarly, add the sample page to the second virtual host.
- sudo VI/ var/www/unixmen2. Local/public_html/index. HTML
Add the following content:
- <title>www.unixmen2.local</title>
- <body>
- Welcome to unixmen2.local website
- </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.
- sudo cp/etc/apache2/ sites-available/000< Span class= "pun" >-default./etc/apache2/< Span class= "PLN" >sites-available/ Unixmen1.. conf
- 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.
- 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.) )。
- <virtualhost *:80>
- # The SERVERNAME directive sets the request scheme, hostname and port that
- # The server uses to identify itself. This was used when creating
- # redirection URLs. In the context of the virtual hosts, the ServerName
- # specifies hostname must appear in the request ' s Host:header to
- # match this virtual host. For the default virtual host (this file) this
- # value was not decisive as it was used as a last resort host regardless.
- # However, must set it for any further virtual host explicitly.
- #ServerName www.example.com
- ServerAdmin [email protected]
- ServerName unixmen1.local
- Serveralias www.unixmen1.local
- Documentroot/var/www/unixmen1.local/public_html
- # Available Loglevels:trace8, ..., Trace1, debug, info, notice, warn,
- # error, Crit, alert, Emerg.
- # It's also possible to configure the LogLevel for particular
- # modules, e.g.
- #LogLevel Info Ssl:warn
- Errorlog ${apache_log_dir}/error.log
- Customlog ${apache_log_dir}/access.log combined
- # for more configuration files from conf-available/, which is
- # enabled or disabled at a global level, it's possible to
- # include a line for only one particular virtual host. For example the
- # following line enables the CGI configuration for this host only
- # after it had been globally disabled with "a2disconf".
- #Include conf-available/serve-cgi-bin.conf
- </VirtualHost>
Similarly, modify the second host file.
- sudo vi /etc/apache2/sites-available/unixmen2. Local. conf
Make the related changes present at the Unixmen2 site.
- <virtualhost *:80>
- # The SERVERNAME directive sets the request scheme, hostname and port that
- # The server uses to identify itself. This was used when creating
- # redirection URLs. In the context of the virtual hosts, the ServerName
- # specifies hostname must appear in the request ' s Host:header to
- # match this virtual host. For the default virtual host (this file) this
- # value was not decisive as it was used as a last resort host regardless.
- # However, must set it for any further virtual host explicitly.
- #ServerName www.example.com
- ServerAdmin [email protected]
- ServerName unixmen2.local
- Serveralias www.unixmen2.local
- Documentroot/var/www/unixmen2.local/public_html
- # Available Loglevels:trace8, ..., Trace1, debug, info, notice, warn,
- # error, Crit, alert, Emerg.
- # It's also possible to configure the LogLevel for particular
- # modules, e.g.
- #LogLevel Info Ssl:warn
- Errorlog ${apache_log_dir}/error.log
- Customlog ${apache_log_dir}/access.log combined
- # for more configuration files from conf-available/, which is
- # enabled or disabled at a global level, it's possible to
- # include a line for only one particular virtual host. For example the
- # following line enables the CGI configuration for this host only
- # after it had been globally disabled with "a2disconf".
- #Include conf-available/serve-cgi-bin.conf
- </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.
- sudo a2dissite -default. conf
- sudo a2ensite unixmen1. Local. conf
- sudo a2ensite unixmen2. Local. conf
Finally, restart the Apache server.
- 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,
- sudo vi /etc/hosts
Add the virtual domain name as shown below at the end of the file.
- 192.168. 1.250 unixmen1. Local
- 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