Reprint: Configure Apache under CentOS

Source: Internet
Author: User
Tags php and mysql php programming language apache log password protection

Reprint Address: http://www.cnblogs.com/zgx/archive/2011/09/22/2185261.html

2011-09-22 16:30 by Zhou, 6379 reading, 0 reviews, Favorites, compilation

To implement the Web server (WWW) on Linux requires Apache, but Apache can only provide the most basic static Web site data, want to implement dynamic website, it is best to PHP and MySQL support, so below we will be the lamp (Linux +apache+mysql+php) as an introduction to installation and setup.

Lamp required software and its structure
httpd mysql mysql-server php php-devel php-mysql
can be installed in RPM package, or installed directly with Yum
#yum install httpd Mysql-server php php-devel php-mysql
To learn about the structure of this version of Apache 2.0 to know how to handle our Web page data
/etc/httpd/conf/httpd.conf: Most of the main configuration files, but many other distribution the file into several small files, respectively, to manage the different parameters. However, the main configuration file is based on this file name.
/etc/httpd/conf.d/*.conf: One of the features of CentOS, If you do not want to modify the original configuration file httpd.conf, then you can separate your own additional parameters, and when you start Apache, this file will be read into the main configuration file. The
/usr/lib/httpd/modules:apache supports many modules, so the module you want to use is placed by default in this directory
/var/www/html: This is the default "home" directory for CentOS.
/var/www/error: If the error message appears on the browser due to incorrect host settings or data errors requested by the browser, the default information for this directory is the primary.
/var/www/icons: Provide some small Apache icons
/var/www/cgi-bin: Default to some executable CGI programs placed directory
/VAR/LOG/HTTPD: The default Apache log files are put here , for large traffic sites, this directory should be very careful, because this file is very easy to change, you need enough space Oh
/usr/sbin/apachectl: This is Apache's main execution file, this executable file is actually shell script, It can proactively detect some of the settings on the system to make it easier for you to start Apache
/usr/sbin/httpd: This is the primary Apache binary
/USR/BIN/HTPASSWD: When you want to log in to certain pages, You need to enter your account number and password. Then Apache itself provides a basic password protection method. This password is generated by this command to implement the

As far as MySQL is concerned, you need to know several important directories and files that have the following:
/ETC/MY.CNF: This is the MySQL configuration file, which includes the optimization of the MySQL database you want to make, or the addition of some additional parameter designations to MySQL, which can be implemented in this file.
/usr/lib/mysql: This directory is where the MySQL database is placed, and when you start any MySQL server, it is important to remember that the directory is fully backed up when it is backed up.

In addition, in PHP, you should know the following several files.
/usr/lib/httpd/modules/libphp4.so:php is available to the Apache module, which we can design the most important files of the PHP programming language on the Apache Web page
/etc/httpd/conf.d/php.conf: Do you want to manually write the module to httpd.conf? No, because the system has actively written the PHP setup parameters to this file, and this file will be read in when Apache restarts.
/etc/php.ini: This is the main PHP configuration file, including PHP can not allow users to upload files, can allow certain low-security flags, etc., are set in this configuration file.
/etc/php.d/mysql.ini/usr/lib/php4/mysql.so:php can support MySQL interface to see these two files. These two files are provided by the Php-mysql software
/usr/bin/phpize/usr/include/php: If you want to install a PHP-like accelerator in the future to speed up browsing, then this file and directory needs to exist, or the accelerator software is not available.

Basic settings for httpd.conf
First, you need to have a full hostname within the/etc/hosts, or you will be prompted to not find the full hostname when you restart the Apache service.
The basic setup for httpd.conf is this:
< set up Projects >
Related parameters within the sub-set item
。。。。
</Setup Items >
For example, if you want to provide additional functionality for the home page/var/www/html, you can set the following:
<directory "/var/www/html" >
Options Indexes
......
</Directory>

Settings items for the host environment
#vi/etc/httpd/conf/httpd.conf
Servertokens OS
# This project tells the client WWW server version and operating system, does not need to adapt it
#如果你不想告诉太多的主机信息, change the OS of this project to minor

ServerRoot "/ETC/HTTPD"
#这个是设置文件的最顶层目录, typically using absolute paths, some of the following data settings use relative paths when
#就是与这个目录设置值有关的下层目录, you don't need to change it
ServerRoot
Set the absolute path for Apache installation
TimeOut
Set the maximum wait time that the server receives to completion
KeepAlive
Set the server to open the continuous request function, the real server is generally open
Port
Sets the default port for the HTTP service.
User/group
Set the performer and genus of the server program, which is typically Apache

Let's do some experiments on Apache.
1: We test to change the default site directory to the root home directory
New/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 the DocumentRoot "/var/www/html" section//apache
Change the/var/www/html directory to/root/website.
After finding <directory "/var/www/html" >//define apache/var/www/html this area
Change the/var/www/html into a/root/website.
So we can get rid of APAHCE's default path.
Then restart the service
#service httpd Restart
Here when you restart the service, may error, said to find the directory, this is mainly due to selinux caused by
How to solve it? There are 2 ways to turn off SELinux
#setenforce 0
or change the SELinux attribute of the/root/website file to match the requirements of the HTTPD server.
How to change? We can copy the SELinux attribute of this directory/var/www/html
#chcon-R--reference/var/www/html/root/website
And then after restarting the service, you'll see that it didn't get an error.
But when you go to localhost, you will find the access denied. Mainly because your/root permissions are 750,ahache This user does not have permission to access, you need to change the permissions, can be changed
#chmod-R 755/root
And then went to visit and found normal.

2: Name-based virtual host
Requires two domain name resolution 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 contents of the main page of different files separately.
#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 contents of CONF.D are also Apache configuration files
Add the following content:
Namevirtualhost 192.168.76.133:80

<virtualhost 192.168.76.133:80>
ServerName service.example.com
Documentroot/var/www/server
</VirtualHost>

<virtualhost 192.168.76.133:80>
ServerName client.example.com
Documentroot/var/www/client
</VirtualHost>
#service httpd Restart
So the name-based virtual host is configured
If you do not have DNS you can re-add the Hosts file on your machine to record Linux in/etc/hosts this file in Windows C:\windows\system32\drivers\etc\hosts file
Plus these two lines
192.168.76.133 server.example.com
192.168.76.133 client.example.com
So you are going to test, you will find that access to different domain names display different content so that the name-based virtual host is configured!

3: IP address-based virtual host
Add a temporary network adapter first
#ifconfig eth0:0 192.168.76.132//temporary use, after reboot will disappear
Then convenient virtual.conf file
#vi/etc/httpd/conf.d/virtual.conf
Change the content to
#NameVirtualHost 192.168.76.133:80

<virtualhost 192.168.76.133: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 have access to the IP, you can also see different content, or you edit the Hosts file, access to the domain name is no problem
So the virtual host based on IP address is also successful!

4: Aliases
Join in the/etc/httpd/conf/httpd.conf.
Alias/test "/root/website/"//alias so you can use 192.168.76.133/test to access the 192.168.76.133 page
This place needs to be noted is/test or/test/this is a difference you use/test then you can only use 192.168.76.133/test access when you visit if you use/test/so 192.168.76.133/test/access, and/ Test will not let you visit
Forget here you first put the/ETC/HTTPD/CONF.D directory inside the virtual directory just set comment out, otherwise can not access, is because the virtual directory, and httpd.conf inside the settings will not be able to access, of course, the other access is not

5: Download resources for Web pages
First add an alias
#vi/etc/httpd/conf/httpd.conf
Add after Alias/test "/root/website/"
Alias/down "/var/ftp/pub"
To set the parameters for the/var/ftp/pub locale
<directory "/var/ftp/pub" >
Options Indexes MultiViews
AllowOverride None
Order Allow,deny
Allow from all
</Directory>
Automatically list directory documents when options join MultiViews//No Index
Then restart the service, so that http://192.168.76.133/down/inside can list/var/ftp/pub inside the file, try to point a save as, whether you can download? Hehe success!

The realization of 6:.HTPASSWD
#vi/etc/httpd/conf/httpd.conf
We'll do it for the/var/ftp/pub we just made.
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>
Then restart the httpd service,
Let's generate a. HTPASSWD user password
Htpasswd-c/ETC/HTTPD/HTPASSWD Test
Access to 192.168.76.133/down will require a password.
That's how it works.

Reprint: Configure Apache under CentOS

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.