apache virtual host configuration

How to configure Apache’s virtual host

Apache’s virtual host is to run multiple websites on a single server, each virtual host can be bound to a separate domain name, for these domain names can be specified in a separate directory, access to these domain names, Apache will open the corresponding directory inside the thing. To configure Apache virtual hosting, you only need to modify the Apache configuration file. The English word for virtual host is virtualhost, so the file you use to configure your Apache for virtual hosting may be httpd.conf, or it may be a file related to the name virtualhost, such as vhost.conf, so you’ll need to judge for yourself which configuration file to use.

Tools/Materials

apache server

Modify the hosts file

1

No matter what system you use, windows, mac, there will be a hosts file on the computer, modify this file, you can change the ip address corresponding to the host name. For example, you have installed a Web development environment (MAMP or WAMP), after starting the environment, visit http://localhost你就会打开环境的默认的目录, the localhost is defined in the hosts file, which points to the ip address is 127.0.0.1, the ip address indicates that the local you are using this (For details, you can search for it:)

Modify this hosts file, we can manually specify any hostname to point to any ip address, of course, this setup only affects your own computer, for example, we can create a domain name, such as hello.com, so that the domain name to point to your computer, that is 127.0.0.1 This ip address. This way, if you request hello.com in your browser, it will open the directory you specified in the environment you built on your local computer. To test apache’s virtual hosting capabilities, you can create a few of these domains so that when you create the virtual host, you can have these domains open their own directories. Here, let’s go ahead and modify this hosts file.

1. Find the hosts file, Windows system is in Windows\System32\Drivers\etc this directory, Mac system is in /etc/hosts, you can use text editor software to open it.

2. In the hosts file, on a new line, first enter the ip address 127.0.0.1, then a space, and then enter a host name, which can be anything, such as hello.com, so that access to hello.com will open the site on your computer. Start another line and define another line, 127.0.0.1hola.com.

3. Save the modified hosts file, then start the local Web environment, open your browser, type hello.com in the address bar, and hola.com, and see if it opens up your local content. Now, what you see should be the same as if you had used http://localhost打开的是一样的东西.

END

Configuring Apache Virtual Hosting

Now that we’ve virtualized two domains so that they point to our local computers, and accessing them opens up the same content in the same directory, here’s an exercise in which we make accessing different domains open up different directories by modifying Apache’s configuration files. You first need to determine the configuration file to configure the Apache virtual host, generally in the httpd.conf this main configuration file, however, many environments will be put in a separate virtual host inside a configuration file, and then in the httpd.conf file to specify the file that contains this separate configuration of the virtual host.

1. Find the configuration file to configure virtual hosts, if you use WAMP to build the environment, first find and open httpd.conf, in this file, search for #Virtualhosts, it has a line of code below, #Includeconf/extra/httpd-vhosts.conf… You can remove the # in front of it, so that after Apache starts, it will also load the httpd-vhosts.conf configuration file, which has the advantage of keeping Apache-related settings tidy. Of course, you can set up virtual hosts in the main httpd.conf file.

2. Open the httpd-vhosts.conf file. If you don’t have it, you can create one, or configure a virtual host directly in the httpd.conf file. Configuring virtual hosts should look something like this:

3. There should be two pieces of code like this, each of which is configuring a virtual host, and you can copy one of them and paste it into the bottom of the file. Then modify this code to suit your needs. Understand its meaning, ServerAdmin is the website administrator’s email address, DocumentRoot is the main directory of the virtual host, that is, access to the virtual host to open the directory, the directory you can specify, it should be noted that you need to create this directory, and then go here to specify the location of this directory. serverName ServerName is the name of the host, such as the one we virtualized before hello.com, ServerAlias is the alias of the host, you can specify a different host name, and when accessing it, it will also open the contents of the virtual host’s directory. ErrorLog is the error log, that is, when an error occurs, it will log the error to which file. CustomLog is the log of virtual host access.

This way your own newly added virtual host should look something like this:

3

4. Open hello.com in a browser, and you will access the contents of the directory /Applications/MAMP/htdocs/hello, which is specified by DocumentRoot in the configuration of this virtual host. You can put things in this directory, such as a WordPress or Drupal site.

Note: When configuring virtual hosts, the first virtual host is the default, you need to keep, configure your own virtual host, you can start from the second configuration, that is, the second paragraph <VirtualHost> code.

Apache server tutorials to configure a virtual host of the operation of the tutorial

Methods/steps

1, just installed Apache can also come to set up. First find the Apache configuration file, httpd.conf. in the Apache installation directory under the conf folder.

2, Notepad to find the text shown below. Look for #Include

conf/extra/httpd-vhosts.conf

.

This is a feature of virtual hosting that is logged out. We want to enable the virtual hosting feature, so we removed the #. It changes to the image below.

3. Close the httpd.conf file and save it.

4. Find the extra folder in the conf folder and open it. Open the httpd-vhosts.conf file. This is specifically to configure the virtual features related to a configuration file, right.

5, in the httpd-vhosts.conf last part of the code. The code is shown below

6, the code in the red box is to add. Note that the case, to be exactly the same as in the red box, can not be wrong, as long as it is written wrong a letter, Apache error! Code added at the end of the file! There are parameters to be introduced, see the following chart

7, “d:/myweb2” that is, in the D disk myweb2 file is Apache’s home page. Enter localhost that is looking for the page here. In d:/myweb2 folder, in the creation of a new.html

8, the red box in the welcome page, the main page is what. As shown in the figure, that is, if the browser input localhost, then first look for the D disk myweb2 folder under the new.html, in is index.html, and so on. If you can’t find it, you get an error.

9, add the code, save. This configuration is basically complete, restart Apache. browser enter localhost

you can access the D disk myweb2 under the new.html file.

10, in myweb2 under the construction of a new2.html. enter http://localhost/new2.html即可访问到了

How to Configure Apache for Virtual Hosting

How to Configure Apache for Virtual Hosting

Read Step-by-Step

How to run multiple websites on top of a single Linux server using Apache, the easiest and fastest way.

Tools/materials

A Linux server

Methods/Steps

Installing Apache packages

yum-yinstallhttpd

(Here only the virtual hosting function of Apache is realized, the LAMP architecture also need to install php and mysqld-server and other packages)

Open Apache’s main configuration file

#vim/etc/httpd/conf/httpd.conf

Cancel the “#NameVirtualHost*:8” line start #comment

NameVirtualHost*:8

Add

<VirtualHost*:80>

ServerAdminwebmaster@example.com

DocumentRoot/ var/www/html/web1

ServerNameweb1.example.com

ErrorLoglogs/web1.example.com-error_log

CustomLoglogs/web1.example. com-access_logcommon

</VirtualHost>

<VirtualHost*:80>

ServerAdminwebmaster@example.com

DocumentRoot/var/www/html/web2

ServerNameweb2.example.com

ErrorLoglogs/web2.example.com-error_log

CustomLoglogs/ web2.example.com-access_logcommon

</VirtualHost>

ServerAdmin is the webmaster’s email

DocumentRoot is the path to where your website is stored

ServerName is the domain name of your website

ErrorLog, CustomLog are log files

DocumentRoot and ServerName must be different when adding multiple websites

Start Apache service

/etc/init.d/ httpdstart

chkconfighttpdon

Add port 80 to the firewall

#grep80/etc/sysconfig/iptables

-AINPUT-mstate –stateNEW-mtcp-ptcp- -dport80-jACCEPT

5

Add the domain name resolution record in the domain name management background, and then visit the domain name can jump to the corresponding website.

Note

The DocumentRoot parameter of the web host cannot be the same as the default site directory of Apache.