Preface:
Scenario One: Usually when we develop, the general items are only stored in the root directory specified by localhost, when there are several items, only in the root directory under different folders, especially inconvenient.
Scene two: At ordinary times when watching teaching video, always see those teachers in a single computer under the use of different domain names, access is the local project code, whenever this time I will ask, this how to achieve.
Scenario Three: During a recent internship interview, the interviewer asked me how to build multiple sites on the Apache server. I can only answer that I won't.
In order to solve the above several problems, I decided to the Apache multi-site configuration This knowledge point to understand. The key is to understand the Apache virtual host, Apache virtual host is a kind of allow on the same machine, running more than one Web site solution. The following is the Apache virtual host launched.
In this blog I will introduce two ways to implement the Apache multi-site configuration:
1, how to configure the domain name based on access to distinguish the configuration of the site.
(for example, access to www.yershop.com is a/home/www/yershop project, access to www.wordpress.com is/home/www/wordpress project)
2. In the case of the same domain name address, how to obtain different sites by accessing different ports.
(for example, access to www.yershop.com is a/home/www/yershop project, and access to www.yershop.com:8080 is/home/www/yershop_admin project (that is, yershop Background management System) one: differentiate the site based on the domain name of the access
Here I introduce the configuration under the Ubuntu system to discuss the CentOS (because I am in the learning process is also first Ubuntu, and then configure the CentOS is referred to the Ubuntu configuration) in Ubuntu environment:
My environment is:
Operating system: Ubnutu 16.04 LTS
Apache Service: apache/2.4.18 (Ubuntu) (obtained using the APACHE2-V command)
On the Ubnutu, the Apache service is called Apache2, not httpd (httpd on Centos), and the main configuration file is/etc/apache2/apache2.conf, we open/etc/apache2/ Apache2.conf, found the last two acts:
# Include The virtual host configurations:
includeoptional sites-enabled/*.conf
Obviously, the configuration of the virtual site (virtualhost) is stored under/etc/apache2/sites-enabled. After viewing, initially, the directory contains a symbolic connection (soft link, equivalent to a shortcut):
000-default.conf->. /sites-available/000-default.conf
This leads to another configuration directory:/etc/apache2/sites-available. This directory places the True profile of all available sites, and for Enabled sites, Apache2 a symbolic link to the sites-available directory file in the sites-enabled directory.
There are two files under/etc/apache2/sites-available:
000-default.conf default-ssl.conf
/etc/apache2/sites-enabled/000-default.conf linked to the file is/etc/apache2/sites-available/000-default.conf, we will/etc/ apache2/sites-available/000-default.conf file For example, look at the configuration of a virtualhost (for simplicity, all the comments I have removed):
<virtualhost *:80>
ServerAdmin webmaster@localhost
documentroot/var/www/html
ErrorLog Log_dir}/error.log
customlog ${apache_log_dir}/access.log combined
</VirtualHost>
The documentroot is the root of this site, so the website configuration available in/etc/apache2/sites-enabled can be scanned and loaded when the Apache2 is started. When the user accesses the localhost:80, APACHE2 returns the index.html (or index.php, configurable) under the root directory/var/www/html of the default site to the browser as a response, and you will enjoy the/var/ Www/html/index.html the contents of this file.
Apache2 default site Let's not move it. We add the site configuration to meet our requirements.
Step One: Add the site configuration file
Create two-site profiles www-yershop-com.conf and www-wordpress-com.conf in the/etc/apache2/sites-available directory:
#进入虚拟主机配置文件夹
cd/etc/apache2/sites-available/
#复制默认的虚拟主机配置文件
sudo cp 000-default.conf www-yershop-com.conf
sudo cp 000-default.conf www-wordpress-com.conf
Edit the two profiles, taking www-yershop-com.conf as an example:
# Created by Zhongjin on 2016-12-12 Winter solstice
<virtualhost *:80>
ServerAdmin
1054840542@qq.com ServerName www.yershop.com
documentroot/home/www/yershop
<directory "/home/www/yershop" >
options FollowSymLinks
allowoverride All
#Require all denied
Require all granted
</Directory>
ErrorLog ${apache_log_dir}/error.log
Customlog ${apache_log_dir}/access.log Combined
</VirtualHost>
Note the configuration above: ServerName, DocumentRoot, and Directory are our key focus points. The ServerName of Yershop is www.yershop.com and the root directory is/home/www/yershop,directory with DocumentRoot.
Change the corresponding configuration for www-wordpress-com.conf, that is, modify ServerName for Www.wordpress.com,DocumentRoot and Directory to/home/www/wordpress.
Step Two: Create a symbolic link in the sites-enabled directory
cd/etc/apache2/sites-enabled
#建立对应的软链接
sudo ln-s/etc/apache2/sites-available/www-yershop-com.conf/etc/ apache2/sites-enabled/www-yershop-com.conf
sudo ln-s/etc/apache2/sites-available/www-wordpress-com.conf/etc /apache2/sites-enabled/www-wordpress-com.conf
Step Three: Put the project code in the corresponding directory
As shown above, our Yershop project code is placed in the/home/www/yershop,wordpress project code in/home/www/wordpress, then we create the corresponding directory in the corresponding directory and give the appropriate permissions, take Yershop as an example:
sudo mkdir-p/home/www/yershop
Then put the project code under/home/www/yershop, and here for the test, create a new index.php, as follows:
<?php
echo "hello,welcome to yershop!";
Give the appropriate permissions:
sudo chmod-r 777/home/www/yershop
Do the same things for WordPress.
Step Fourth: Modify the/etc/hosts file
Open the View/etc/hosts file, beginning with:
127.0.0.1 localhost
We added after the line:
# Modified by Zhongjin on 2016-12-21 Winter solstice
127.0.0.1 www.yershop.com 127.0.0.1 www.wordpress.com
# Save exit
Step Fifth: Reboot the Apache server and test
Reboot the Apache server to make the configuration effective:
Systemctl Restart Apache2.service
Access www.yershop.com or www.wordpress.com in the browser (if it is a desktop version) to see if the content in the index.php file is exported.
If you are not a desktop version (server), you can use the command line test:
Curl www.yershop.com
See if the returned string is correctly exported. under Centos Environment:
My environment is:
Operating system: Centos 7
Apache Service: apache/2.4.6 (CentOS) (obtained via HTTPD-V)
Here we achieve the same effect in the Ubuntu environment.
Centos, the Apache service is called httpd, the main configuration file is/etc/httpd/conf/httpd.conf, we browse httpd.conf file, search keyword Vhost, find that there is no relevant things, However, the last two lines have the following:
# Load config files in the '/etc/httpd/conf.d ' directory, if any.
Includeoptional conf.d/*.conf
The configuration of the virtual host is also under/ETC/HTTPD/CONF.D. LZ went to look at the contents of all the files below the directory, found that there is no configuration of the virtual host, if you carefully observe the http.conf configuration file, you will find that, in fact, in Centos, its default host only localhost ==>/var/www/html , if necessary, we have to expand ourselves.
We have two ways to implement our own extensions, one is to append the configuration content directly behind the/etc/httpd/conf/httpd.conf, one is to configure the external file first, and then introduce our configuration like Includeoptional conf.d/*.conf. It is not generally recommended to modify the main configuration file directly, so we use the second method.
We still imitate the above configuration:
Step One: Add the site configuration file
CD/ETC/HTTPD
sudo mkdir vhost-conf.d
We add our virtual host Profile www-yershop-com.conf and www-wordpress-com.conf to the VHOST-CONF.D directory, for example, for www-yershop-com.conf, we enter the following:
<virtualhost *:80>
#Created by Zhongjin on 2016-12-21 Winter solstice
Serveradmin 1054840542@qq.com ServerName
www.yershop.com
documentroot/home/www/yershop
<directory "/home/www/yershop" >
options FollowSymLinks
allowoverride All
#Require all denied
Require all granted
</Directory>
</VirtualHost>
Change the corresponding configuration for www-wordpress-com.conf, that is, modify ServerName for Www.wordpress.com,DocumentRoot and Directory to/home/www/wordpress.
Step Two: Introduce our virtual host configuration in the master configuration
sudo vim/etc/httpd/conf/httpd.conf
# Add the following at the end of the main configuration file
# Load vhost-config files in the '/ETC/HTTPD/VHOST-CONF.D ' directory if any
# created by Zhongjin on 2016-12-21 Winter solstice
Include vhost-conf.d/*.conf
Step Three: Put the project code in the corresponding directory
Direct reference to the third step configuration under Ubnutu environment.
Step Fourth: Modify the/etc/hosts file
Direct reference to the fourth step configuration in the Ubnutu environment.
Step Fifth: Reboot the Apache server and test
Reboot the Apache server to make the configuration effective:
sudo systemctl restart Httpd.service
Access www.yershop.com or www.wordpress.com in the browser (if it is a desktop version) to see if the content in the index.php file is exported.
If you are not a desktop version (server), you can use the command line test:
Curl www.yershop.com
See if the returned string is correctly exported.
Note:
Under Centos, because the default virtual host configuration (localhost) is configured directly in the/etc/httpd/conf/httpd.conf file, we add our configuration later, overwriting the same configuration as before, so when you curl localhost, it is possible to access the code under the Yershop project, or the code under WordPress.
My solution is: Follow the steps above to rebuild a localhost domain name so that it points to the/var/www/html directory. second, under the same domain name, through access to different ports to get different sites
In fact, the steps to achieve here is not much different from the above, I said that need to do the extra operation.
This takes yershop as an example, by accessing www.yershop.com:80 (default is 80), returning "Hello,welcome to Yershop", and accessing www.yershop.com:8080, returning "Hello, Welcome to Yershop Background Management ". Ubuntu Environment: Let our apache2 listen to port 8080:
Modify the/etc/apache2/ports.conf file and add two lines after Listen 80:
Namevirtualhost *:8080
Listen 8080
Add www-yershop-com-8080.conf under/etc/apache2/sites-available/and create symbolic links under/etc/apache2/sites-enabled/. Method Reference front.
The main configuration of www-yershop-com-8080.conf is as follows:
<virtualhost *:8080>
#Created by Zhongjin on 2016-12-21 Winter solstice
Serveradmin 1054840542@qq.com ServerName
www.yershop.com
documentroot/home/www/yershop_admin
<directory "/home/www/yershop_admin" >
Options followsymlinks
allowoverride all
#Require denied
Require all granted
</Directory>
</VirtualHost>
We must see clearly need to change the place, virtualhost change to 8080,documentroot and Directory are changed to/home/www/yershop_admin, but ServerName do not have to change, or with Www.yershop.com
Create directory/home/www/yershop_admin, add file index.php, output "hello,welcome to Yershop background management".
Reboot the Apache server and test the Centos environment: Let httpd listen to port 8080:
Modify the/etc/httpd/conf/httpd.conf configuration file directly and add two lines after Listen 80:
# Created by Zhongjin on 2016-12-21 Winter solstice
namevirtualhost *:8080
Listen 8080
Add www-yershop-com-8080.conf under/ETC/HTTPD/VHOST-CONF.D
Copy the www-yershop-com.conf file directly, rename it to www-yershop-com-8080.conf, and modify it to:
<virtualhost *:8080>
#Created by Zhongjin on 2016-12-21 Winter solstice
Serveradmin 1054840542@qq.com ServerName
www.yershop.com
documentroot/home/www/yershop_admin
<directory "/home/www/yershop_admin" >
Options followsymlinks
allowoverride all
#Require denied
Require all granted
</Directory>
</VirtualHost>
Attention to the modification of the place.
Create directory/home/www/yershop_admin, add file index.php, output "hello,welcome to Yershop background management".
Reboot the Apache server and test something:
1, multi-site configuration There is another configuration, that is, through a variety of IP multiple site configuration, because I did not carry out the experiment (the previous content is LZ personally experienced), so here is not given a method.
2, because there is no windows machine on hand, so can not test Apache configuration in the Windows environment, but the method and need to operate the file is almost the same, you can refer to "Apache--Multiple virtual host multi-site configuration of the two implementation plan" try.
3, this blog reference from the "Use Apache2 Configure multiple Sites", "CentOS7 Configure Apache multi-site virtualhost"