环境:Linux CentsOs 6.7 32位
任务:搭建web环境:Linux--nginx-php-mysql
(1)安装PHP包括一些附加件:
1
<code>yum </code><code>install</code> <code>php php-mysql php-gd php-imap php-ldap php-mbstring php-odbc php-pear php-xml php-xmlrpc</code>
(2)安装MySQL客户端及服务器:
2
3
4
5
6
7
8
9
10
11
<code>yum -y </code><code>install</code> <code>mysql </code><code>#安装客户端 可以查找一下 yum search mysql</code>
<code>yum </code><code>install</code> <code>mysql-server </code><code>#安装服务器</code>
<code>chkconfig --level 345 mysql on </code><code>#设置mysql自启动 可以不要</code>
<code>service mysqld start </code><code>#开启mysql服务</code>
<code>mysql_secure_installation </code><code>#设置mysql用户根据英文提示完成配置,需要给root用户设置密码,</code>
<code>#其他按需要设置,一般是回车到底 第一次使用mysql没有密码直接回车,然后会提示给root用户设置</code>
<code>密码,然后直接一直回车就OK了。</code>
(3)安装nginx:
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<code>#直接yum安装需要更新一下yum源,方法及文件如下:</code>
<code>#1.在 /etc/yum.repos.d 文件夹下新建 nginx.repo 文件</code>
<code>cd</code> <code>/etc/yum</code><code>.repos.d</code>
<code>vim nginx.repo</code>
<code>#添加以下内容 :</code>
<code>[nginx]</code>
<code>name=nginx repo</code>
<code>baseurl=http:</code><code>//nginx</code><code>.org</code><code>/packages/centos/</code><code>$releasever/$basearch/</code>
<code>gpgcheck=0</code>
<code>enabled=1</code>
<code>#查看yum是否完成更新:</code>
<code>yum list| </code><code>grep</code> <code>nginx</code>
<code>返回内容(类似即可,版本不同可能会有差异):</code>
<code>nginx.i386 1.10.1-1.el6.ngx @nginx </code>
<code>nginx-debug.i386 1.8.0-1.el6.ngx nginx </code>
<code>nginx-debuginfo.i386 1.10.1-1.el6.ngx nginx </code>
<code>nginx-module-geoip.i386 1.10.1-1.el6.ngx nginx </code>
<code>nginx-module-image-filter.i386 1.10.1-1.el6.ngx nginx </code>
<code>nginx-module-njs.i386 1.10.1.0.0.20160414.1c50334fbea6-1.el6.ngx</code>
<code> </code><code>nginx </code>
<code>nginx-module-perl.i386 1.10.1-1.el6.ngx nginx </code>
<code>nginx-module-xslt.i386 1.10.1-1.el6.ngx nginx </code>
<code>nginx-nr-agent.noarch 2.0.0-9.el6.ngx nginx </code>
<code>pcp-pmda-nginx.i686 3.10.9-6.el6 base</code>
<code>#yum源更新完毕,开始安装nginx</code>
<code> </code>
<code>yum </code><code>install</code> <code>-y nginx</code>
<code>#安装完成</code>
(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版本一致,这是必须的,切记!
<code>yum </code><code>install</code> <code>-y php-fpm </code><code>#有需要的可以先查找下,yum search php-fpm</code>
<code> </code><code>#可能需要更新解决依赖,yum会搞定...只需要看着安装完成就可以了</code>
(5)配置
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<code>1.配置 ngingx :</code>
<code># nginx 的配置文件在 /etc/nginx/ 下 和 /etc/nginx/conf.d </code>
<code>(1)</code><code>/etc/nginx/</code><code>文件下的配置文件是 nginx.conf</code>
<code>#该配置文件不需要配置</code>
<code>#nginx.conf配置解释:http://blog.csdn.net/tjcyjd/article/details/50695922</code>
<code>(2) </code><code>/etc/nginx/conf</code><code>.d 下的配置文件 default.conf</code>
<code> </code><code>cd</code> <code>/etc/nginx/conf</code><code>.d</code>
<code> </code><code>vim default.conf</code>
<code>server {</code>
<code> </code><code>listen 80;</code>
<code> </code><code>server_name localhost;</code>
<code> </code><code>#charset koi8-r;</code>
<code> </code><code>#access_log /var/log/nginx/log/host.access.log main;</code>
<code> </code><code>location / {</code>
<code> </code><code>root </code><code>/usr/share/nginx/html</code><code>;</code>
<code> </code><code>index index.html index.htm;</code>
<code> </code><code>#修改为:index index.html index.htm index.php;</code>
<code> </code><code>}</code>
<code> </code><code>#error_page 404 /404.html;</code>
<code> </code><code># redirect server error pages to the static page /50x.html</code>
<code> </code><code>#</code>
<code> </code><code>error_page 500 502 503 504 </code><code>/50x</code><code>.html;</code>
<code> </code><code>location = </code><code>/50x</code><code>.html {</code>
<code> </code><code># proxy the PHP scripts to Apache listening on 127.0.0.1:80</code>
<code> </code><code>#location ~ \.php$ {</code>
<code> </code><code># proxy_pass http://127.0.0.1;</code>
<code> </code><code>#}</code>
<code> </code>
<code> </code><code># pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</code>
<code>#nginx 不像Apache 它是将php文件交给php执行才能正常显示,通过上句可以它是通过 9000 端口发给PHP的</code>
<code> </code><code>location ~ \.php$ {</code>
<code> </code><code>root html;</code>
<code> </code><code>fastcgi_pass 127.0.0.1:9000;</code>
<code> </code><code>fastcgi_index index.php;</code>
<code> </code><code>fastcgi_param SCRIPT_FILENAME </code><code>/script</code><code>$fastcgi_script_name;</code>
<code> </code><code>#修改为:fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;</code>
<code>#/usr/share/nginx/html 为php文件所在地址</code>
<code> </code><code>include fastcgi_params;</code>
<code> </code><code># deny access to .htaccess files, if Apache's document root</code>
<code> </code><code># concurs with nginx's one</code>
<code> </code><code>#location ~ /\.ht {</code>
<code> </code><code># deny all;</code>
<code>}</code>
<code>2.配置php-fpm :</code>
<code>#配置文件为: /etc/php-fpm.conf 和 /etc/php-fpm.d/www.conf</code>
<code>#因为其默认配置中监听的端口为 9000 所以不需要修改,可以直接使用</code>
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)测试
<code>#在 /usr/share/nginx/html 下</code>
<code>#将自带的 index.html 重命名或者删除</code>
<code>cd</code> <code>/usr/share/nginx/html</code>
<code>#重命名:</code>
<code>mv</code> <code>index.html index.html.bak</code>
<code>#删除:</code>
<code>rm</code> <code>-f index.html</code>
<code>#新建php文件:</code>
<code>vim index.php</code>
<code><?php</code>
<code> </code><code>phpinfo(); </code>
<code>?></code>
<code>#开启服务:</code>
<code>service nginx restart</code>
<code>service php-fpm restart</code>
<code>#主机访问:127.0.0.1</code>
本文转自 nw01f 51CTO博客,原文链接:http://blog.51cto.com/dearch/1790382,如需转载请自行联系原作者