您的位置:首页 > 运维架构 > Nginx

web环境搭建之Linux--nginx-php-mysql

2016-06-17 16:49 796 查看
环境:Linux CentsOs 6.7 32位
任务:搭建web环境:Linux--nginx-php-mysql

(1)安装PHP包括一些附加件:
yum install php php-mysql php-gd php-imap php-ldap php-mbstring php-odbc php-pear php-xml php-xmlrpc
(2)安装MySQL客户端及服务器:
yum -y install mysql         #安装客户端   可以查找一下 yum search mysql

yum install mysql-server     #安装服务器

chkconfig --level 345 mysql on     #设置mysql自启动 可以不要

service mysqld start         #开启mysql服务

mysql_secure_installation  #设置mysql用户根据英文提示完成配置,需要给root用户设置密码,
#其他按需要设置,一般是回车到底 第一次使用mysql没有密码直接回车,然后会提示给root用户设置
密码,然后直接一直回车就OK了。
(3)安装nginx:

#直接yum安装需要更新一下yum源,方法及文件如下:

#1.在  /etc/yum.repos.d 文件夹下新建 nginx.repo 文件
cd /etc/yum.repos.d
vim nginx.repo

#添加以下内容 :
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

#查看yum是否完成更新:

yum list| grep nginx

返回内容(类似即可,版本不同可能会有差异):
nginx.i386                                  1.10.1-1.el6.ngx            @nginx
nginx-debug.i386                            1.8.0-1.el6.ngx             nginx
nginx-debuginfo.i386                        1.10.1-1.el6.ngx            nginx
nginx-module-geoip.i386                     1.10.1-1.el6.ngx            nginx
nginx-module-image-filter.i386              1.10.1-1.el6.ngx            nginx
nginx-module-njs.i386                       1.10.1.0.0.20160414.1c50334fbea6-1.el6.ngx
nginx
nginx-module-perl.i386                      1.10.1-1.el6.ngx            nginx
nginx-module-xslt.i386                      1.10.1-1.el6.ngx            nginx
nginx-nr-agent.noarch                       2.0.0-9.el6.ngx             nginx
pcp-pmda-nginx.i686                         3.10.9-6.el6                base

#yum源更新完毕,开始安装nginx

yum install -y nginx

#安装完成
(4)安装 php-fpm 使nginx解释php(说法不标准):
这个地方是最重要的地方,因为默认情况下Nginx和PHP他俩之间是一点感觉没有的。在之前,很多朋友都搭建过Apache+PHP,Apache+PHP编译后生成的是模块文件,而Nginx+PHP需要PHP生成可执行文件才可以,所以要利用fastcgi技术来实现Nginx与PHP的整合,这个只要我们安装是启用FastCGI即可。此次我们安装PHP不仅使用了FastCGI,而且还使用了PHP-FPM这么一个东东,PHP-FPM说白了是一个管理FastCGI的一个管理器,它作为PHP的插件存在,在安装PHP要想使用PHP-FPM时就需要把PHP-FPM以补丁的形式安装到PHP中,而且PHP要与PHP-FPM版本一致,这是必须的,切记!
yum install -y php-fpm #有需要的可以先查找下,yum search php-fpm
#可能需要更新解决依赖,yum会搞定...只需要看着安装完成就可以了
(5)配置
1.配置 ngingx :

# nginx 的配置文件在 /etc/nginx/ 下 和 /etc/nginx/conf.d

(1)/etc/nginx/文件下的配置文件是 nginx.conf

#该配置文件不需要配置

#nginx.conf配置解释:http://blog.csdn.net/tjcyjd/article/details/50695922

(2) /etc/nginx/conf.d 下的配置文件 default.conf
cd /etc/nginx/conf.d
vim default.conf

server {
listen       80;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

location / {
root   /usr/share/nginx/html;
index  index.html index.htm;
#修改为:index  index.html index.htm index.php;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1; #}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#nginx 不像Apache 它是将php文件交给php执行才能正常显示,通过上句可以它是通过 9000 端口发给PHP的
#
location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME   /script$fastcgi_script_name;
#修改为:fastcgi_param  SCRIPT_FILENAME   /usr/share/nginx/html$fastcgi_script_name;
#/usr/share/nginx/html   为php文件所在地址
include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

2.配置php-fpm :

#配置文件为: /etc/php-fpm.conf 和 /etc/php-fpm.d/www.conf

#因为其默认配置中监听的端口为 9000 所以不需要修改,可以直接使用


注意:这段代码在修改后要把前面的#号注释删掉!

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
#修改为:fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
#/usr/share/nginx/html 为php文件所在地址
include fastcgi_params;
}
(6)测试
#在 /usr/share/nginx/html 下
#将自带的 index.html 重命名或者删除
cd /usr/share/nginx/html
#重命名:
mv index.html index.html.bak
#删除:
rm -f index.html

#新建php文件:
vim index.php

<?php
phpinfo();
?>

#开启服务:
service nginx restart
service php-fpm restart

#主机访问:127.0.0.1
本文出自 “启思・朝圣者” 博客,请务必保留此出处http://dearch.blog.51cto.com/10423918/1790382
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: