owned this note
owned this note
Published
Linked with GitHub
PHP + MYSQL + NGINX + LINUX
===
###### tags: `PHP` `Linux` `centOS` `Nginx` `MySQL`
===
# 系統資訊
查一下版本資訊,使用 ``` cat /etc/os-release```
```bash=
[root@localhost tmp]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
```
# 安裝NGINX
```bash=
[root@localhost tmp]# sudo yum install nginx
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: ftp.cuhk.edu.hk
* extras: ftp.cuhk.edu.hk
* updates: ftp.cuhk.edu.hk
No package nginx available.
Error: Nothing to do
```
[Sol.](http://stackoverflow.com/questions/27244511/no-package-nginx-available-error-centos-6-5)
要先安裝 EPEL repo 才抓的到
```bash=
yum install epel-release
```
成功了!檢查看看NGINX狀態
```bash=
service nginx start
service nginx status
```
設定為開機時自動啟動
```bash=
[root@localhost tmp]# sudo systemctl enable nginx
````
*--------------下面這段不重要-------------*
在沒有圖形介面下
可以用 traceroute 或 links 或 wget 之類的來 檢查loacal host
之後會補上利用DNS 讓外部可以直接連到VM中的localhost 的設定方法
```bash=
yum install traceroute
```
# 安裝PHP
```bash=
[root@localhost nginx]# yum -y install php
```
成功!
```bash=
[root@localhost nginx]# php -v
PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
[root@localhost nginx]# nginx -v
nginx version: nginx/1.10.2
```
# 安裝 MYSQL
```bash=
yum update
```
```bash=
sudo rpm -Uvh mysql57-community-release-el6-n.noarch.rpm
```
找不到?
```bash=
yum install wget
```
```bash=
wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
```
找到了!抓下來...
安裝
```bash=
yum localinstall mysql57-community-release-el7-7.noarch.rpm
```
確定已經裝好了
```bash=
yum repolist enabled | grep "mysql.*-community.*"
```
安裝發行版...
```bash=
yum install mysql-community-server
```
啟動
```bash=
[root@localhost nginx]# service mysqld start
```
確認狀態
```bash=
[root@localhost nginx]# service mysqld status
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2017-05-03 13:55:19 CST; 1min 32s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 55481 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 55407 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 55484 (mysqld)
CGroup: /system.slice/mysqld.service
└─55484 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
5月 03 13:55:13 localhost.localdomain systemd[1]: Starting MySQL Server...
5月 03 13:55:19 localhost.localdomain systemd[1]: Started MySQL Server.
```
PHP + MYSQL + NGINX 版本資訊
```bash=
[root@localhost nginx]# php -v; mysql -V; nginx -v
PHP 5.4.16 (cli) (built: Nov 6 2016 00:29:02)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
mysql Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using EditLine wrapper
nginx version: nginx/1.10.2
```
[MYSQL版本更新](http://ithelp.ithome.com.tw/questions/10086439)
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04
# php-fpm
***好像不用安裝了 後面版本有內建 不過設定還是要做***
```bash=
yum install php-fpm php-mysql
```
查看 php-fpm 的啟動設定
```bash=
[root@localhost nginx]# systemctl list-unit-files | grep php-fpm
php-fpm.service disabled
```
改成開機啟動
```bash=
[root@localhost nginx]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
```
啟動然後檢查
```bash=
[root@localhost nginx]# systemctl start php-fpm
[root@localhost nginx]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
Active: active (running) since 三 2017-05-03 14:18:30 CST; 9s ago
Main PID: 55678 (php-fpm)
Status: "Ready to handle connections"
CGroup: /system.slice/php-fpm.service
├─55678 php-fpm: master process (/etc/php-fpm.conf)
├─55679 php-fpm: pool www
├─55680 php-fpm: pool www
├─55681 php-fpm: pool www
├─55682 php-fpm: pool www
└─55683 php-fpm: pool www
5月 03 14:18:30 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Man....
5月 03 14:18:30 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manager.
Hint: Some lines were ellipsized, use -l to show in full.
```
編輯
```bash=
vim /etc/php5/fpm/php.ini
```
找到這一行改成 0
```bash=
cgi.fix_pathinfo=0
```
[how-to-install-linux-nginx-mysql-php-lemp-stack](https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04)
```bash=
vim /usr/share/nginx/html/info.php
```
```php=
<?php
phpinfo();
?>
```
```bash=
sudo service nginx restart
```
```bash=
wget http://127.0.0.1/info.php
```
```bash=
[root@localhost html]# wget http://127.0.0.1/info.php
--2017-05-03 18:08:16-- http://127.0.0.1/info.php
正在連接 127.0.0.1:80... 連上了。
已送出 HTTP 要求,正在等候回應... 200 OK
長度: 21 [application/octet-stream]
Saving to: ‘info.php.1’
100%[==================================================>] 21 --.-K/s in 0s
2017-05-03 18:08:16 (1.26 MB/s) - ‘info.php.1’ saved [21/21]
```
# NGINX 設定
### 純文字瀏覽器
在終端機下面要看到網頁結果怎麼辦
上網查一下還真的有終端機下的瀏覽器
Installation of Lynx and Links
https://www.tecmint.com/command-line-web-browsers/
```bash=
links http:127.0.0.1
或是
lynx http:/127.0.0.1
```
不過這不是長久辦法
前輩看到我用termianl開網頁都快QQ惹
教我一招方便的方法可以直接讓外部連進VM上的server
首先先搞清楚PHP NGINX 的關係
NGINX 原本只能代理HTML,為了讓他能夠解析.php結尾的檔名,
必須使用php-fpm來讓nginx認識
php-fpm 系蝦咪挖溝
全名是 FastCGI Process Manager
FastCGI
https://zh.wikipedia.org/wiki/FastCGI
```
快速通用閘道器介面(Fast Common Gateway Interface/FastCGI)是一種讓互動程式與Web伺服器通訊的協定。FastCGI是早期通用閘道器介面(CGI)的增強版本。
FastCGI致力於減少網頁伺服器與CGI程式之間互動的開銷,從而使伺服器可以同時處理更多的網頁請求。
歷史
CGI使外部程式與Web伺服器之間互動成為可能。CGI程式運行在獨立的進程中,並對每個Web請求建立一個進程,這種方法非常容易實現,但效率很差,難以擴展。面對大量請求,進程的大量建立和消亡使操作系統效能大大下降。此外,由於位址空間無法共享,也限制了資源重用。
實現
與為每個請求建立一個新的行程不同,FastCGI使用持續的行程來處理一連串的請求。這些行程由FastCGI伺服器管理,而不是web伺服器。 當進來一個請求時,web伺服器把環境變數和這個頁面請求通過一個socket比如FastCGI行程與web伺服器(都位於本地)或者一個TCP connection(FastCGI行程在遠端的server farm)傳遞給FastCGI行程。
```
這篇有說明他們的關係
https://segmentfault.com/q/1010000004854045
```
Nginx和PHP-FPM的进程间通信有两种方式,一种是TCP,一种是UNIX Domain Socket.
其中TCP是IP加端口,可以跨服务器.而UNIX Domain Socket不经过网络,只能用于Nginx跟PHP-FPM都在同一服务器的场景.用哪种取决于你的PHP-FPM配置:
方式1:
php-fpm.conf: listen = 127.0.0.1:9000
nginx.conf: fastcgi_pass 127.0.0.1:9000;
方式2:
php-fpm.conf: listen = /tmp/php-fpm.sock
nginx.conf: fastcgi_pass unix:/tmp/php-fpm.sock;
其中php-fpm.sock是一个文件,由php-fpm生成,类型是srw-rw----.
UNIX Domain Socket可用于两个没有亲缘关系的进程,是目前广泛使用的IPC机制,比如X Window服务器和GUI程序之间就是通过UNIX Domain Socket通讯的.这种通信方式是发生在系统内核里而不会在网络里传播.UNIX Domain Socket和长连接都能避免频繁创建TCP短连接而导致TIME_WAIT连接过多的问题.对于进程间通讯的两个程序,UNIX Domain Socket的流程不会走到TCP那层,直接以文件形式,以stream socket通讯.如果是TCP Socket,则需要走到IP层,对于非同一台服务器上,TCP Socket走的就更多了.
UNIX Domain Socket:
Nginx <=> socket <=> PHP-FPM
TCP Socket(本地回环):
Nginx <=> socket <=> TCP/IP <=> socket <=> PHP-FPM
TCP Socket(Nginx和PHP-FPM位于不同服务器):
Nginx <=> socket <=> TCP/IP <=> 物理层 <=> 路由器 <=> 物理层 <=> TCP/IP <=> socket <=> PHP-FPM
像mysql命令行客户端连接mysqld服务也类似有这两种方式:
使用Unix Socket连接(默认):
mysql -uroot -p --protocol=socket --socket=/tmp/mysql.sock
使用TCP连接:
mysql -uroot -p --protocol=tcp --host=127.0.0.1 --port=3306
```
了解關係之後可以來編輯設定檔
檔案位置在
***/etc/nginx/conf.d/default.conf***
```
server {
#原本的
#server_name example.toright.com;
#改成想要的名子
server_name test123;
#去哪邊找文件
root /usr/share/nginx/html;
# "/" 這個路徑預設讀那些檔案
index index.html index.php index.htmi index.php;
#設定放LOG檔的地方
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png|ogg|ogv|svg|svgz|eot|otf|woff)(\?.+)?$ {
expires max;
log_not_found off;
}
server_tokens off;
# framework rewrite
location / {
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
# 改成透過 127.0.0.1:9000 連線
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
#$document_root 就是上面設定的server root
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
```
---
再來到 windows 做連到VM的設定
先打開 C:\Windows\System32\drivers\etc\
編輯host檔,加上對應的DNS設定,填上VM上主機的IP 跟我們設定的NGINX SERVER NAME
```
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
192.168.109.128 test123
```
再來就可以在window上打開瀏覽器連到 http://test123 就可以看見nginx的index了。
*甚麼是DNS*
*http://dns-learning.twnic.net.tw/dns/03opDNS.html
DNS分為Client和Server,Client扮演發問的角色,也就是問Server 一個Domain Name,而Server必須要回答此Domain Name的真正IP地址。而當地的DNS先會查自己的資料庫。如果自己的資料庫沒有,則會往該DNS上所設的的DNS尋問,依此得到答案之後,將收到的答案存起來,並回達客戶。真正DNS的運作:有兩種詢問方法,Recursive和Iterative兩種。前面是由DNS代理去問,問的方法是用Iterative方式,後者是由本機直接做Iterative式的詢問。*
---
原本conf.d/default.conf 的設定下去跑 會遇到
[ERROR] php-fpm doesn't create .sock file
var/run/php-fpm/
http://stackoverflow.com/questions/35367676/php-fpm-doesnt-create-sock-file
更改 etc/nginx/conf.d
將原本的 var/run/php-fpm/php-fpm.sock;
改成 fastcgi_pass 127.0.0.1:9000;
*Edit: The real solution here is that the listen in www.conf and fastcgi_pass in nginx configuration have to match. Whether you use sockets or tcp is up to you.*
*The answer was to not use a .sock file at all. in /etc/php-fpm.d/www.conf it has listen = 127.0.0.1:9000 so in my nginx config I put fastcgi_pass 127.0.0.1:9000; instead of using something like fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;*
---
一些指令,用久就背起來了
```
Stop PHP FPM command: service php-fpm stop
Start PHP FPM command: service nginx start
Restart PHP FPM command: service php-fpm restart
Reload PHP FPM command: service php-fpm reload
```
---
Nginx 相關檔案位置:
所有設定檔:/etc/nginx/
主要設定檔:/etc/nginx/nginx.conf
預設設定檔:/etc/nginx/conf.d/default.conf
程序設定檔:/usr/sbin/nginx
log 檔:/var/log/nginx/
html: /usr/share/nginx/html
---
---
Windows 如何清除暫存的 DNS Cache
發表於 2010 年 12 月 16 日 由 Tsung
只要是查詢過的 DNS, 都會暫存一段時間, 於 Windows 要清掉 DNS Cache, 要如何清除?
於 Windows 要清掉 DNS Cache
方法1
執行 cmd
ipconfig /flushdns
看到此行字即完成: successfully flushed the dns resolver cache
方法2
控制台 -> 管理工具 -> 服務
重新啟動 DNS Client 和 DHCP Client 服務即可.
https://blog.longwin.com.tw/2010/12/windows-clear-dns-cache-2010/
---