Apache and PHP combined with Apache default virtual host

Source: Internet
Author: User

11.14-11.15 Apache and PHP combined

Apache (httpd) configuration file:/usr/local/apache2.4/conf/httpd.conf.

Configure Apache

Select the PHP to invoke

Because this machine has two versions of PHP installed, this configuration is required, and if you install only one PHP service, the existing PHP service will be called by default (PHP5 is selected here).

[Email protected] ~]# vim/usr/local/apache2.4/conf/httpd.conf
LoadModule Php5_module modules/libphp5.so
#LoadModule Php7_module modules/libphp7.so
Description: Specifies that Apache calls PHP5.

Physical machine: Enable telnet command in Win7

Graph LR
Start-up Control Panel
Control Panel---Programs
Programs--turn Windows features on or off
Turn Windows features on or off-select Telnet Client
For Apache configuration

To change the configuration file:
[Email protected] ~]# vim/usr/local/apache2.4/conf/httpd.conf
allowoverride None
Require all granted

Detect the configuration for syntax errors:
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl-t
Syntax OK

Reload Service:
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl Graceful
Note: This command does not cause the service to restart, but to load the contents of the configuration file.
Note: Change the "Require all denied" here to "Require all granted".

To add a rule that listens on port 80:

[[email protected] ~]# iptables-i input-p TCP--dport 80-j ACCEPT
Description: At this point, you can access the local virtual machine IP directly using your browser.


Add PHP Service

Authorizing the PHP service to parse the native server
[Email protected] ~]# vim/usr/local/apache2.4/conf/httpd.conf
AddType application/x-compress. Z
AddType application/x-gzip. gz. tgz
AddType application/x-httpd-php. php
Description: Add "AddType application/x-httpd-php. php" Here, meaning that the PHP service is allowed to parse the server.

Add Index page
[Email protected] ~]# vim/usr/local/apache2.4/conf/httpd.conf
<ifmodule dir_module>
DirectoryIndex index.html index.php

Detect, Load:
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl-t
Syntax OK
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl Graceful

Note: Add "index.php" here,

Detects if the server supports PHP parsing:

[Email protected] ~]# vim/usr/local/apache2.4/htdocs/1.php

Phpinfo ();
#该命令的含义是: When accessing this address, print the relevant information of PHP directly
Description: Create a PHP file and then use your browser to access the file (you do not need to restart the server at this time):


That is, normal access, indicating that the server supports PHP parsing, if you do not support PHP parsing, access to the file will show the source code.

Complete the PHP parsing configuration!

Common faults:

Failure: The server is unable to parse PHP!

Check if Apache has loaded the PHP module
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl-m
If it is not loaded, you need to check that there are no PHP modules in the system:

[Email protected] ~]# ls/usr/local/apache2.4/modules/libphp5.so
If the module exists but does not load, check that the Apache configuration file has no PHP configuration loaded:


Check the configuration file for the PHP Service load command:


Execute the command "/USR/LOCAL/APACHE2.4/BIN/APACHECTL-T" to check the configuration file for any syntax errors.

Check if there is an index page (the row has little impact):


Apache Configuration PHP7 Parsing

To edit a configuration file (call PHP7):
[Email protected] ~]# vim/usr/local/apache2.4/conf/httpd.conf
#LoadModule Php5_module modules/libphp5.so
LoadModule Php7_module modules/libphp7.so

Detect, Load:
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl-t
Syntax OK
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl Graceful
Note: Because other related configurations have been completed in the PHP5 parsing configuration, it is only necessary to change the PHP version to be called, and if the initial configuration, the operation is the same as PHP5.

Add: Shortcut key Ctrl+r

Ctrl+r use: Reverse-search and invoke executed commands. (Reverse-i-search).
How to use: Press CTRL+R after the command line into the "(Reverse-i-search)": "State, and then enter the used parameters, the corresponding command will be displayed directly, enter to execute the command.

11.16-11.17 Apache Default Virtual Host

Windows System hosts Management

