PHP Learning notes-Apache settings

Source: Internet
Author: User
Tags php language multisite

Apache Configuration

1, first create a new own amp directory (imitate wampserver installation directory), the future apache,mysql,php are placed in this directory.

2, download Apache according to their own system download the corresponding compression package, my 64-bit system download is: "Httpd-2.4.23-x64-vc14.zip"

3. ' Unzip the downloaded Apache tarball into the amp directory. Note that the folder name after the decompression package is Apache24 (because my Apache is version 2.4), rename the folder to Apache and copy it to the amp directory

4, open the command prompt tool as an administrator (directly run CMD Open will encounter permissions issues), enter the E:\amp\Apache\bin directory, and then run httpd-k install Apache Service installation, you will see "Installing HT ' Apache2.4 ' service the ' Apache2.4 ' service is successfully installed. " "Testing httdp.conf ..." If you encounter "Errors reported here must is corrected before the service can be started." Don't worry, we're going to start the Apache service manually: Double-click the "ApacheMonitor.exe" file in the Amp\apache\bin directory and tap start to start the service.

If startup fails, you can use ' httpd-t ' to see why the startup failed

5, our site files are placed in the Amp\apache\htdocs folder, in the browser input localhost enter, you can see the Apache page, modify the Htdocs directory index.html file, refresh can see the changes in the page.

6. Configure Apache to run PHP programs

The first step: Loading PHP "module", "module" refers to the PHP language pack, that is, we download the extracted php file directory. Rename this directory to "PHP" to copy to the amp directory (and the Apache directory sibling) and open the Apache configuration file httpd.conf, which is in the amp\apache\conf directory. Insert the following code in the configuration file:

LoadModule php_module "E:/amp/php/php7apache2_4.dll" <  "\.php$\">setHandler application/x-httpd-php</FilesMatch  >
该配置文件的代码作用是,告知appache,凡是带.php后缀的文件都要找php语言模块处理,"php7apache2_4.dll"文件在amp\php目录下还有另一种配置方式:AddType application/x-httpd-php .php .pap .phtml,这些.pap .phtml 是我们自定义的文件后缀,同样也可以用php模块处理

7. Configuration of PHP
Time zone configuration
PHP configuration file needs to be modified, there are two configuration files in PHP directory: Php.ini-development and Php.ini-production, We copy the php.ini-development and rename it to php.ini to the current directory, open php.ini, find the "timezone" keyword, remove the timezone comment, and change to

PRC represents PRC
Then set the php.ini file path in the httpd.conf configuration file and add the following code

Phpinidir = "e:/amp/php"

Note: Do not add the php.ini file name, only the directory

8.PHP Configuring the MySQL module
In the php.ini file, set the path to the MySQL module file (find the Extension-dir by looking for it, the location under Windows), and modify it to point to the full physical path to the ext directory in the PHP language pack, with the following code:

Extension_dir = "E:/amp/php/ext"

Then find the MySQL keyword, remove the following sentence in front of the "; “

; extension = Php_mysqli.dll

Now PHP is configured with MySQL module.

Host (site) configuration single Site configuration item port monitoring:

To configure a custom listening port in the Apache Master Profile httpd.conf, the default is: Listen 80
Custom configuration (multiple Listen monitoring can be set at the same time):

Listen 8088Listen 9099Listen 192.168.253.1:10000

If a write-only port indicates that multiple IPs of this machine are used to change the port, otherwise it is only "responsible" (listening) for the IP

Set the name of the host (site): Server name

By default, the Apache installation succeeds with a "site" configured by default, where the actual location of the site is:
apache安装位置/htdocs/
The default hostname is set at install time, such as www.abc.com, and of course, localhost is also available.
The configuration in httpd.conf is:

# If Your host doesn ' t has a registered DNS name, enter its IP address here. ServerName localhost:80

can also be set to:

ServerName www.abc.com
Set the physical path of the host (site)

