4.12 Tasks Apache combined with PHP and Apache default virtual host

Source: Internet
Author: User
Tags fully qualified domain name

Apache and PHP Combine
虽然我们现在已经安装好了apache,mysql,php。但是我们还没有让他们结合起来,虽然apache调用了php作为它的一个模块,但我们还不知道他是否能解析php。这时我们还需要去编辑一下httpd的配置文件。它的配置文件是/usr/local/apache的全名名字/conf/httpd.conf

[[email protected] ~]# vim/usr/local/apache2.4/conf/httpd.conf
Here we need to edit a few places
1.ServerName
When we restart httpd, we often come across a hint
[[email protected] ~]#/usr/local/apache2.4/bin/apachectl restart
ah00558:httpd : Could not reliably determine the server ' s fully qualified domain name, using FE80::d f71:e49:bd9a:8b3. Set the ' ServerName ' directive globally to suppress this message
appears to be an error at first glance, but it is actually a warning, looking very awkward, we want to delete him. We can search for the
ServerName and then delete the "#" number #servername www.example.com:80. After the
completes we restart httpd, and then another error message appears
[[email protected] ~]#/usr/local/apache2.4/bin/apachectl start
/usr/ Local/apache2.4/bin/apachectl: Line 79:1399 error $HTTPD-K $ARGV
This is because we have just added two PHP here, we just need to add a "#" in front of one of them to comment out.
LoadModule php5_module modules/libphp5.so
#loadmodule php7_module modules/libphp7.so
and then we restart httpd, There will be no hint.

2.Require all denied, if we do not change this line, we are not access to the Linux IP, he will prompt us to check the server and firewall, if we encounter such a problem, we should first check whether our IP is unblocked. Then we check if Port 80 is unblocked. First enter the DOS mode in Windows to see if 80 port is open, if there is no command to find after entering DOS, we go to the Windows Control Panel, enter the program, then click Programs and features, then click on or turn off Windows features, Click on the block in front of telnet, install the Telnet client, and then enter the TELNET+IP address + port number in DOS to view, as long as the DOS interface without any display, it proves that our 80 port pass. If it does not, it shows that it is connected all the time. We can also use IPTABS-NVL to see if Port 80 is open.
[[email protected] ~]# IPTABLES-NVL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
Pkts bytes Target Prot Opt in Out source destination
2614 228K ACCEPT All-- 0.0.0.0/0 0.0.0.0/0 State related,established
1 60 Accept ICMP-- 0.0.0.0/0 0.0.0.0/0
0 0 Accept All--Lo 0.0.0.0/0 0.0.0.0/0
1 ACCEPT TCP-- 0.0.0.0/0 0.0.0.0/0 State NEW tcp dpt:22
2370 2330K REJECT All--
* 0.0.0.0/0 0.0.0.0/0 reject-with ICMP -host-prohibited

Chain FORWARD (Policy ACCEPT 0 packets, 0 bytes)
Pkts bytes Target prot opt in Out source destination
0 0 REJECT All-- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (Policy ACCEPT 2143 packets, 336K bytes)
Pkts bytes Target prot opt in Out source destination
The result is not open, we can first use a command to open 80 port.
Iptables-i input-p TCP--dport 80-j ACCEPT
Then go to the Windows browser to enter the IP of our virtual machine, this time can be connected.

Sometimes our browser is connected, but it shows 403, because in our configuration file there is a
Require all denied, we want to change the denied to granted, there are two in the configuration file, you have to change.
We have to change the configuration first to check its syntax, such as we want to check the Apache configuration file syntax, you can enter
/usr/local/apache2.4/bin/apachectl-t//-t is the parameter of the test syntax
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl-t
Syntax OK (This prompt proves that the syntax is correct)
Then reload the configuration file, add graceful at the end of the file, and reload.

3. Add a line and PHP-related configuration
AddType application/x-httpd-php. php
First we search AddType
And then in AddType application/x-compress. Z and AddType application/x-gzip. GZ tgz Add a line below, AddType application/x-httpd-php. PHP, so you can parse PHP.

4. Add an index page
First go to the config file to edit and then find # directoryindex:sets The file that Apache will serve if a directory,
Then replace it with the previous file, DirectoryIndex index.html index.php

Then we verify that Apache can verify PHP, write a test script first
vim/usr/local/apache2.4/htodcs/1.php
The contents are as follows
<?php
Phpinfo ();
?>
Then we re-enter our IP in the browser followed by/1.php, if the page appears, it proves successful, but if the source code, it proves that our Apache configuration file error. Once something goes wrong, we need to examine one aspect.
1. See if the loading module is loaded with PHP
/usr/local/apache2.4/bin/httpd-m
But if there is no PHP module found, we have to check if there is no libphp5.so this file.
2. Find the Libphp5.so file
[Email protected] htdocs]# ls/usr/local/apache2.4/modules/libphp5.so
/usr/local/apache2.4/modules/libphp5.so
If there is, but not loaded, we're going to have to go to the config file and have it loaded
LoadModule Php5_module modules/libphp5.so
If it's loaded, we're going to see if he has AddType application/x-httpd-php. php
3. Find AddType application/x-httpd-php. php
It is important to note that in the final. PHP must add a space before, without spaces, grammar check will be error.
After checking these steps, we can parse our PHP.
We check that after modifying the configuration file must do a syntax detection, and then graceful loading, or the site will be wrong.

