☆ 查看系統 CentOS 自帶的 Apache 安裝版本
[root@httpd ~]# rpm -qa httpd
httpd-2.2.15-26.el6.centos.x86_64
☆ 卸載 Apache
[root@httpd ~]# rpm -e --allmatches --nodeps httpd-2.2.15-26.el6.centos.x86_64
[root@httpd ~]# rpm -qa httpd
[root@httpd ~]# rpm -e --allmatches --nodeps httpd-2.2.15-26.el6.centos.x86_64
error: package httpd-2.2.15-26.el6.centos.x86_64 is not installed
☆ 安裝 Apache
tar -zxvf httpd-2.2.27.tar.gz
cd httpd-2.2.27
./configure \
--prefix=/application/apache2.2.27 \
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-modules=most \
--enable-so \
--with-mpm=worker \
--enable-rewrite
make
make install
cd /application/
ll
ln -s /application/apache2.2.27/ /application/apache
cd application/
ll
ln -s /application/apache2.2.27/ /application/apache
rm -fr /application/apache
ll
ln -s /application/apache2.2.27/ /application/apache
ll
☆ 啓動並訪問
cd /application/apache/bin
./apachectl -t
./apachectl start
lsof -i :80
ps -ef|grep httpd
☆ 服務器內部測試
wget localhost
curl localhost
服務器外部測試
http://server ip/
☆ 修改項目首頁
cd /application/apache/conf
vi httpd.conf
<IfModule dir_module>
DirectoryIndex default.html index.html
</IfModule>
以上配置片段,把默認首頁修改爲,第一首頁是 [ default.html ] , 第二首頁是 [ index.html ] 。
優先使用第一首頁,找不到第一首頁,使用第二首頁,二者都找不懂,項目無首頁。
優雅地重啓 Apache
cd /application/apache/bin
./apachectl graceful
☆ 獲取一份純淨的 Apache 配置文件
把 Apache 配置文件的註釋和空行去掉,新內容輸出到新文件 httpd.conf.ori ,
源文件保持不變。
grep -Ev "#|^$" httpd.conf >httpd.conf.ori
☆ 項目無首頁時,不要列出網站文件
默認情況下,項目無首頁, Apache 列出網站的所有文件,
修改配置文件,禁用此功能。
vi httpd.conf
<Directory "/application/apache2.2.27/htdocs">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
以上配置片段把把原先的 Indexes 去掉,優雅重啓 Apache
./apachectl graceful
☆ 設置 Apache 虛擬主機(部署多個 Apache 項目)
mkdir -p /var/html/{www,blog,bbs} [花括號內的三個目錄名稱之間不能有空格]
touch /var/html/{www,blog,bbs}/index.html
for name in www blog bbs;do echo "http://$name.test.com/" >/var/html/$name/index.html; done
for name in www blog bbs;do cat /var/html/$name/index.html;done
tree /var/html
vi /application/apache/conf/extra/httpd-vhosts.conf
新增如下配置:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/html/www"
ServerName www.test.com
ServerAlias www.test.com
ErrorLog "logs/www-error_log"
CustomLog "logs/www-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/html/blog"
ServerName blog.test.com
ServerAlias blog.test.com
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/html/bbs"
ServerName bbs.test.com
ServerAlias bbs.test.com
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
</VirtualHost>
vi /application/apache/conf/httpd.conf
a.把下面配置片段的註釋打開
Include conf/extra/httpd-mpm.conf
Include conf/extra/httpd-vhosts.conf
b.增加如下配置
<Directory "/var/html">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
檢查配置文件的語法
cd /application/apache/bin
./apachectl -t
優雅地重啓 Apache
./apachectl graceful
配置客戶端機器的 hosts
修改文件 C:\Windows\System32\drivers\etc\hosts ,增加如下配置
10.1.3.53 www.test.com blog.test.com bbs.test.com
☆ 解決異常-無法可靠地確認服務器的完全限定域名
httpd: Could not reliably determine the server's fully qualified domain name,
using ::1 for ServerName
vi httpd.conf
打開註釋並修改他的內容,如下所示
ServerName 127.0.0.1:80
☆ Apache 日誌輪詢
安裝 cronolog
tar -zxvf cronolog
cd cronolog
./configure
make
make install
安裝完畢,生成如下工具
ll /usr/local/sbin/cronolog
vi httpd-vhosts.conf
修改配置片段
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/html/www"
ServerName www.test.com
ServerAlias www.test.com
ErrorLog "|/usr/local/sbin/cronolog /application/apache/logs/www-error-%Y%m%d.log"
CustomLog "|/usr/local/sbin/cronolog /application/apache/logs/www-access-%Y%m%d.log" combined
</VirtualHost>
其中, ErrorLog 是錯誤日誌輪詢,CustomLog 是訪問日誌輪詢。
vi /application/apache/conf/httpd.conf
a.把
CustomLog "logs/access_log" common
修改爲
CustomLog "|/usr/local/sbin/cronolog /application/apache/logs/default-access-%Y%m%d.log" combined
b.把
ErrorLog "logs/error_log"
修改爲
ErrorLog "|/usr/local/sbin/cronolog /application/apache/logs/default-error-%Y%m%d.log"
優雅地重啓 Apache
cd /application/apache/bin
./apachectl graceful
☆ 安裝 PHP 並結合 Apache
rpm -q zlib libxml2 \
libjpeg-turbo freetype libpng gd \
curl zlib-devel libxml2-devel \
libjpeg-turbo-devel freetype-devel \
libpng-devel gd-devel \
yum install zlib libxml2 \
libjpeg-turbo freetype libpng gd \
curl zlib-devel libxml2-devel \
libjpeg-turbo-devel freetype-devel \
libpng-devel gd-devel -y \
rpm -q zlib libxml2 \
libjpeg-turbo freetype libpng gd \
curl zlib-devel libxml2-devel \
libjpeg-turbo-devel freetype-devel \
libpng-devel gd-devel \
tar -zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make
make install
tar -zxvf php-5.3.27.tar.gz
cd php-5.3.27
mkdir -p /application/php5.3.27
./configure \
--prefix=/application/php5.3.27 \
--with-apxs2=/application/apache/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-xmlrpc \
--with-openssl \
--with-zlib \
--with-freetype-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-iconv=/usr/local/libiconv \
--enable-short-tags \
--enable-sockets \
--enable-zend-multibyte \
--enable-soap \
--enable-mbstring \
--enable-static \
--enable-gd-native-ttf \
--with-curl \
--with-xsl \
--enable-ftp \
--with-libxml-dir
make
make install
ln -s /application/php5.3.27/ /application/php
cd /application/apache/modules
ll
grep php /application/apache/conf/httpd.conf
cp php.ini-production /application/php/lib/php.ini
vi /application/apache/conf/httpd.conf
找到配置片段
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
在這個配置片段增加如下配置片段
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps
找到配置片段
User daemon
Group daemon
把他們修改爲
User www
Group www
找到配置片段
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
把他們修改爲
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
groupadd www
useradd www -g www -M -s /sbin/lologin
./apachectl -t
./apachectl restart
index.php
<?php
phpinfo();
?>
mysql.php
<?php
$link_id=mysql_connect('10.1.1.3','root','test') or mysql_error();
if($link_id){
echo "mysql successful by root !";
}else{
echo mysql_error();
}
?>