Detailed description of CentOS Apache configuration

Source: Internet
Author: User
Tags apache log password protection

Software and structure required by LAMP
Httpd mysql-Server php-devel php-mysql
You can install it using an rpm package or directly using yum.
# Yum install httpd mysql-server php-devel php-mysql
First, let's take a look at the structure of apache 2.0, so that we can know how to process our webpage data.
/Etc/httpd/conf/httpd. conf: The main configuration file. However, many other distribution files are split into several small files to manage different parameters respectively. However, the main configuration file is based on the file name.
/Etc/httpd/conf. d /*. conf: One of the characteristics of CentOS, if you do not want to modify the original configuration file httpd. conf, you can separate your own extra parameters. When apache is started, this file will be read into the main configuration file.
/Usr/lib/httpd/modules: apache supports many modules. Therefore, the modules you want to use are stored in this directory by default.
/Var/www/html: the default "Homepage" Directory of CentOS.
/Var/www/error: if the host is set incorrectly or the data requested by the browser is incorrect, the error message displayed on the browser is the default information of this directory.
/Var/www/icons: provides some small icons of apache.
/Var/www/cgi-bin: directory for some executable CGI programs by default
/Var/log/httpd: by default, apache log files are stored here. For websites with high traffic, this directory should be very careful, because this file is easily changed to a large one, you need enough space.
/Usr/sbin/apachectl: This is the main execution file of Apache. The execution file is actually a shell script, which can actively detect some settings on the system, this makes Apache startup easier.
/Usr/sbin/httpd: This is the main apache binary file.
/Usr/bin/htpasswd: When you want to log on to some web pages, you need to enter your account and password. Apache itself provides the most basic password protection method. This command is used to generate the password.

For MySQL, You Need To Know several important directories and files as follows:
/Etc/my. cnf: This is the Mysql configuration file, which can be used to optimize the mysql database or specify some additional parameters for mysql.
/Usr/lib/mysql: the directory where the MySQL database is stored. When starting any MySQL server, remember to back up the directory completely during Backup.

In addition, you should know the following files in PHP.
/Usr/lib/httpd/modules/libphp4.so: the module provided by PHP for apache. Can we design the most important php program language file on the apache webpage?
/Etc/httpd/conf. d/php. conf: Do you want to manually write this module into Httpd. conf? No, because the system has actively written the php setting parameters to this file, and this file will be read when apache restarts.
/Etc/php. ini: This is the main configuration file of PHP, including whether PHP can allow users to upload files and some low-security labels, which are all set in this configuration file.
/Etc/php. d/mysql. ini/usr/lib/php4/mysql. so: Can PHP support the MySQL interface. These two files are provided by the php-mysql software.
/Usr/bin/phpize/usr/include/php: If you want to install a PHP accelerator later to speed up browsing, this file and directory will need to exist, otherwise, the accelerator software cannot be used.

Httpd. conf Basic settings
First, you need to have a complete host name in/etc/hosts. Otherwise, you will be prompted that the complete host name cannot be found when you restart the apache service.
The basic settings of httpd. conf are as follows:
<Set project>
Related Parameters in the project
....
</Set project>
For example, if you want to provide additional functions for the homepage/var/www/html, you can perform the following settings:
<Directory "/var/www/html">
Options Indexes
......
</Directory>

Set items for the host environment
# Vi/etc/httpd/conf/httpd. conf
ServerTokens OS
# This Project tells the client about the WWW server version and operating system and does not need to be adapted.
# If you do not want to tell too much host information, change the OS of this project to Minor.

ServerRoot "/etc/httpd"
# This is the top-level directory of the setting file, which usually uses absolute paths. When some of the following data settings use relative paths
# It is the lower-level directory related to the directory setting value and does not need to be changed.
ServerRoot
Set the absolute path for Apache installation
TimeOut
Sets the maximum wait time for the server to receive the message.
KeepAlive
Set whether the server enables the continuous request function. Generally, the real server must enable the function.
Port
Set the default port of the http service.
User/Group
Set the executor and Group of the server program, which is generally apache

Below we will do a few experiments on Apache
1: We tested to change the default website directory to the root directory.
Create/root/website directory
# Mkdir-p/root/website
# Echo "website page">/root/website/index.html
# Vi/etc/httpd/conf/httpd. conf
Find the root directory of DocumentRoot "/var/www/html" // apache
Change the/var/www/html directory to/root/website.
Find the <Directory "/var/www/html"> // define apache/var/www/html
Change/var/www/html to/root/website
In this way, we can change the default path of apahce.
Then restart the service.
# Service httpd restart
// When you restart the service, an error may be reported, indicating that the directory cannot be found, which is mainly caused by selinux.
How can this problem be solved? There are two ways to disable selinux
# Setenforce 0
Or change The selinux attribute of the/root/website file to match the requirements of the httpd server.
How to change it? We can copy the selinux attribute of the/var/www/html directory.
# Chcon-R -- reference/var/www/html/root/website
Then, after restarting the service, you will see that there is no error.
But when you access localhost, you will find that the access is denied. Why? The main reason is that your/root permission is 750, and the ahache user has no access permission. You need to change the permission.
# Chmod-R 755/root
Then, access and find that it is normal.

2: Name-based VM
The two domain names need to be resolved to your server. The corresponding relationship is
/Var/www/server server.example.com
/Var/www/client client.example.com
When you access these two domain names, you can display the content of the homepage in different files.
# Echo "server page">/var/www/server/index.html
# Echo "client page">/var/www/client/index.html
Then we edit a configuration file.
# Vi/etc/httpd/conf. d/virtual. conf // remember that the content in conf. d is also the configuration file of apache.
Add the following content:
NameVirtualHost 192.168.76.133: 80

<VirtualHost 192.168.76.small: 80>
ServerName service.example.com
DocumentRoot/var/www/server
</VirtualHost>

<VirtualHost 192.168.76.small: 80>
ServerName client.example.com
DocumentRoot/var/www/client
</VirtualHost>
# Service httpd restart
In this way, the name-based virtual host is configured.
If you do not have DNS, you can add a record to the hosts file on your machine for linux in the/etc/hosts file for windows in the C: \ windows \ system32 \ drivers \ etc \ hosts file
Add the two rows
192.168.76.133 server.example.com
192.168.76.133 client.example.com
In this way, you will find that the access to different domain names shows different content, so the name-based virtual host is configured!

3: IP address-based VM
First add a temporary Nic
# Ifconfig eth0: 0 192.168.76.132 // for temporary use, it will disappear after restart
Then the virtual. conf file is convenient.
# Vi/etc/httpd/conf. d/virtual. conf
Modify the content
# NameVirtualHost 192.168.76.20.: 80

<VirtualHost 192.168.76.small: 80>
ServerName service.example.com
DocumentRoot/var/www/server
</VirtualHost>

<VirtualHost 192.168.76.132: 80>
ServerName client.example.com
DocumentRoot/var/www/client
</VirtualHost>
After you access it with an ip address, you can find that different content can be displayed, or you can edit the hosts file to access it with a domain name.
In this way, the IP address-based virtual host is also successful!

4: alias
Add/etc/httpd/conf/httpd. conf
Alias/test "/root/website/" // Alias. In this way, when you access 192.168.76.20./ test, the page of 192.168.76.20.is displayed.
Note the difference between/test and/test/. If you use/test, you can only access it with 192.168.76.20./ test. If you use/test/, 192.168.76.20. /test/access, and/test won't let you access
Forget to put/etc/httpd/conf first. the virtual directory just set in the d directory is commented out or cannot be accessed because the virtual directory is created, and httpd. the configuration in conf cannot be accessed. Of course, localhost can be used for access, but none of the other accesses can be used.

5. Download Web Resources
Add an alias first
# Vi/etc/httpd/conf/httpd. conf
After Alias/test "/root/website/", add
Alias/down "/var/ftp/pub"
Set parameters for the/var/ftp/pub region.
<Directory "/var/ftp/pub">
Options Indexes MultiViews
AllowOverride None
Order allow, deny
Allow from all
</Directory>
Automatically List Directory documents when MultiViews is added to Options without index
Then restart the service so that the files in the/var/ftp/pub file can be listed in http: // 192.168.76.20./ down/. Try to save it as a file. Can you download it? Haha success!

6:. Implementation of htpasswd
# Vi/etc/httpd/conf/httpd. conf
We have just done this for/var/ftp/pub.
Add the following information:
Alias/down "/var/ftp/pub /"
<Directory "/var/ftp/pub">
Options Indexes MultiViews
AllowOverride AuthConfig
Order allow, deny
Allow from all
</Directory>
<Directory "/var/ftp/pub">
AuthType Basic
AuthName "this is test"
AuthUserFile/etc/httpd/htpasswd
Require User test
</Directory>
Restart the httpd service,
Generate a. htpasswd User Password
Htpasswd-c/etc/httpd/htpasswd test
The password is required to access 192.168.76.20./ down later.
This will succeed.

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.