CentOS (Linux) Apache server configuration and management method sharing _linux

Source: Internet
Author: User
Tags parent directory php script relative web services apache log centos
One, Web server and Apache
1, Web server and URL
2, the history of Apache
3, supplementary
http://www.netcraft.com/can view the market share of Apache servers
At the same time must pay attention to is Ngnix, is in the rising period of strong growth, and Apache a competition for the world's feeling, really Goson ~ ~ ~ ~
second, the Apache Server Management Command
1, command start: Service httpd Start/stop/restart/reload/condrestart/status/configtest/graceful/help
2. Script start:/etc/init.d/httpd start/stop/...
3, set to boot automatically start: ntsysv+ space key
directory and files for Apache servers
1, the Web site Directory
Directory of/var/www Apache site files
/var/www/html Web files to store Web sites
/var/www/cgi-bin CGI Program Files
/var/www/html/manual Apache Web Server Manual

2. Configuration file
. htaccess a directory-based configuration file that contains access control instructions for files in its directory. htaccess file
/etc/httpd/conf/httpd.conf Apache Web server configuration file directory

3. Startup script
/ETC/RC.D/INIT.D/HTTPD the Web server Daemon's startup script
/ETC/RC.D/RC3.D/S85HTTPD to connect the run level directory (/ETC/RC3.D) to the startup script in the directory/ETC/RC.D/INIT.D

