NGINX+APACHE+PHP+MYSQL整合

通过清心醉

NGINX+APACHE+PHP+MYSQL整合

NGINX+APACHE+PHP+MYSQL整合

#需要的朋友可以下载文档,复制过来的文章修改的部分红色标准没有了.可能对新手来说看的会比较辛苦.

APACHE和NGINX各有优势。APACHE来说吧,处理动态是第一的选择,同时他还有丰富的扩展支持,可是占用的内存实在不敢想象,而且就作者4GB内存的VPS来说吧,一个AB命令基本就已经进入负荷状态了。NGINX对静态的支持可以说是目前最好的,占用资源也低,但如果处理动态数据,就经常出错了。

为此,要让APACHE和NGINX同时存在,让APACHE处理后端的动态数据,NGINX处理前端的静态页面。

首先,安装NGINX:

在 /etc/yum.repos.d/ 目录下,建立名叫nginx.repo的软件源配置文件。
默认是没有nginx的软件源的,自己增加个下载配置.
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@qingxinzui ~]# yum install nginx

接着我们在/etc/nginx下创建两个文件:分别是proxy.conf和proxy.conf

写入内容:

#proxy.conf

proxy_redirect          off;

proxy_hide_header      Vary;

proxy_set_header        Accept-Encoding ”;

proxy_set_header        Host            $host;

proxy_set_header        X-Real-IP      $remote_addr;

proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size    10m;

client_body_buffer_size 128k;

proxy_connect_timeout  90;

proxy_send_timeout      90;

proxy_read_timeout      90;

proxy_buffer_size      4k;

proxy_buffers          32 4k;

proxy_busy_buffers_size 64k;

 

#gzip.conf

gzip on;

gzip_http_version 1.0;

gzip_disable      “MSIE [1-6]\.”;

gzip_disable      “Mozilla/4”;

gzip_comp_level  3;

gzip_proxied      any;

gzip_vary        on;

gzip_buffers      4 16k;

gzip_min_length  1100;

gzip_types        text/plain text/xml text/css application/xml application/xhtml+xml application/rss+xml application/atom_xml application/javascript application/x-javascript;

 

接着我们修改下nginx.conf,添加#########内的两行(即刚才写的配置文件)

[root@qingxinzui ~]# vi/etc/nginx/nginx.conf

include       /etc/nginx/mime.types;

##########################################

include       /etc/nginx/proxy.conf;

include       /etc/nginx/gzip.conf;

##########################################

 

接下来修改默认站点的配置文件:

[root@qingxinzui ~]# vi/etc/nginx/conf.d/default.conf

server {

#其他内容…

location / {

root   /var/www/html;   #修改网站目录

index  index.php index.html index.htm; #修改index.php头

}

#其他内容…

location ~ .*\.(php?|cgi|pl|py)$ {

proxy_pass   http://127.0.0.1:9999; #这是将以上4个脚本类型交给9999的APACHE端口处理

}

location ~ /\.ht {

deny  all;

}

location ~ /.+\.(inc|conf|cnf) {

deny  all;

}

#其他内容…

}

 

好了,到这里位置,NGINX的前端配置基本已经完成了,接下来是后端的APACHE

[root@qingxinzui ~]# yum install httpd

[root@qingxinzui ~]# vi /etc/httpd/conf/httpd.conf

#修改以下5项->

Listen 80 -> Listen 127.0.0.1:9999

Options Indexes FollowSymLinks -> Options ExecCGI FollowSymLinks

AllowOverride None -> AllowOverride All

ServerSignature On -> ServerSignature Off

#AddHandler cgi-script .cgi -> AddHandler cgi-script .cgi .pl .py

 

安装PHP、MYSQL支持

[root@qingxinzui ~]# yum -y install php php-mysql php-gd php-xml php-mbstring mysql-server

安装mcrypt模块:

CentOS官方已经不再对mcrypt模块进行支持,作者所以使用了Fedora的扩展库,EPEL (Extra Packages for Enterprise Linux).

32位的可以去:http://mirrors.sohu.com/fedora-epel/6/i386/

64位的:http://mirrors.sohu.com/fedora-epel/6/x86_64/

搜索epel-release

如作者的64,下载扩展先:

rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86
_64/epel-release-6-8.noarch.rpm

完成后#yum update 更新下
在中间会有个提示更新其他的咨询是否下载,按N,否则系统就会全部进行更新下载。

然后查看#yum repolist

[root@qingxinzui ~]# yum repolist

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.mirror.ndchost.com
* epel: mirrors.solfo.com
* extras: mirror-centos.hostingswift.com
* updates: mirror.hmc.edu
repo id       repo name                                            status
base          CentOS-6 – Base                                       6,367
epel          Extra Packages for Enterprise Linux 6 – x86_64       11,128
extras        CentOS-6 – Extras                                        15
updates       CentOS-6 – Updates                                    1,608
repolist: 19,118

说明已经有了.接下来是安装:

[root@qingxinzui ~]# yum –y install php-mcrypt

 

安装php加速器eaccelerator和php-pecl-memcached模块,提高性能

[root@qingxinzui ~]# yum install php-eaccelerator php-pecl-memcached

使用默认设置已经足够使用了。

 

好了,我们在/var/www/html文件夹内创建个index.php并写入以下代码:

<?php

phpinfo();

?>

 

#接着启动nginx|httpd|mysql

[root@qingxinzui ~]# service nginx restart

[root@qingxinzui ~]# service httpd restart

[root@qingxinzui ~]# service mysqld restart

 

访问:127.0.0.1

phpinfo()函数打印出了所有的PHP信息,但这样我们不知道,是否动态脚本完全提交给了APACHE?

很简单:关闭HTTPD服务

[root@qingxinzui ~]# service httpd stop

再次访问:127.0.0.1

An error occurred.

Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check theerror log for details.

Faithfully yours, nginx.

看来动态脚本的确是有提交给APACHE,以上就是说明没有PHP支持。

为了保险起见,在/var/www/html文件夹内创建个index.html写入内容:NGINX静态测试

看看在关闭了HTTPD的情况下,NGINX是否可以独立打开。

访问:127.0.0.1

可以访问。

表示NGINX处理前端,APACHE处理后端的基础配置已经基本完成。

关于作者

清心醉 administrator

发表评论

如果喜欢作者的文章,您可以打赏给作者:

TRC20(虚拟货币):


ERC20(虚拟货币):


Bitcoin(BTC):