環境: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,如需轉載請自行聯系原作者