Apache篇
安装 PCRE
PCRE(Perl Compatible Regular Expressions) 是一个 Perl 库,包括 perl 兼容的正则表达式库。
可以在 PCRE 的 sourceforge 下载最新版 PCRE,文件格式为 pcre-版本.tar.gz。本文以 pcre-8.40 为例,使用时请使用最新版,命令只要替换版本号就行了。
cd /usr/local/src
wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz
tar -zxf pcre-8.45.tar.gz
cd pcre-8.45
./configure --prefix=/usr/local/pcre
make
make install通过 ./configure --help 可以查看更多编译选项。
下载编译安装 Apache
Apache 2.4 版本和 Apache 2.2 版本的编译安装不太一样,Apache 2.4 需要两个依赖包:apr 和 apr-util,而 Apache 2.2 则不需要。
Apache 可移植运行时( Apache Portable Runtime,简称APR)是 Apache HTTP 服务器的支持库,提供了一组映射到下层操作系统的 API。如果操作系统不支持某个特定的功能,APR 将提供一个模拟的实现。更多关于 APR 的详情,可以访问中文维基。
在 apr 的官网可以下载最新版 apr 和 apr-util,文件格式为 apr-版本.tar.gz 和 apr-util-版本.tar.gz,在 httpd 的官网可以下载最新版 Apache HTTP 服务器,文件格式为 httpd-版本.tar.gz,官网下不动可以选择镜像下载。
本文以 apr-1.7.0、apr-util-1.6.1 和 httpd-2.4.55 为例,使用时请使用最新版,命令只要替换版本号就行了。本文 Apache 安装在 /usr/local/apache 目录下。
cd /usr/local/src
wget https://dlcdn.apache.org//apr/apr-1.7.0.tar.gz
wget https://dlcdn.apache.org//apr/apr-util-1.6.1.tar.gz
wget https://dlcdn.apache.org/httpd/httpd-2.4.55.tar.gz
tar -zxf apr-1.7.0.tar.gz
tar -zxf apr-util-1.6.1.tar.gz
tar -zxf httpd-2.4.55.tar.gz
mv apr-1.7.0 httpd-2.4.55/srclib/apr
mv apr-util-1.6.1 httpd-2.4.55/srclib/apr-util
cd httpd-2.4.55
./configure \
--prefix=/usr/local/apache \
--with-pcre=/usr/local/pcre \
--with-mpm=prefork \
--with-included-apr \
--enable-so \
--enable-ssl \
--enable-modules=all \
--enable-mods-shared=all
make
make installshared 表示动态加载这些模块,如果没有 --enable-mods-shared=all,那么这些模块就是静态加载的。静态加载的模块是通过 <ifmodule></ifmodule> 来配置,动态使用 LoadMoule 来加载。动态加载的模块只要去除配置文件前面的注释符就能启用相应模块,所以说动态加载更加方便,但是官方说会有大约 5% 的性能损失。
可以使用 ./configure --help | grep disable 来查看哪些模块是默认会加载的,默认会加载的可以不用在 ./configure 指定。
要查看编译的参数,请点此进入官方 Apache 2.4 的手册,手册的内容比较容易,通过 Google 翻译一般都看得懂(=・ω・=)
设置 Apache
前面已经把 Apache 安装完成了,接下来要把 Apache 的文件复制到 /etc/init.d/ 中注册成服务、设置开机自启动、编辑 Apache 的配置文件、设置 Apache 目录等。本文中网站的默认目录是 /data/www/default/。
cp -f /usr/local/apache/bin/apachectl /etc/init.d/httpd
#拷贝文件到init.d里
sed -i '2a # chkconfig: - 85 15' /etc/init.d/httpd
sed -i '3a # description: Apache is a World Wide Web server. It is used to server' /etc/init.d/httpd
#想要将Apache作为服务启动必须要这两句,不要问为什么,它就是这样的
chkconfig --add httpd
#将httpd设置为服务
chkconfig httpd on
#将httpd的2、3、4、5运行级设置为On,即开机启动
rm -rf /etc/httpd
ln -s /usr/local/apache/ /etc/httpd
#/etc/httpd为运行目录
cd /usr/sbin/
ln -fs /usr/local/apache/bin/httpd
ln -fs /usr/local/apache/bin/apachectl
#将这两个文件链接过来,以适应init.d脚本
cd /var/log
rm -rf httpd/
ln -s /usr/local/apache/logs httpd
#将日志链接到log目录
groupadd apache
useradd -g apache -s /sbin/nologin apache
#添加apache组和apache用户
mkdir -p /data/www/default/
chmod -R 755 /data/www/default/
#创建apache的默认目录,并设置权限接下来就是配置 Apache 的配置文件 httpd.conf 了。
注:以下配置 shell 亲测有效,全复制下来黏贴就行, 请不要多次运行 。
#先配置/usr/local/apache/conf/httpd.conf文件
cp -f /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.bak
sed -i 's@^#LoadModule\(.*\)mod_socache_shmcb.so@LoadModule\1mod_socache_shmcb.so@' /usr/local/apache/conf/httpd.conf
sed -i 's@^#LoadModule\(.*\)mod_deflate.so@LoadModule\1mod_deflate.so@' /usr/local/apache/conf/httpd.conf
sed -i 's@^#LoadModule\(.*\)mod_deflate.so@LoadModule\1mod_deflate.so@' /usr/local/apache/conf/httpd.conf
sed -i 's@^#LoadModule\(.*\)mod_expires.so@LoadModule\1mod_expires.so@' /usr/local/apache/conf/httpd.conf
sed -i 's@^#LoadModule\(.*\)mod_ssl.so@LoadModule\1mod_ssl.so@' /usr/local/apache/conf/httpd.conf
sed -i 's@^#LoadModule\(.*\)mod_rewrite.so@LoadModule\1mod_rewrite.so@' /usr/local/apache/conf/httpd.conf
sed -i 's@^User daemon@User apache@' /usr/local/apache/conf/httpd.conf
sed -i 's@^Group daemon@Group apache@' /usr/local/apache/conf/httpd.conf
sed -i 's/^ServerAdmin you@example.com/ServerAdmin administrator@ttionya.com/' /usr/local/apache/conf/httpd.conf
sed -i 's@^#ServerName www.example.com:80@ServerName localhost@' /usr/local/apache/conf/httpd.conf
sed -i '/^<Directory \/>/a\
Options FollowSymLinks\
AllowOverride None\
delttionya' /usr/local/apache/conf/httpd.conf
sed -i '/delttionya/,+2d' /usr/local/apache/conf/httpd.conf
sed -i "s@^DocumentRoot.*@DocumentRoot \"/data/www/default\"@" /usr/local/apache/conf/httpd.conf
sed -i "s@^<Directory \"/usr/local/apache/htdocs\">@<Directory \"/data/www/default\">@" /usr/local/apache/conf/httpd.conf
sed -i 's@Options Indexes FollowSymLinks@Options +Includes -Indexes@' /usr/local/apache/conf/httpd.conf
sed -i 's@DirectoryIndex index.html@DirectoryIndex index.php index.html@' /usr/local/apache/conf/httpd.conf
sed -i 's@^ErrorLog \"logs/error_log\"@ErrorLog \"| /usr/local/apache/bin/rotatelogs /usr/local/apache/logs/error_log_%Y%m%d.log 86400\"@' /usr/local/apache/conf/httpd.conf
sed -i 's@CustomLog \"logs/access_log\" common@#CustomLog \"logs/access_log\" common@' /usr/local/apache/conf/httpd.conf
sed -i 's@#CustomLog \"logs/access_log\" combined@CustomLog \"| /usr/local/apache/bin/rotatelogs /usr/local/apache/logs/access_log_%Y%m%d.log 86400\" combined@' /usr/local/apache/conf/httpd.conf
sed -i "s@AddType\(.*\)Z@AddType\1Z\n AddType application/x-httpd-php .php .phtml\n AddType application/x-httpd-php-source .phps@" /usr/local/apache/conf/httpd.conf
sed -i 's@^#Include conf/extra/httpd-mpm.conf@Include conf/extra/httpd-mpm.conf@' /usr/local/apache/conf/httpd.conf
sed -i 's@^#Include conf/extra/httpd-vhosts.conf@Include conf/extra/httpd-vhosts.conf@' /usr/local/apache/conf/httpd.conf
sed -i 's@^#Include conf/extra/httpd-default.conf@Include conf/extra/httpd-default.conf@' /usr/local/apache/conf/httpd.conf
cat >> /usr/local/apache/conf/httpd.conf << EOF
ServerTokens ProductOnly
ServerSignature Off
EOF
#接下来配置/usr/local/apache/conf/extra/httpd-vhosts.conf文件
rm -f /usr/local/apache/conf/extra/httpd-vhosts.conf
cat > /usr/local/apache/conf/extra/httpd-vhosts.conf << EOF
<VirtualHost *:80>
DocumentRoot "/data/www/default/"
ServerName localhost
DirectoryIndex index.php index.html
<Directory "/data/www/default/">
Options +Includes -Indexes
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
EOF
#再配置/usr/local/apache/conf/extra/httpd-default.conf文件
sed -i 's@Timeout 60@Timeout 120@' /usr/local/apache/conf/extra/httpd-default.conf
sed -i 's@MaxKeepAliveRequests 100@MaxKeepAliveRequests 1024@' /usr/local/apache/conf/extra/httpd-default.conf更多Apache配置文件内容请见官网。
启动,测试 Apache
Apache 的启动、关闭、重启命令:/etc/init.d/httpd (start|stop|restart)
在 /data/www/default/ 创建一个 index.html 文件,写上一段话,开启 httpd 服务,如果能显示这段话,那么表示 Apache 安装启动成功。
cat > /data/www/default/index.html << EOF
<html><head><title>测试</title></head><body>Success!</body></html>
EOF
#写一个HTML页面
/etc/init.d/httpd start
#启动Apache
sed -i '/^-A INPUT\(.*\) 22 \(.*\)/a\
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT' /etc/sysconfig/iptables
/etc/init.d/iptables restart
#对80端口添加防火墙规则,上面的22表示在ssh的22端口下面添加这条规则,如果改变了ssh端口,请把22改成你的ssh端口,不然会无法添加注:为避免出现没有添加到防火墙规则的情况出现,这里执行完以后请再执行一下 cat /etc/sysconfig/iptables 命令,看里面有没有 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT 这句规则,若没有,请使用 vim /etc/sysconfig/iptables 命令进入 vim,另起一行添加 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT 这条规则。
访问网站 IP,如果出现 Success! 字样,表示安装启动成功。