Form:
DocumentRoot "Actual physical path"
Important idea: "Site" is a folder
In httpd.conf, the default configuration for the site is as follows:

* Note: The following configuration is apache2.2 configuration, apache2.4 has a new configuration method, for details, please refer to:
Directory permission settings for apache2.4 *

# Documentroot:the directory out of which you'll serve your# documents. By default, all requests is taken from the This directory, but# symbolic links and aliases is used to point to other Loca tions.# Front set Define srvroot "E:/amp/apache" # here is the original site, to write to their own site location DocumentRoot "${srvroot}/htdocs"<Directory"${srvroot}/htdocs"># # Possible values for the Options directive is "None", "All", # or any combination of: # Indexes in Cludes followsymlinks SymLinksIfOwnerMatch execcgi multiviews # # Note that ' multiviews ' must be named *explicitly*    ---"Options all" # doesn ' t give it for you.  # # The Options directive is both complicated and important.    Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information.    # Options Indexes followsymlinks # # AllowOverride Controls What directives is placed in. htaccess files. # It can be ' all ', ' None ', or any combination of the keywords: # Options FileInfo authconfig Limit # Allowover    Ride None # # Controls who can get stuff from the this server. # Require All granted</Directory>

We'll set up a custom site next

#修改为自己的站点位置DocumentRoot "g:/mysite/project" #此时在浏览器中访问 "localhost" will find the Forbidden access (Forbidden), you need to add the following access rights <  "G:/mysite/project">    Options Indexes    Order deny,allow    DirectoryIndex my_index.html </ Directory >

Above is the configuration of apache2.2, the following is the configuration of apache2.4:

# Custom Site Configuration DocumentRoot "E:/amp/mysite" <  "e:/amp/mysite">    # Allow access to file list under directory    Options Indexes    # Require all denied    Require All granted    DirectoryIndex index.html index.php    # Next line, representing the current directory, allowing "distributed" permission    configuration allowoverrideall</  Directory>
Directory Access Permissions

A site, is a "directory"
Each directory should have "access rights", with the following syntax:

<"directory path to set permissions"># Setting, when a request does not have a given request file name and no default page (first page), Show file list options Indexes #设置权限控制的先后顺序, there are only 2 kinds: deny,allow (first deny, then allow), Allow,deny (allow first, then deny) Order deny,allow# deny is used to set the source address or server name to deny access, almost meaningless! The Deny from Deny list (for example: Deny from 192.169.1.3 172.180.4) allows the From permission list (for example: Allow from all) # Set the "default page" under this folder (First page), you can set multiple, for the request without the text The file name is automatically used as the "Back" page (search in order) DirectoryIndex index.html index.php main.php abc.php 123.html
"Distributed Permissions" in Directory access permissions

Access to a directory is set up in Apache using directory, but may not be convenient:
1.apache configuration modification, Apache must be restarted
2. "Subordinate directory" access in a site, not easy to set up separately
This can be called "Centralized permissions";

distributed permissions, that is, for any one of the sub-directories of a site, you can set their own access permissions, and can not restart Apache to take effect
Practice: in 2 steps:
1. Add one in the directory configuration for the catalog permissions for the site:

# Custom Site Configuration DocumentRoot "E:/amp/mysite" <  "e:/amp/mysite">    Options Indexes    Order deny,allow    directoryindex index.html index.php    # Next line, representing the current directory, allowing "distributed" permission configuration    allowoverrideall</ Directory >

2. In any subdirectory under this directory, create a special file in .htaccess which to write the required "permission content" (almost as in directory)
Example: In www.abc.com/day1/, you can initially browse the list of files in Day1 and then add files to the folder with the .htaccess following contents:

Deny from all # denies all allowed form 192.168.3.4 # allow this IP access
Multi-site configuration is divided into 3 steps:

Multi-site Configuration