4. Application Documents
/usr/sbin where Apache Web server program files and applications are stored
/usr/doc/place Apache Web Server documentation
/var/log/http location of Apache log files
iv. More orders
1, view the Apache installation information: apachectl-c
2, the Apache configuration file for syntax check: apachectl-t
3, view the Apache compilation configuration parameters: Apachectl-v
4, view the Apache compiler module: apachectl-l
V. httpd.conf configuration file
(i) Basic configuration
The httpd.conf file is contained in the following three sections:
1. Global environment settings: Part of controlling the entire Apache server behavior (that is, Global environment variables)
2. master server configuration: Directives that define primary or default service parameters, as well as default setting parameters for all virtual hosts
3. Virtual host settings: Setting parameters for Virtual host
Where a line cannot be written with "\" to denote a newline, except for the parameter value of the option, all option directives are case-insensitive, and "#" represents the annotation.
Now, let's learn the specific settings in httpd.conf:
1. Set the path to the relative root directory
The relative root directory is usually where Apache stores the configuration files and log files, usually the relative root is "/etc/httpd", which typically contains the conf and logs subdirectories, which can be set in the "ServerRoot"/etc/httpd format.
2. Set the IP address and port number for Apache listening
Apache By default listens for client requests on TCP 80 ports on all available IP addresses on this computer, and you can use the Listen statement to listen for requests on a specified address and port. For example, to set the server to listen only for 192.168.0.94 80 ports, you can set the appropriate settings in httpd.conf: Listen 192.168.0.94:80. If you need to change the port number to 8080, you can also use settings such as "Listen 192.168.0.94:8080", but when you access a Web site through a Web browser, you must also add the corresponding port number after the domain name address, such as the input "http:// 192.168.0.94:8080 "can only be accessed. Note: My virtual machine linux IP is 192.168.0.94,windows IP is 192.168.0.225
3. Set up your network administrator's e-mail address
When a client computer accesses an error on the server, the server usually returns the error message page to the client computer, which, in order to facilitate the resolution of the error, usually contains an administrator's e-mail address in the Web page, at which point the serveradmin statement can be used to set the administrator's e-mail address, for example, " ServerAdmin xinyuan365@sohu.com ".
4. Set Server host Name
To facilitate Apache identification of the server itself, you can use the ServerName statement to set the server's host name. In the servername statement, if the server has a domain name, fill in the server's domain name, and if there is no domain name, populate the server's IP address. For example, "ServerName 192.168.0.94:80".
5. To set the path of the home directory
The Apache server home directory default path is "/var/www/html", which allows you to place pages that need to be published in this directory, as well as modifying the path of the home directory to a different directory for easy user management and use. For example, if you need to set the Apache Server home directory path to "/home/lk/www", you can modify it in the httpd.conf file: DocumentRoot "/home/www".
6. Set the default document
The default document refers to the Web page in the Web browser where the IP address of the website is entered or the domain name is displayed, which is usually called the home page. By default, Apache's default document name is Index.html, and the default document is defined by the DirectoryIndex statement, for example, in httpd.conf through "DirectoryIndex index.html Index.html.var, you can modify the default document name of the DirectoryIndex statement to another file.
If you have more than one file name, each file name must be separated by a space, and Apache finds the file name specified in the DirectoryIndex statement, based on the order of the file name. If you can find the 1th one, call the 1th one, or else look for and call the 2nd, and so on. For example, if you add index.htm and index.php files as the default documents, you can modify the httpd.conf file as "DirectoryIndex index.html index.htm index.php".
7. Setting up log files
Log files are important for users to find a system failure or to analyze the health of a Web server, with two important settings at this time.
(1) Error log. The error log records the errors that occur when Apache starts and runs, so when Apache makes a mistake, it should first check the log file. Usually the file name for the error log is Error_log, and the location and filename of the error log file can be set by the errorlog parameter. For example, "ErrorLog Logs/erroe_log". If the log file holds a path that does not begin with "/", it means that the path is a relative path relative to the ServerRoot directory.
(2) Access log. The access log records all of the access information for the client computer, and it is possible to know when the client accessed the files of the Web site by analyzing the access log. Typically, the file name for the log is Access_log, and the location and filename of the access log file can be set through the Customlog parameter, such as "Customlog Logs/access_log combined".
In this access log setting, combined indicates the format used by the log, where common or combined can be used. Common refers to the common standard format used by Web servers, which can be recognized by many log analysis programs, combined refers to the use of combined record format, and common compared to the combined format is basically the same, It's just a lot of reference pages and browser identification information.
8. Set the default character set
The Adddefaultcharset option sets the default character set that the server returns to the client computer, and because the Apache server default character set is Western Europe (UTF-8), garbled behavior occurs when the client accesses the server's Chinese Web page. The solution is to change the statement "Adddefaultcharset UTF-8" to "Adddefaultcharset GB2312", and then restart the Apache server, the Chinese Web page can be displayed normally.
(ii) Configure directory permissions
1. Define Directory Features
Each directory that Apache accesses can be set to the associated services and attributes that are allowed or (and) disallowed. (also affects its subdirectories)
First, setting the "Default" address has only the most basic permissions:
Copy Code code as follows:

<directory/>
Options FollowSymLinks
AllowOverride None
</Directory>

Note that you must set up special permissions from now on so that you do not produce unexpected results. Please confirm it carefully.
For example:
Copy Code code as follows:

<directory "D:/www_root" >
#
# This value is: ' None ', ' all ', or the following combination: ' Indexes ',
# "Includes", "FollowSymLinks", "execcgi", or "multiviews".
# note ' MultiViews ' must explicitly specify---' Options all ' does not include this attribute.
#
Options Indexes followsymlinks MultiViews
#
# this controls which. htaccess files in the directory can be overwritten.
# Allow value: ' All ' or a combination of the following: ' Options ', ' FileInfo ',
# "Authconfig", "Limit"
#
AllowOverride None
#
# controls which users can obtain data from this server.
#
Order Allow,deny
Allow from all
</Directory>