Anache Default Virtual Host

A single server can access multiple Web sites, each of which is a virtual host.
Concept: Domain name (hostname), DNS, resolving domain name, hosts
Virtual Host: Any domain name, pointing to this IP, he will access to this site, this site is called the default virtual host.

Windows Write hosts
This file is stored in the location under Windiws/system32/drives/etc, open with Notepad, in for example: Next write the IP, then write the website, save exit. The site will then point to the specified IP.

Open the Apache configuration file
Vim/usr/local/apache2.4/conf/httpd.conf
Find Extra, find # Virtual hosts
Then remove the "#" in front of the include conf/extra/httpd-vhosts.conf.
#Virtual hosts
Include conf/extra/httpd-vhosts.conf
Save exit.

Then we open a level two configuration file, this level two configuration file is the virtual host configuration file. In this configuration file we can configure multiple servename. They all start with <virtualhost :80>, End with </VirtualHost>
Virtual Host configuration file: vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<virtualhost
:80>
ServerAdmin [email protected]
(admin mailbox, can be deleted)

DocumentRoot "/usr/local/apache2.4/docs/dummy-host.example.com"(定义网站的根目录)我们可以修改为 DocumentRoot "/data/wwwroot/123.com",这还是要根据我们的实际网站名需求去改。ServerName dummy-host.example.com

(server name) can be changed to 123.com. The website name can be changed according to the actual situation.

Serveralias www.dummy-host.example.com
(server alias) www.123.com www.abc.com can be modified to the site name according to the actual situation, and can write more than one.

ErrorLog "logs/dummy-host.example.com-error_log"错误日志,可根据实际情况改名。CustomLog "logs/dummy-host.example.com-access_log" common访问日志,可根据实际情况修改。

</VirtualHost>
A one-to-two appearance, which represents a virtual host. A host is a Web site.
Defining a site at its core is documentroot and ServerName. Serveralias can write multiple, but servername can only write one.

When we're done, we'll create the corresponding directory.
[Email protected] ~]# mkdir/data/wwwroot/
[Email protected] ~]# mkdir/data/wwwroot/123.com
[Email protected] ~]# mkdir/data/wwwroot/111.com
Can be modified according to the website name

Then create a index.php in the corresponding site root directory
[Email protected] ~]# vim/data/wwwroot/123.com/index.php
And then edit it in vim.
<?php
echo "123.com";
Then save the exit.

[Email protected] ~]# vim/data/wwwroot/111.com/index.php
<?php
echo "111.com";
Save exit

Then check the syntax for any errors
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl-t
Ah00526:syntax error on line of/usr/local/apache2.4/conf/extra/httpd-vhosts.conf:
ServerName takes one argument, the hostname and port of the server
There was an error in the 33rd line. After entering the view, it was found that a website name was written more. So to delete one.
When you are finished modifying, reload.

We can use Curl-x to test, if we go directly to ping domain name, will ping directly to the outside network,
[[email protected] ~]# Ping 123.com
PING 123.com (61.132.13.130) bytes of data.
Bytes from 61.132.13.130 (61.132.13.130): icmp_seq=1 ttl=116 time=44.4 ms
Bytes from 61.132.13.130 (61.132.13.130): icmp_seq=2 ttl=116 time=44.3 ms
Bytes from 61.132.13.130 (61.132.13.130): icmp_seq=3 ttl=116 time=44.5 ms

Then we use Curl-x.
[Email protected] ~]# curl-x192.168.1.106:80 www.llt.com
123.com[[email protected] ~]# curl-x192.168.1.106:80 www.789.com
123.com[[email protected] ~]# curl-x192.168.1.106:80 www.123.com
123.com[[email protected] ~]# curl-x192.168.1.106:80 www.1111.com
123.com[[email protected] ~]# curl-x192.168.1.106:80 www.111.com
At this point, we can see no matter what website, now will be assigned to 123.com.
We enter the URL, it will search all the configuration segments, if the configuration segment does not exist, then it will automatically jump to our virtual host. (The virtual host here is 123.com)

Conclusion
1. To take the virtual host into effect, first find the virtual host in the Apache configuration file, the virtual host name is # virtual hosts
Include conf/extra/httpd-vhosts.conf, and remove the previous "#". The virtual host configuration file is now in effect.
2. Each virtual host has multiple virtualhost, each virtualhost has a corresponding servername and DocumentRoot, each virtualhost has a site, each of which represents a virtual host.
3. There is a special on the virtual host, called the virtual default host, the default virtual host is no matter what domain name resolves to this IP, it will go to access this default virtual host.

4.12 Tasks Apache combined with PHP and Apache default virtual host

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.