本文仅包含最基础的内容,只能让 Apache 2.x 和 PHP 能够正常工作。
1. 安装Apache2
Ubuntu执行以下命令安装最新版本的 Apache 2.4.18:
who@ubuntu:~$ sudo apt-get install apache2
Centos执行以下命令安装Apache:
[who@centos ~]# sudo yum -y install httpd
检查是否可以正常重启 :
who@ubuntu:~$ sudo apachectl restart AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Ubuntu编辑 /etc/apache2/apache2.conf,Centos编辑/etc/httpd/conf/httpd.conf,增加一行 ServerName localhost
,然后执行 sudo apachectl restart,一般就可以正常重启了。
这时候可以查看到 Apache 进程:
who@ubuntu:~$ sudo netstat -antup Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name ...... tcp6 0 0 :::80 :::* LISTEN 2389/apache2 ......
在浏览器中访问 http://127.0.0.1/index.html,可以看到 Apache2 Ubuntu Default Page,该页面默认位于 /var/www/html 目录下。
2. 安装PHP
Ubuntu中,执行以下命令安装最新版本的 PHP 7.0.32:
who@ubuntu:~$ sudo apt-get install php
Centos中,默认的PHP可能不包含Apache配置所需的libphp*.so,可以先卸载:
[who@centos ~]# sudo rpm -qa | grep php php-fpm-5.4.45-8.el6.remi.x86_64 php-common-5.4.45-8.el6.remi.x86_64 php-cli-5.4.45-8.el6.remi.x86_64 [who@centos ~]# sudo rpm -e php-fpm [who@centos ~]# sudo rpm -e php-cli [who@centos ~]# sudo rpm -e php-common
查看什么安装包带有libphp*.so:
[who@centos ~]# sudo yum whatprovides */libphp*.so php-5.3.3-49.el6.x86_64 : PHP scripting language for creating dynamic web sites Repo : base Matched from: Filename : /usr/lib64/httpd/modules/libphp5.so php-embedded-5.3.3-49.el6.x86_64 : PHP library for embedding in applications Repo : base Matched from: Filename : /usr/lib64/libphp5-5.3.3.so Filename : /usr/lib64/libphp5.so ...
执行以下命令安装带有libphp*.so的PHP版本:
[who@centos ~]# sudo yum -y install php
创建 /var/www/html/index.php:
<?php echo phpinfo(); ?>
在浏览器中访问 http://127.0.0.1/index.php,Ubuntu中的Apache还不能正常显示 php 页面,而Centos中的Apache一般可以正常显示php页面。
3. 配置Apache2
1) 在 Apache 中 加载 PHP 模块。
Ubuntu需要先安装额外的包,才能让Apache加载 PHP 模块:
who@ubuntu:~$ sudo apt-get install libapache2-mod-php7.0 who@ubuntu:~$ sudo a2enmod php7.0
重启 Apache 可能产生错误:
who@ubuntu:~$ sudo apachectl restart Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.
这是因为 php7 无法使用 mod_mpm_event,但可以使用 mod_mpm_prefork,因此需要依次执行以下命令:
who@ubuntu:~$ sudo a2dismod mpm_event who@ubuntu:~$ sudo a2enmod mpm_prefork who@ubuntu:~$ sudo apachectl restart
Centos中,如果Apache没有正常显示php页面,之前安装PHP时我们选择的版本带有libphp*.so,因此只需要将libphp*.so文件硬拷贝/etc/httpd/modules目录中,然后编辑/etc/httpd/conf/httpd.conf,增加一行 LoadModule php5_module modules/libphp5.so
,重启Apache即可。如果PHP不带有libphp*.so,也可以通过如下命令安装:
[who@centos ~]# yum -y install mod_php
2) 告知 Apache 将扩展名 .php 解析成 PHP。
Ubuntu编辑 /etc/apache2/apache2.conf,Centos编辑/etc/httpd/conf/httpd.conf,增加一行 AddType application/x-httpd-php .php
即可。但是为了避免潜在的危险,例如有人恶意上传或者创建类似 exploit.php.jpg 的文件却被当做 PHP 执行,推荐使用另一种方式,在conf文件中增加如下内容:
<FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
重启Apache 后在浏览器中访问 http://127.0.0.1/index.php,可以成功显示 php 页面。
3) 将目录的默认索引页面改为 index.php。
Apache 默认索引页面为 index.html,如果要改为 index.php,Ubuntu编辑 /etc/apache2/apache2.conf,Centos编辑/etc/httpd/conf/httpd.conf,增加一行 DirectoryIndex index.php
即可。但如果网站目录中有 index.html,仍会先索引 index.html。
4) 实现URL重写与重定向。
需要加载 rewrite 模块。Ubuntu编辑 /etc/apache2/apache2.conf,Centos编辑/etc/httpd/conf/httpd.conf,增加一行 LoadModule rewrite_module modules/mod_rewrite.so
;也可以执行 sudo a2enmod rewrite。
通过重写,浏览器无法知道页面位置发生变化,地址栏显示的地址不变。通过重定向,浏览器知道页面位置发生变化,从而改变地址栏显示的地址。重写规则的一些参数可以参考链接。
将目录的默认索引改为 index.php。Ubuntu编辑 /etc/apache2/sites-enabled/000-default.conf,增加内容:
RewriteEngine on RewriteRule ^$ index.php
将 .html 页面映射到 .php 页面。Ubuntu编辑 /etc/apache2/sites-enabled/000-default.conf,增加内容:
RewriteEngine on RewriteRule ^(.*).html$ $1.php [PT]
将 index.html 页面临时重定向到 index.php 页面。Ubuntu编辑 /etc/apache2/sites-enabled/000-default.conf,增加内容:
RewriteEngine on RewriteRule index.html index.php [R=302,NC]
将 index.html 页面永久重定向到 index.php 页面。Ubuntu编辑 /etc/apache2/sites-enabled/000-default.conf,增加内容:
RewriteEngine on RewriteRule index.html index.php [R=301,NC]
3) 修改最大连接数
这里只介绍工作模式为prefork模式的情况。
Ubuntu 编辑 /etc/apache2/mods-enabled/mpm_prefork.conf文件:
<IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 ServerLimit 256 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
Centos 编辑 /etc/httpd/conf/httpd.conf 文件:
<IfModule prefork.c> StartServers 8 MinSpareServers 5 MaxSpareServers 20 ServerLimit 1200 MaxClients 1000 MaxRequestsPerChild 4000 </IfModule>
编辑完必须通过如下命令重启才会生效:
sudo /etc/init.d/apache2 force-reload sudo /etc/init.d/apache2 restart
4) 设置正向代理
需要先加载几个module:
who@ubuntu:~$ sudo a2enmod proxy who@ubuntu:~$ sudo a2enmod proxy_connect who@ubuntu:~$ sudo apachectl proxy_http
编辑 /etc/apache2/sites-enabled/000-default.conf,增加如下内容:
ProxyRequests On ProxyVia On <Proxy *> #Order deny,allow #Deny from all #Allow from 192.168.10.0/24 Allow from all </Proxy>