The hosts location "C:\Windows\System32\drivers\etc\hosts" in the Windows system.
Open the file with Notepad:


Description: This allows you to customize the local IP and domain names to add temporary access addresses (used when DNS is not in effect).

Add a virtual Host

Adding " www.adai.com" to the Hosts file adds the local virtual host to the Windows system, which is automatically resolved to the local virtual host "ServerName www.example.com:80" when the domain name is accessed.

Linuxapache Virtual Host Management

The domain name "www.adai.com" accessed by the physical machine is not defined in the virtual machine Apache configuration file, only the "ServerName www.example.com:80" domain name is defined in the virtual machine, which is the default host for Apache. Any access to a domain name that is bound to the virtual machine's IP at this point will jump to that host.
Because a server can run multiple domain names, in order to facilitate management, the virtual host needs to be configured:

Configuring a virtual Host

To edit an Apache configuration file:
[Email protected] ~]# vim/usr/local/apache2.4/conf/httpd.conf

Virtual hosts

Include conf/extra/httpd-vhosts.conf
Description: Edit the "virtual hosts" parameter, remove #, enable the virtual host profile "Httpd-vhosts.conf".
Note: Apache also supports managing each virtual host separately, with the following configuration parameters:

[Email protected] ~]# vim/usr/local/apache2.4/conf/httpd.conf

Virtual hosts

Include conf/extra/vhosts/*.conf
[Email protected] ~]# mkdir/usr/local/apache/conf/extra/vhosts
You can then create a virtual host in the/usr/local/apache/conf/extra/vhosts directory.

Edit a virtual host configuration file
[Email protected] ~]# vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
Under this profile, each group of "< VirtualHost *:80 >" represents a host (that is, a Web site):


Make the following edits:

<virtualhost *:80>
DocumentRoot "/data/wwwroot/abc.com"
ServerName abc.com
Serveralias www.abc.com www.123.com
#设置别名 (can be set more than one)
Errorlog "Logs/abc.com-error_log"
Customlog "Logs/abc.com-error_log" common

<virtualhost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
Serveralias www.example.com
Errorlog "Logs/111.com-error_log"
Customlog "Logs/111.com-access_log" common

At this point, a Web site is defined, the core parameters of the definition site are: documentroot (site root), ServerName (domain name).
Note: After the virtual host is in effect, the default host (www.example.com ~ in the original Apache configuration file is invalidated (this is verified with www.example.com ~ 111.com).

Add the appropriate directory for the virtual host

[Email protected] ~]# mkdir/data/wwwroot/
[Email protected] ~]# mkdir/data/wwwroot/abc.com
[Email protected] ~]# mkdir/data/wwwroot/111.com
To create a PHP file in the appropriate directory:

[Email protected] ~]# vim/data/wwwroot/abc.com/index.php
echo "Welcome to ABC.com";

[Email protected] ~]# vim/data/wwwroot/111.com/index.php
echo "Welcome to 111.com";
To detect and reload the Apache configuration file:

[Email protected] ~]#/usr/local/apache2.4/bin/apachectl-t
Syntax OK
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl Graceful
Virtual Host Testing

Use the browser and Curl command here to test separately.

If you do not have a local domain name configuration (hosts) in the virtual machine, you will have access to the extranet when you test the virtual host domain name with the ping command.

[[email protected] ~]# Ping www.abc.com
PING abc.com ( bytes of data.
If you want to point to the local virtual machine IP when you access "abc.com", you can specify the domain name in the/etc/hosts file or access it using the Curl command, as follows:


Curl-x Specifying Access ports
[Email protected] ~]# curl-x192.168.8.131:80 abc.com
Welcom to ABC.com
Note: Accessing all undefined domain names at this point will point the access address to the default virtual host, because the virtual host has been customized, so the default virtual host at this time is the first virtual host in profile "Httpd-vhosts.conf" (abc.com here), the effect is as follows:

[Email protected] ~]# curl-x192.168.8.131:80 www.abcde.com
Welcom to ABC.com

Apache and PHP combined with Apache default virtual host

Related Article

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.