(1) Open our Apache configuration file, httpd.conf, find the vhost.conf keyword, will find the following line in front of the comment symbol # Stripped to open the multisite configuration:

(2) Open conf/extra/httpd-vhosts.conf the file and follow the steps below to configure the site

A. In the head of this file (as far as possible), add the following line of code (apache2.2 need this step, apache2.4 namevirtualhost is removed, can not do this step)

# Set the IP address and port number that you want to provide "multi-site" service, usually by default: *.80# which * represents all IP, the default is actually Apache installed after the installation, no need to manually set (keyword namevirtualhost): # Write only once ' Namevirtualhost *:80 '

B. Remove the default two sites, or modify them to our own site, such as we will this two sites, the first site is configured as Apache default site, the second is configured as one of our own site (that is, a folder on the disk only)

# set Apache's default site to the first site so that when the requested site does not match here, apache# will return the first site here<VirtualHost*:80>ServerName localhost DocumentRoot "D:\AMP\httpd-2.4.18-win64-VC11\Apache24\htdocs" # Property needs to be consistent with DocumentRoot <Directory"D:\AMP\httpd-2.4.18-win64-VC11\Apache24\htdocs">Options Indexes # Allow all requests Require all granted # almost do not write deny DirectoryIndex index.htm l,index.php</Directory></VirtualHost><VirtualHost*:80>ServerName www.myphp.com # Sets the alias for the site, which can also be accessed through the following domain name Serveralias "lt.php.com" DocumentRoot "D:\Software\PHP\p roject "# attribute needs to be consistent with DocumentRoot<Directory"D:\Software\PHP\project">Options Indexes # Allow all requests Require all granted # almost do not write deny DirectoryIndex index.php index.php</Directory></VirtualHost>

Note that the above comments, where the first site is set to the Apache default site, the second site is set up as a folder on my disk, later need more sites, then in the form above to add it, But the path behind DocumentRoot needs to be consistent with that path in directory.

Attention:

Because we have opened the multisite configuration, then when Apache detects the domain name that cannot be matched, Apache will return the first site by default, and the first site that is written in the front is the one.

The above site permission access control is based on apache2.4, apache2.2 is deny from all (reject All) or allow from all or deny from IP address or domain name, allow from multiple IP addresses or multiple domain names form.
* Note:
1. First remove the site from the 2 initial installations in the httpd-vhosts.conf file!!
2. The settings for each site, mainly 3: ServerName , DocumentRoot ,
<Directory></Directory>

Settings for directory aliases (virtual directories)

* * There is a directory Dir1 under the corresponding folder for a site www.abc.com, you can access the directory as follows:
Www.abc.com/dir1
This dir1 is called "Real directory I" * *

The virtual directory is:
A directory name does not exist under the site, but it can be accessed using the same syntax, such as:
WWW.ABC.COM/DIR2//Assuming that the directory is not dir2 at all in this Site directory, you can access

The implementation of this technique, the so-called "directory Alias" (virtual directory) settings, the process is as follows:
The first step:
Set the name of the directory alias and its corresponding "real-World Directory (folder location)":
Alias/directory alias name "Location of the actual real Directory (folder)"
Step Two
Set access permissions for this real directory (otherwise Forbidden)

<"Real directory path"># and other directory permissions settings </directory> 

For example:

<VirtualHost*:80>ServerName www.gragon.com # Sets the alias for the site, which can also be accessed by the following domain name # serveralias "lt.php.com" DocumentRoot "E:/amp/mysite" # attributes need to be consistent with DocumentRoot<Directory"E:/amp/mysite">Options Indexes # Allow all requests Require all granted # almost do not write deny DirectoryIndex index.php index.php</Directory># Set the virtual directory here Alias/abc "E:/a_personneldocument"<Directory"E:/a_personneldocument">Options Indexes Require all granted</Directory></VirtualHost>

PHP Learning notes-Apache settings

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.