Description
Allow and deny can be used in Apache conf files or. htaccess files (with directory, Location, files, etc.) to control access authorization for directories and files.
So, the most common is:
Order Deny,allow Allow from all note that there is only one comma in the middle of "Deny,allow", and there can be only one comma, there will be errors in the spaces, and the case of the word is not limited. The meaning set above is to set "first check prohibit set, no prohibition of all allow", and the second sentence without deny, that is, no access to prohibit the setting, directly is to allow all access. This is primarily used to ensure or overwrite the settings of the parent directory, and to open access to all content.
As explained above, the following settings are unconditional access forbidden:
Order Allow,deny Deny to all if you want to disallow access to part of the content, all others are open:
Order Deny,allow Deny ip1 ip2 or
The order Allow,deny Allow from the ip1 Ip2apache will decide which rule to use, such as the second one, in accordance with the order, although the second sentence Allow allows access, However, since allow is not the final rule in order, it also needs to see if there is a deny rule, so in the third sentence, access to ip1 and ip2 is forbidden. Note that the "last" rule of the order decision is very important, and here are two examples of errors and how to correct them:
Order Deny,allow Allow from all Deny Domain.org error: Want to prohibit access from domain.org, but the deny is not the final rule, Apache in processing to the second sentence of the Allow has been successful match, will not go to see the third sentence.
Solution: Order Allow,deny, the following two words do not move, you can.
Order Allow,deny Allow from ip1 deny to all error: you want to allow access only from IP1, but although the second sentence has the Allow rule set, the third sentence will prevail because the deny is in the sequence, The third sentence clearly contains the ip1 (all include ip1), so all access is prohibited.
Solution One: Remove the third sentence directly.
Workaround Two:
Order Deny,allow Deny Allow from ip1 Summary: The rule is that the order Deny,allow indicates that the deny priority is low and the Allow priority is high, the order Allow, The Deny indicates that the allow priority is low and the deny priority is high.
(iii) Create a virtual directory if our site is built in a different directory, such as/opt/www/lk, is not in the home directory/var/www/html can not access it? Of course not. You can use virtual directories to allow users to access files in other directories. The virtual directory is a directory that is located outside of the Apache home directory, and we typically make Web access by creating aliases for the virtual directory. It is safe to do this; second, access is simple, do not need to enter so long real directory address, but only to enter a simple alias on the line; third, easy to move the site directory, as long as the virtual directory name unchanged, change the actual location, will not affect the Web access. We create virtual directories with the alias option, such as alias/bbs/"/opt/www/lk/" <directory "/opt/www/lk/" > Options Indexes multiviews allowoverride None order Allow,deny allow from all</directory>
(iv) User authentication is a very important part of network security, so that users who want to visit the designated Web site to enter a user name and password to login, played a natural security barrier role. Apache security certification is also common in practical use. Now let's use an example to demonstrate the ability to implement user authentication in the Apache server. We operate on the above virtual directories: alias/bbs/"/opt/www/lk/" <directory "/opt/www/lk/" > Options Indexes multiviews allowoverride None order Allow,deny allow to Authtype:basic authname "Welcome go home:" Authuserfile/etc/httpd/authpwd Require US ER laoda laoer</directory> Description: authtype: Defines the type of user authentication, commonly used Mod_auth provided by the basic Authname:web browser display in the User name and password box when the prompt text; AuthUserFile: Defines the path to the password file htpasswd; Require User: Defines the list of users that are allowed to access, separated by a space between the names. After that, create the Authpwd file to save the password, touch/etc/httpd/authpwd then write the username and password to the file (note: The first time the user is created using the-c parameter, the second user is created without writing, otherwise it will overwrite the user): [ Root@localhost http] #htpasswd-c/etc/httpd/authpwd laoda new Password:re-type new password:add for user password Root@localhost http] #htpasswd/etc/httpd/authpwd laoer new password:re-type new Password:add password for user Laoer now S Ervice httpd Restart, and then use the browser to access the Linux IP address can.
(v) Configure a virtual host to save money and improve the efficiency of the server, we can build multiple "hosts" on a single machine. Each host can provide the external Web services, in the outside world seems to be some different sites, but for the server, in fact, seemingly different sites, in fact, they are running on the same host on the different virtual host. How do I configure a virtual host? Apache's virtual host function is very powerful, and the configuration is simple. Mainly divided into ip-based and domain-based virtual host. Let's configure the ip-based virtual host first. There are also two options, how many available IP do you have now? If you buy a lot of IP, then you can assign each virtual host different IP, let them use the same port, this is one. If in this era of IP shortage, you do not have so many IP, only bought one, then you can use an IP address plus different ports, so that different ports to access different virtual host. 1, the IP address is the same, but the port number is different: Now my CentOS, only a ip:192.168.0.94, I would like to use 8080 and 80,812 ports to configure two Web sites, edit Httpd.conf:Listen 8080Listen 8081 <virtualhost 192.168.0.94:8080>documentroot/var/www/web1directoryindex index.html index.htmHostNameLookups Off</virtualhost><virtualhost 192.168.0.94:8081>documentroot/var/www/web2directoryindex index.html Index.htmhostnamelookups off</virtualhost> Restart the service, you can.
2, the same port number, but the IP address is different, if one is 94, one is 95:
Copy Code code as follows:

