1) Unload the system's own httpd:
Rpm-qa|grep httpd
RPM-E Httpd-2.2.15-15.el6.centos--nodeps
Rpm-e Httpd-tools
2) Find the latest download link
Find the latest version of the download link from http://httpd.apache.org/download.cgi, now the most stable version of the link is: http://mirror.bjtu.edu.cn/apache//httpd/httpd-2.2.24.tar.gz
3) Start installing Apache
Cd/usr/local/src
wget http://mirror.bjtu.edu.cn/apache//httpd/httpd-2.2.24.tar.gz
TAR-ZXVF httpd-2.2.19.tar.gz
CD httpd-2.2.19
./configure--prefix=/usr/local/apache--enable-vhost-alias--enable-rewrite--enable-info
Make
Make install
4) Copy initialization files and set Apache boot up
CP BUILD/RPM/HTTPD.INIT/ETC/INIT.D/HTTPD
chmod 755/etc/init.d/httpd
Chkconfig--add httpd
Chkconfig--level httpd on
5) Creating Symbolic Links
Check the/etc/init.d/httpd to see the required files
Conffile=/etc/httpd/conf/httpd.conf
HTTPD=${HTTPD-/USR/SBIN/HTTPD}
Pidfile=${pidfile-/var/log/httpd/${prog}.pid}
Lockfile=${lockfile-/var/lock/subsys/${prog}}
The symbolic links are as follows:
Ln-s/usr/local/apache//etc/httpd
Ln-s/usr/local/apache/bin/httpd/usr/sbin/httpd
Ln-s/usr/local/apache/bin/apachectl/usr/sbin/apachectl
Ln-s/usr/local/apache/logs/var/log/httpd
6) Start/stop service
Service httpd Restart
Service httpd Start
/usr/local/apache/bin/apachectl start
/usr/local/apache/bin/apachectl stop
/usr/local/apache/bin/apachectl status
/ETC/INIT.D/HTTPD start
/ETC/INIT.D/HTTPD stop
/ETC/INIT.D/HTTPD restart
Use Pgrep to find the process that was started.
Pgrep httpd
7) View in Browser:
Http://192.168.0.120:80
If you see it works, it means that Apache has started.
8) Apache configuration file
Vi/usr/local/apache/conf/httpd.conf
For more configure options refer to Http://httpd.apache.org/docs/2.2/programs/configure.html
9) By default, the Apache configuration file is as follows:
#
# Documentroot:the directory out of which you'll serve your
# documents. By default, all requests is taken from this directory, but
# Symbolic links and aliases may is used to the other locations.
#
DocumentRoot "/usr/local/apache/htdocs"
#
# each directory to which Apache have access can is configured with respect
# to which services and features is allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
<directory/>
Options FollowSymLinks
AllowOverride None
Order Deny,allow
Deny from all
</Directory>
#
# This should is changed to whatever your set DocumentRoot to.
#
<directory "/usr/local/apache/htdocs" >
#
# Possible values for the Options directive is "None", "all",
# or any combination of:
# Indexes includes followsymlinks symlinksifownermatch execcgi multiviews
#
# Note that "MultiViews" must is named *explicitly*---"Options all"
# doesn ' t give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/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
#
AllowOverride None
#
# Controls who can get stuff from the this server.
#
Order Allow,deny
Allow from all
</Directory>
#
# directoryindex:sets The file that Apache would serve if a directory
# is requested.
#
<ifmodule dir_module>
DirectoryIndex index.html
</IfModule>
If you need your home directory to be accessible, you need to create a symbollink under DocumentRoot to point to your home and make sure that the home directory has executable permissions for everyone. For example:
[Aaa@centos_aaa cgi-bin]$ ll-d/usr/local/apache/htdocs/~aaa
lrwxrwxrwx. 1 root root 9 Apr 7 09:44/usr/local/apache/htdocs/~aaa-/HOME/AAA
[Aaa@centos_aaa cgi-bin]$ ll-d/home/aaa
Drwxr-xr-x. AAA AAA 4096 APR 7 11:10/HOME/AAA
Iii. directories and files of the Apache server
1. Web Site Directory
/VAR/WWW Directory of Apache site files
/var/www/html Web files that store Web sites
/var/www/cgi-bin CGI Program Files
/var/www/html/manual Apache Web Server Manual
2. Configuration files
. htaccess directory-based configuration files,. htaccess files contain access control directives for files in the directory in which they reside
/etc/httpd/conf/httpd.conf Apache Web server configuration file directory
3. Startup script
/ETC/RC.D/INIT.D/HTTPD startup scripts for the Web server daemon
/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 files
/usr/sbin where Apache Web server program files and applications are stored
/usr/doc/placing Apache Web server documents
/var/log/http where Apache log files are placed
Iv. More Commands
1. View Apache installation information: apachectl-c
2, the Apache configuration file for the syntax check: apachectl-t
3. View Apache Compilation configuration parameters: Apachectl-v
4. View Apache's compilation module: Apachectl-l
V. httpd.conf configuration file
(i) Basic configuration
The httpd.conf file contains the following three sections:
1. Global environment settings: the part that controls the behavior of the entire Apache server (that is, Global environment variables)
2. Primary 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 does not write with "\" for newline, except for the parameter value of the option, all option directives are case-insensitive and "#" denotes comments.
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 configuration files and log files, typically the relative root directory is "/etc/httpd", which generally contains conf and logs subdirectories, which can be set in the "ServerRoot"/etc/httpd "format.
2. Set the IP address and port number for Apache snooping
Apache, by default, listens for client requests on TCP 80 ports on all available IP addresses on this computer, and 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 take the "Listen 192.168.0.94:8080" and other settings, but at this time through the Web browser to access the site must also add the appropriate port number after the domain name, such as input "HTTP/ 192.168.0.94:8080 "can only be accessed. Note: The IP of my virtual machine Linux is 192.168.0.94,windows IP is 192.168.0.225
3. Set up your network administrator's e-mail address
When the client computer Access server error, the server will usually return to the client computer error prompt page, in order to facilitate the resolution of the error, in this page usually contains an administrator's e-mail address, you can use the ServerAdmin statement to set the administrator's e-mail address, such as " ServerAdmin xinyuan365@sohu.com ".
4. Set Server host Name
To make it easier for Apache to identify the server itself, you can use the ServerName statement to set the host name of the server. 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, fill in the server's IP address. For example "ServerName 192.168.0.94:80".
5. Set the path to the home directory
The default path for the Apache Server home directory is "/var/www/html", which allows you to place pages that need to be published in this directory, as well as to modify the path of the home directory to another directory for easy user management and use. For example, 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 default Document
The default document is to enter the Web site's IP address or the Web page that the domain name displays in the Web browser, which is usually 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 the DirectoryIndex index.html Index.html.var, you can modify the default document name of the DirectoryIndex statement to a different file.
If there are multiple filenames, each file name must be separated by a space, and Apache will find the file name specified in the DirectoryIndex statement according to the order of the file name. If you can find the 1th one, call the 1th one, otherwise look for and call 2nd, and so on. For example, if you add index.htm and index.php files as default documents, you can modify the httpd.conf file to "DirectoryIndex index.html index.htm index.php Index.html.var" accordingly.
7. Setting up log files
Log files are important for the user to find a system failure or to analyze the health of the Web server, with two important settings at this time.
(1) Error log. The error log records Apache's errors at startup and runtime, so when Apache goes wrong, it should first check the log file. Usually the error log file name is Error_log, where the error log file is stored and the file name can be set by the errorlog parameter. For example "Errorlog logs/erroe_log". If the log file storage path does not begin with "/", it indicates that the path is relative to the ServerRoot directory.
(2) Access logs. The access log records all of the client computer's access information, and by analyzing the access log you can know when the client accessed which files of the Web site. Typically, the file name of the access log is Access_log, and the location and file name 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 in which the log is used, where common or combined can be used. Where common refers to the common standard format commonly used by Web servers, which can be recognized by many log analysis programs, combined refers to the use of the combined record format, compared to common, combined format is basically the same, Only the reference page and browser identification information are more.
8. Set the default character set
The Adddefaultcharset option sets the default character set that the server returns to the client computer, because the Apache server default character set is Western Europe (UTF-8), so garbled 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 will be displayed properly.
(ii) Configuring directory Permissions
1. Define Directory Features
Each directory that Apache accesses can be set to the related services and features that are allowed or (and) disallowed. (also affects its subdirectories)
First, setting the "Default" address has only the most basic permissions:
Options FollowSymLinks
AllowOverride None
Note that from now on, special permissions must be set up so that no unintended results can occur. Please confirm it carefully.
For example:
#
# This value is however: "None", "all", or the following combination: "Indexes",
# "includes", "FollowSymLinks", "execcgi", or "multiviews".
Note "MultiViews" must be explicitly specified---"Options all" does not include this attribute.
#
Options Indexes followsymlinks MultiViews
#
# This entry controls which. htaccess files in the directory can be overwritten.
# allowable values: "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
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", there can be only one comma, there is a space error, and the word is not limited in size. The meaning of the above is to set the first "check the forbidden set, no forbidden all allow", and the second sentence does not deny, that is, no forbidden access to the setting, directly allows all access. This is primarily used to ensure or overwrite the settings of the parent directory and to open all content access rights.
According to the above explanation, the following settings are unconditionally forbidden to access:
Order Allow,deny
Deny from all
If you want to prohibit access to part of the content, all other open:
Order Deny,allow
Deny from Ip1 ip2
Or
Order Allow,deny
Allow from all
Deny from Ip1 ip2
Apache will decide which rule to use at the end of order, such as the second one above, although the second sentence allows access, but since allow is not the last rule in order, it also needs to see if there is a deny rule, so in the third sentence, Access that complies with IP1 and IP2 is banned. Note that the "last" rule of order decision is very important, and here are two examples of errors and how to correct them:
Order Deny,allow
Allow from all
Deny from domain.org
Error: You want to prohibit access from domain.org, but deny is not the last rule, Apache has successfully matched the second sentence to allow, and will not see the third sentence at all. Solution: Order Allow,deny, the following two sentences do not move, you can.
Order Allow,deny
Allow from ip1
Deny from all
Error: You want to allow access only from IP1, however, although the second sentence is set to enable rule, because the order of the deny after, so the third sentence will prevail, and the scope of the third sentence is clearly included in the IP1 (all include ip1), so all access is forbidden. Workaround One: Remove the third sentence directly. Workaround Two:
Order Deny,allow
Deny from all
Allow from ip1
Summary: The rule is that order Deny,allow indicates that the deny priority is low and allow priority is high, and order Allow,deny indicates that 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 under the main directory/var/www/html can not access it. Of course not.
You can use a virtual directory 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 the web accessible by creating an alias for the virtual directory. It is safe to do this, the second is to access the simple, do not enter so long is the directory address, but only with the input of 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 use the alias option to create a virtual directory, such as
alias/bbs/"/opt/www/lk/"
<directory "/opt/www/lk/
">
Options Indexes MultiViews
AllowOverride None
Order Allow,deny
Allow from all
(iv) Certification of users
User authentication is a very important part of network security, so that users who want to access the specified website can enter a user name and password to log in, and play a natural security barrier. Apache's security certifications are also common in practical use. Here's an example of how to implement user authentication in an 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 from all
Authtype:basic
authname "Welcome go home:"
authuserfile/etc/httpd/authpwd
Require User Laoda Laoer
Description:
authtype: Defines the type of user authentication, which is commonly used by mod_auth provided by basic;
Authname:web browser Displays the prompt text when entering the user name and password box;
AuthUserFile: Defines the path to the password file htpasswd;
Require User: Defines the list of users allowed to access, The names are separated by a space.
After that, create a authpwd file to save the password, touch/etc/httpd/authpwd
Next, write the user name and password to the file (note: The first time you create a user with the-c parameter, the second user is created without having to write , or overwrite just the user):
[root@localhost http] #htpasswd-c/etc/httpd/authpwd laoda
New password:
R E-type New Password:
Add password for user Laoda
[root@localhost http] #htpasswd/etc/httpd/authpwd LAOER
New password:
re-type New password:
Add password for user laoer
Now service httpd Restart, then use the browser to access this Linux IP address.
(v) Configuring virtual hosts
to save money and improve server utilization, we can build multiple "hosts" on a single machine. Each host can provide Web services to external, in the outside world seems to be a few 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 just.
How to configure a virtual host. Apache's virtual hosting capabilities are very powerful, and the configuration is simple. Mainly divided into IP-based and domain-based virtual host.
Let's first configure the IP-based virtual hosting. There are also two options for how many IPs you have available 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 buy one, then you can use an IP address and different ports, so that different ports to access different virtual hosts.
1, the IP address is the same, but the port number is different:
Now on my CentOS, there is only one ip:192.168.0.94, I would like to use 8080 and 80,812 ports respectively to configure two sites, edit httpd.conf:
Listen 8080
Listen 8081
Documentroot/var/www/web1
DirectoryIndex index.html index.htm
Hostnamelookups off
Documentroot/var/www/web2
DirectoryIndex index.html index.htm
Hostnamelookups off
Restart the service.
2, the port number is the same, but the IP address is different, if one is 94, one is 95:
ServerName 192.168.0.94:80
Documentroot/var/www/web1
DirectoryIndex index.html index.htm
ServerName 192.168.0.95:80
Documentroot/var/www/web2
DirectoryIndex index.html index.htm
If the machine has only one network card, then you have to bind the multi-IP on this NIC:
Ifconfig eth0:1 192.168.0.95
3. Domain-based virtual host
Namevirtualhost 192.168.0.94:
ServerName www.web1.com:80
Documentroot/var/www/web1
DirectoryIndex index.html index.htm
ServerName www.web2.com:80
Documentroot/var/www/web2
DirectoryIndex index.html index.htm
Then everyone in, Linux under the/etc/hosts file or under Windows C:\WINNT\system32\drivers\etc\hosts file, add
192.168.0.94 www.web1.com
192.168.0.94 www.web2.com
Please note that after the virtual machine configuration has changed, be sure to restart.
(vi) Added PHP support