<virtualhost 192.168.0.94>
ServerName 192.168.0.94:80
Documentroot/var/www/web1
DirectoryIndex index.html index.htm
</VirtualHost>
<virtualhost 192.168.0.95>
ServerName 192.168.0.95:80
Documentroot/var/www/web2
DirectoryIndex index.html index.htm
</VirtualHost>

If this machine only has one network card, then must bind the multiple IP on this network card:

Ifconfig eth0:1 192.168.0.95

3, domain-based virtual host
Namevirtualhost 192.168.0.94:
Copy Code code as follows:

<virtualhost www.web1.com>
ServerName www.web1.com:80
Documentroot/var/www/web1
DirectoryIndex index.html index.htm
</VirtualHost>
<virtualhost www.web2.com>
ServerName www.web2.com:80
Documentroot/var/www/web2
DirectoryIndex index.html index.htm
</VirtualHost>

Then everyone in the/etc/hosts file under Linux, or under Windows C:\WINNT\system32\drivers\etc\hosts file, join
192.168.0.94 www.web1.com
192.168.0.94 www.web2.com

Please note that the virtual machine configuration changes, must be restart.
(vi) Join PHP support
At this point, our Apache can only support static HTML or HTM end of the file, want to support PHP also have to do the following:
#rpm-qa | grep php, query whether the machine has installed a PHP program, if not installed, then go to the CD in the CentOS directory, open the terminal in the directory space:
#rpm-IVH php-common-5.1.6-20.el5.i386.rpm
#rpm-IVH php-cli-5.1.6-20.el5.i386.rpm
#rpm-IVH php-5.1.6-20.el5.i386.rpm
At this point, create a new ok.php script in the/var/www/html directory and write the following:
Enter "http://web server ip/ok.php" in the browser to see if it was successful.
For historical reasons, some programs end with. php3 or. PhP4. We need to modify the PHP configuration file to open the/etc/httpd/conf.d/php.conf
Modify "#AddType Application/x-httpd-php-source. php" as "AddType application/x-httpd-php-source. php. php3 php4" To remove #, There's a space before the PHP3/4.

Exercise 1:
Use the server to set up your own Web server on the CentOS, set the "/opt/" for the newly created virtual directory, this virtual directory alias is "Exam", under this virtual directory, create a new HTML document named "Index.htm",<body> The content is "write your own name". Create Apache user authentication, add two user user1 (password 123456) and user2 (password 654321), and finally use the browser to access the "ip/exam/:8080" can see the page showing your name, can be.
Exercise 2:
Configure the virtual host. Thinking, how to configure a virtual host, which runs four sites, is 192.168.0.101 run web1,192.168.0.102 run web2,192.168.0.100 www.cba.com and www.nba.com two sites.
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.