顯示具有 ngnix 標籤的文章。 顯示所有文章
顯示具有 ngnix 標籤的文章。 顯示所有文章

2016年9月16日 星期五

使用nginx限制連線IP

目的
為了只讓VPN連線進來的連線能訪問網站

編輯nginx設定檔
# vim /etc/nginx/sites-available/test.yourdomain.com.conf
在 location / { } 區塊中新增
location / {
    ...
    allow 10.12.0.0/24; # Openconnect / iPhone,你VPN後的IP區段
    deny all;
}
要注意 location ~ \.php$ { }區塊中沒設的話, 訪問php頁面不會受影響

最後重啟nginx


參考資料:
http://www.cyberciti.biz/faq/linux-unix-nginx-access-control-howto/  Nginx Block And Deny IP Address OR Network Subnets


2016年9月15日 星期四

Letsencrypt 使用心得

前言

想把網站升級到https,避免被gateway的MIS監控
雖然他要看到 https 網站上的內容是可以的,但是如果是要把  client-> server 中間的封包擷取出來分析出內容,以目前技術很難做,他看到的只會是加密的一串不知道什麼東西。除非他用類似 man in the middle attack 的技術,植入一些木馬程式在你的 client 電腦上,讓 client <-> server 中間的加解密失效
但如果是http,那gateway可以看到明文,連cookie也行
所以才說不要隨便亂上一些沒有 https 加密的網站
你如果不信任公司網路的話,一些 http 網站,建議你跳到自己的 server 上

SSH代理

1. Windows 打開 cygwin (需安裝openssh)
# ssh -D 3128 your.server.ip
2. 然後 browser 打開 proxy
底下 localhost 直接連線的設定都要清空,然後在 socket server 那邊填上  127.0.0.1  port 3128,這樣就會透過 ssh tunnel 到你的 server 再連上網站
如圖:
與用shadowsocks方法類似


Letsencrypt 使用限制

Registrations/IP 限制就是每個 IP 只能在 3 小時內發送 10 次請求,如果超過 10 次就會被阻擋,所以申請成功的 Domain,不要隨便砍掉 /etc/letsencrypt/accounts 目錄,否則你就不能重複申請了 => 不是說你的 client -> https server 的連線,是要註冊幾次
Certificates/Domain 限制就是在七天內你的主 Domain 跟 sub Domain 夾起來只能申請五個,所以當你申請五個 domain 時,Letsencrypt 就會阻擋你申請下一個 domain 了,最後要注意的是,Letsencrypt 申請憑證使用期限是 90 天,官方建議如果使用在正式網站,請 60 天重新 renew 一次。

如果某個網站 a.bear.com 沒設dns,直接寫hosts, 這樣還能上http?
這樣也可以上,dns 是 dns

使用lukas2511 的letsencrypt.sh產生證書

Let's Encrypt的腳本不只lukas2511 ,還有其他的。如:
https://github.com/Neilpang/acme.sh
https://github.com/certbot/certbot
這邊以lukas2511 的版本為例
github lukas2511的 letsencrypt.sh 專案後來改名成dehydrated ,需注意
下載最新版本
$ cd ~; git clone https://github.com/lukas2511/letsencrypt.sh.git
$ cd letsencrypt.sh/
把程式安裝到 /etc/letsencrypt.sh/ 下:
# mkdir /etc/letsencrypt.sh/
# cp ~/letsencrypt.sh/dehydrated /etc/letsencrypt.sh/
設定config
# echo "WELLKNOWN=/var/www/letsencrypt" > /etc/letsencrypt.sh/config
# mkdir -p /var/www/letsencrypt/
設定/etc/nginx/nginx.conf  ,主要網址 yourdomain.com 設定的server{}中加入
location /.well-known/acme-challenge/ {
    alias /var/www/letsencrypt/;
}
我有把 /var/www/letsencrypt/ 權限改成apache
產生SSL certificate
# /etc/letsencrypt.sh/dehydrated -c -d yourdomain.com
(成功訊息)
# INFO: Using main config file /etc/letsencrypt.sh/config
Processing yourdomain.com
 + Signing domains...
 + Generating private key...
 + Generating signing request...
 + Requesting challenge for yourdomain.com...
 + Responding to challenge for yourdomain.com...
 + Challenge is valid!
 + Requesting certificate...
 + Checking certificate...
 + Done!
 + Creating fullchain.pem...
 + Done!

成功後產生的檔案都在 /etc/letsencrypt.sh/certs/yourdomain.com/
# ls /etc/letsencrypt.sh/certs/yourdomain.com/ -l
-rw------- 1 root root 1643 Sep 15 03:22 cert-14739097xx.csr
-rw------- 1 root root    0 Sep 15 03:22 cert-14739097xx.pem
-rw------- 1 root root 1643 Sep 15 03:38 cert-14739106xx.csr
-rw------- 1 root root    0 Sep 15 03:38 cert-14739106xx.pem
-rw------- 1 root root 1643 Sep 15 03:49 cert-14739113xx.csr
-rw------- 1 root root    0 Sep 15 03:49 cert-14739113xx.pem
-rw------- 1 root root 1643 Sep 15 03:54 cert-14739116xx.csr
-rw------- 1 root root 2134 Sep 15 03:55 cert-14739116xx.pem
lrwxrwxrwx 1 root root   19 Sep 15 03:55 cert.csr -> cert-14739116xx.csr
lrwxrwxrwx 1 root root   19 Sep 15 03:55 cert.pem -> cert-14739116xx.pem
-rw------- 1 root root 1647 Sep 15 03:55 chain-14739116xx.pem
lrwxrwxrwx 1 root root   20 Sep 15 03:55 chain.pem -> chain-14739116xx.pem
-rw------- 1 root root 3781 Sep 15 03:55 fullchain-14739116xx.pem
lrwxrwxrwx 1 root root   24 Sep 15 03:55 fullchain.pem -> fullchain-14739116xx.pem
-rw------- 1 root root 3243 Sep 15 03:22 privkey-14739097xx.pem
-rw------- 1 root root 3243 Sep 15 03:38 privkey-14739106xx.pem
-rw------- 1 root root 3243 Sep 15 03:49 privkey-14739113xx.pem
-rw------- 1 root root 3243 Sep 15 03:54 privkey-14739116xx.pem
lrwxrwxrwx 1 root root   22 Sep 15 03:55 privkey.pem -> privkey-14739116xx.pem

接著就可以修改 nginx 的 SSL 設定:
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    listen 443 default_server ssl;
    
    # other configs
    ...

    location /.well-known/acme-challenge/ {
        alias /var/www/letsencrypt/;
    }
    ssl_certificate /etc/letsencrypt.sh/certs/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt.sh/certs/yourdomain.com/privkey.pem;
}

listen 443 default_server ssl; 要記得設,不然訪問https 報ERR_CONNECTION_REFUSED 錯誤。
可以檢查443 port有沒有開
# netstat -ant | grep 443
tcp        0      0 0.0.0.0:4433            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN  # 設 listen 後才有
tcp6       0      0 :::4433                 :::*                    LISTEN

重啟nginx
# service nginx restart

配置subdomain 上SSL
server {
    listen  80;
    listen 443 ssl;
    server_name test.yourdomain.com;

    # other configs
    ...

    location /.well-known/acme-challenge/ {
        alias /var/www/letsencrypt/;
    }
    ssl_certificate /etc/letsencrypt.sh/certs/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt.sh/certs/yourdomain.com/privkey.pem;
}
用同一張證書即可

讓不同port的gitlab也掛上SSL
因為我的gitlab走的是不同的port,
在多次修改 /etc/gitlab/gitlab.rb 
external_url 'http://yourdomain.com:1234'
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/privkey.pem"
nginx['custom_gitlab_server_config']="location /.well-known/acme-challenge/ {\n alias /var/www/letsencrypt/;\n}\n"
和重啟gitlab後
# gitlab-ctl reconfigure

仍無法實現 https://yourdomain.com:1234 訪問gitlab,但是關閉系統nginx,只啟動gitlab的nginx時可以

讓cron每天自動檢查並更新
設定 /etc/cron.d/letsencrypt-yourdomain_com (因為 /etc/cron.d/ 裡面的檔名不能有 . 這個符號,用 _ 取代)
0 0 * * * root sleep $(expr $(printf "%d" "0x$(hostname | md5sum | cut -c 1-8)") % 86400); ( /etc/letsencrypt.sh/dehydrated -c -d yourdomain.com; /sbin/service nginx reload ) > /tmp/dehydrated-yourdomain.com.log 2>&1
在cron job 中 $(expr $(printf "%d" "0x$(hostname | md5sum | cut -c 1-8)") % 86400) 
設計是利用機器名稱產生出十六進位 hash 值,抓一部分轉成十進位後除以一天的秒數,得到餘數後先停這個秒數再跑 dehydrated ,這樣可以避免同時間有太多機器到 Let's Encrypt 的伺服器,造成類似 DDoS 的攻擊。

使用wireshark 查封包
選擇網卡後篩選出傳到自己主機的封包
HTTP:
http && ip.addr == x.x.x.x
HTTPS:
ssl && ip.addr == x.x.x.x
刷網頁時記得用ctrl+f5清緩存,否則封包304緩存去看是不準確的
可以發現 ssl 的封包被加密過,http的封包可以看到明文(HTML)
Linux可以用tcpdump

參考資料:
Rellik
https://letsencrypt.tw/   (主要)
https://github.com/lukas2511/dehydrated (主要)
https://blog.wu-boy.com/2015/12/letsencrypt-entering-public-beta-free-ssl/  Letsencrypt 開放申請免費 SSL 憑證
https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubuntu-14-04  How To Create an SSL Certificate on Nginx for Ubuntu 14.04
https://www.foolegg.com/how-to-secure-your-websites-with-lets-encrypt-certificate/  [教學] 如何使用 Let’s Encrypt 加密網站的連線
http://stackoverflow.com/questions/34189199/how-do-i-use-let-s-encrypt-with-gitlab  How do I use let’s encrypt with gitlab?
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/nginx.md  NGINX settings
https://www.digitalocean.com/community/tutorials/how-to-secure-gitlab-with-let-s-encrypt-on-ubuntu-16-04  How To Secure GitLab with Let's Encrypt on Ubuntu 16.04













2016年3月2日 星期三

Codeigniter 搭配 Nginx + PHP-FPM 設定檔


/etc/nginx/sites-available/xxxx
server {
    listen       80;
    server_name  laravel.wuboy.twbbs.org;
    root         /usr/home/git/laravel/public;
 
    access_log /var/log/nginx/laravel_access.log;
    error_log /var/log/nginx/laravel_error.log;
 
    location / {
        index  index.php index.html index.htm;
    }
 
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }
 
    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }
 
    # removes access to "system" folder, also allows a "System.php" controller
    if ($request_uri ~* ^/system)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }
 
    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }
 
    # catch all
    error_page 404 /index.php;
 
    # use fastcgi for all php files
    location ~ \.php$
    {
        fastcgi_pass unix:/tmp/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /usr/local/etc/nginx/fastcgi_params;
        fastcgi_param HTTPS off;
    }
 
    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }
}



參考資料:
https://blog.wu-boy.com/2012/10/php-mvc-framework-nginx-php-fpm/  PHP MVC Framework 搭配 Nginx + PHP-FPM 設定檔


2016年2月26日 星期五

nginx 跑CI 產生500錯誤空白頁

環境:Linode
系統:CentOS 7

把在VM上用Apache跑的CI 2.2.1專案搬到新的Linode上,打開後跑500錯誤空白頁,而且nginx error log 沒有報錯

原因:php-mysql沒有安裝
# php -i | grep mysql
PHP Warning:  Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
安裝 php-mysql
# yum install php-mysql
重啟php-fpm(只重啟nginx無效)
# service php-fpm restart

參考資料:
http://serverfault.com/questions/412683/configuring-nginx-php5-fpm-to-work-with-codeigniter  Configuring Nginx & php5-fpm to work with Codeigniter


2015年11月13日 星期五

nginx 心得

LINUX上有兩個php
/usr/bin/php
/usr/local/php55/bin/php
$ php --version
PHP 5.4.42 (cli)
$ /usr/local/php55/bin/php --version
PHP 5.5.28 (cli)

要怎麼讓nginx 跑的是 /usr/local/php55/bin/php 的php?
=> 看你的  php-fpm 是跑在哪裡啊,nginx 一定得用 php-fpm 的 fastcgi

找不到php-fpm指令,但有在跑
$ which php-fpm
/usr/bin/which: no php-fpm in (/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/bear/bin)
$ ps axu | grep php-fpm
root       827  0.0  0.1 220016  4720 ?        Ss   Sep21   2:51 php-fpm: master process (/etc/php-fpm.conf)                   
www        829  0.0  0.4 231124 17476 ?        S    Sep21  17:56 php-fpm: pool www     
www      22389  0.0  0.3 232648 14560 ?        S    Sep21  17:54 php-fpm: pool www  
www      22563  0.0  0.3 227552 13332 ?        S    Oct05  14:00 php-fpm: pool www

CentOS
查有沒有裝php-cli(編譯裝的查不到)
$ rpm -qa | grep php-cli
查php-cli裝在哪裡(編譯裝的查不到)
$ rpm -qs php-cli

Ubuntu
$ dpkg -L php5-fpm # 列出檔案
$ dpkg -S php5-fpm # 搜尋放在哪

查nginx ( sites-available )設定檔
...
fastcgi_pass   127.0.0.1:9000;
 127.0.0.1:9000 這個是 php-fpm,表示你的 127.0.0.1:9000 這個 port 是 php5.5 開啟來的

就是 sudo service php5-fpm start ( => 去 call /etc/init.d/php5-fpm )所開啟的

查 php5-fpm startup scripts
# vim /etc/init.d/php5-fpm
...
prefix=/usr/local/php55

=> 就是他,只是我沒想到他會去改那隻 startup scripts,不要搞髒他啊 . .

Rellik:
我討厭用一些 pip rvm 之類的東西
因為這實際上並沒有進 centos 官方測試
安裝對應的路徑也不一樣
你隨便用一些奇怪套件安裝方式會把自己系統搞髒
最恐怖的是萬一這類的工具沒有把 dependency 弄好的話
其實很恐怖
他可能會蓋掉一些centos 或是 ubuntu 官方的套件
所以除非有特定版本需求
不然我大部分都還是以官方的套件管理系統為主
這樣管理機器上也不用擔心會踩到地雷 . . .
我在 freebsd 上用 ports 安裝的話就是下載原始碼啊
問題是  linux 上原始碼並不會進套件資料庫去管控起來
所以裝完以後你久了就忘了
根本不記得你有裝過什麼東西
freebsd 上面 ports 下載來的會盡套件資料庫控管
只是你要花時間編譯就是
總之你要自己編譯安裝也是可以
但前提是你要怎麼控管整個 flow ?
不要今天想到缺什麼就自己跑去下載原始碼開始編譯 . . .
系統就是這樣子被搞髒的


參考資料:
http://www.howtogeek.com/howto/ubuntu/see-where-a-package-is-installed-on-ubuntu/  See Where a Package is Installed on Ubuntu



2015年9月1日 星期二

nginx vhost配置

系統:ubuntu 14.04

1. 建立資料夾
# mkdir /var/www/html/test.vm  # 程式資料夾
# mkdir /var/www/html/test.vm/logs # log資料夾

# chmod 755 -R /var/www/html/test.vm
# chown www-data:www-data -R /var/www/html/test.vm  # 我沒做,依然用bear:bear

2. 建立設定檔
# touch /etc/nginx/sites-available/test.vm
編輯設定檔test.vm內容:
server {
    listen 80;
    listen [::]:80;

    root /var/www/html/test.vm;  # 設定你程式的路徑
    index index.html index.htm; 

    server_name test.vm; # 虛擬主機的完整名稱

    location / {
        try_files $uri $uri/ =404;
    }

    access_log /var/www/html/test.vm/logs/access.log; # access_log 的位置
    error_log /var/www/html/test.vm/logs/error.log;
}

3. 啟用虛擬主機
nginx沒有像Apache 有a2ensite的指令來啟用虛擬主機,所以要用ln 去做
# ln -s /etc/nginx/sites-available/test.vm /etc/nginx/sites-enabled/test.vm

4. 重新啟動nginx
# service nginx restart

5. php頁面打不開(連結變成下載php檔案)
在你的test.vm加入下面這一段
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index  index.php;
        include        fastcgi_params;
}

完整的test.vm檔:
server {
    listen 80;
    listen [::]:80;

    root /var/www/html/test.vm;
    index index.html index.htm;

    server_name test.vm;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index  index.php;
        include        fastcgi_params;
    }

    access_log /var/www/html/test.vm/logs/access.log;
    error_log /var/www/html/test.vm/logs/error.log;

}

5.2 Nginx $document_root$fastcgi_script_name 與 $request_filename 的區別
http://serverfault.com/questions/465607/nginx-document-rootfastcgi-script-name-vs-request-filename  Nginx $document_root$fastcgi_script_name vs $request_filename
http://serverfault.com/questions/502776/what-is-the-correct-setting-of-nginxs-fastcgi-param-script-filename  What is the correct setting of Nginx's fastcgi_param SCRIPT_FILENAME?
$request_filename
This variable is equal to path to the file for the current request, formed from directives root or alias and URI request;
這個變數等於當前請求檔案的路徑,從指向的根或alias和URI請求所形成
$document_root
This variable is equal to the value of directive root for the current request;
這個變數等於當前請求的指向的根
$fastcgi_script_name
This variable is equal to the URI request or, if if the URI concludes with a forward slash, then the URI request plus the name of the index file given by fastcgi_index. It is possible to use this variable in place of both SCRIPT_FILENAME and PATH_TRANSLATED, utilized, in particular, for determining the name of the script in PHP.
這個變數等於URI請求,如果URI的結尾是斜線( ex. http:xxx/xx/ ),則該URI請求會加上fastcgi_index所設的索引名字。
$request_filename is just a nicer way of writing it.
$request_filename 只是更好的寫法

6.1 將入口檔改為a.do( 其內容一樣是php )
index 新增 a.do
index index.html index.htm a.do;
在 location / {} 中加入這段
location ~ .*\.(do|php)?$ {
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_param  SCRIPT_FILENAME  /your_a.do_path/$fastcgi_script_name;
    include        fastcgi_params;
}

6.1.1 (AWS nginx編譯安裝)出現錯誤:
2016/xx/xx XX:XX:XX [error] 8866#0: *41 open() "/root/project/test.html" failed (13: Permission denied), client: x.x.x.x, server: user.domain.net, request: "GET /test.html HTTP/1.1", host: "user.domain.net"
原因,nginx編譯安裝只能把程式放在 安裝的路徑下 /usr/local/webserver/nginx/html/ 

6.1.2 出現錯誤:
2016/xx/xx XX:XX:XX [error] 8924#0: *42 FastCGI sent in stderr: "Access to the script '/usr/local/webserver/nginx/html/project/a.do' has been denied (see security.limit_extensions)" while reading response header from upstream, client: 1
6.1.2.2 出現錯誤:(CentOS 7)
頁面顯示 Access Denied.
access log 有記錄,error log 無記錄,一樣用下面的解法解。security.limit_extensions那行註解記得拿掉
解法:
http://stackoverflow.com/questions/23390531/access-denied-403-for-php-files-with-nginx-php-fpm
php-fpm的 /etc/php-fpm.d/www.conf 的security.limit_extensions新增.do
security.limit_extensions = .php .php3 .php4 .php5 .do

6.2 如果網頁伺服器是Apache2, 使a.do可以視為php網頁訪問
http://stackoverflow.com/questions/3555681/why-are-my-php-files-showing-as-plain-text
在 apache/conf/httpd.conf
修改 <IfModule mime_module> 中的
AddType application/x-httpd-php .php => AddType application/x-httpd-php .php .do

7. 設定url rewrite
在 location / {} 中加入這段
rewrite ^/(.+)-(.+).html$ /a.do?controller=$1&action=$2 last;

7.2 apache2
http://stackoverflow.com/questions/13867543/htaccess-rewrite-shtml-and-html-as-php
在網頁根目錄加入.htaccess,內容為
RewriteEngine On
RewriteRule ^(.+)_(.+)\.shtml$ /a.do?controllers=$1&actions=$2 [L,QSA]

8. 設定alias 資料夾
在 server {} 中加入
location /phpmyadmin {
    alias /var/www/phpmyadmin/$1;
}
這樣 http://your_domain/phpmyadmin 就能直接訪問 /var/www/phpmyadmin/ 的phpmyadmin

9. 設定多個網址指到同一隻程式(同Apache的ServerAlias)
在server {} 中的server_name加入
server_name test.vm m.test.vm; # 虛擬主機的完整名稱

參考資料:
http://www.arthurtoday.com/2010/09/ubuntu-nginx-virtual-host.html Ubuntu 設定 Nginx 虛擬主機 ( Virtual Host )
http://stackoverflow.com/questions/5687938/nginx-trouble-loading-index-file nginx trouble loading index file



2015年8月10日 星期一

解決php5-fpm ( nginx ) "Call to undefined function json_encode" 錯誤

系統:
ubuntu 14.04

用nginx跑php時找不到json_encode()函數報錯:
Call to undefined function json_encode ... 

但使用apache2跑則正常

檢查:
# dpkg --get-selections | grep php5-json
有安裝php5-json

# apt-get install php5-json
Reading package lists... Done
Building dependency tree
Reading state information... Done
php5-json is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
已安裝,所以沒新東西被裝

但是
# php5-fpm -m | grep json
(無結果)

root@ubuntu:/etc/php5/fpm/conf.d# ls -l
total 0
lrwxrwxrwx 1 root root 32 Jul  3 11:11 05-opcache.ini -> ../../mods-available/opcache.ini
lrwxrwxrwx 1 root root 28 Jul  3 11:11 10-pdo.ini -> ../../mods-available/pdo.ini
lrwxrwxrwx 1 root root 29 Jul  3 11:11 20-curl.ini -> ../../mods-available/curl.ini
lrwxrwxrwx 1 root root 27 Jul  3 11:11 20-gd.ini -> ../../mods-available/gd.ini
lrwxrwxrwx 1 root root 31 Jul  3 11:11 20-mcrypt.ini -> ../../mods-available/mcrypt.ini
lrwxrwxrwx 1 root root 31 Jul  3 11:11 20-mysqli.ini -> ../../mods-available/mysqli.ini
lrwxrwxrwx 1 root root 30 Jul  3 11:11 20-mysql.ini -> ../../mods-available/mysql.ini
lrwxrwxrwx 1 root root 34 Jul  3 11:11 20-pdo_mysql.ini -> ../../mods-available/pdo_mysql.ini
lrwxrwxrwx 1 root root 33 Jul  3 11:11 20-readline.ini -> ../../mods-available/readline.ini
原來json.ini不在php5-fpm的設定裡面

把php5-fpm的conf.d備份,然後重建
root@ubuntu:/etc/php5/fpm# mv conf.d/ conf-old.d
root@ubuntu:/etc/php5/fpm# ln -s /etc/php5/mods-available /etc/php5/fpm/conf.d

然後重啟
# service nginx restart
# service php5-fpm restart  // 注意:重啟nginx是無效的,要重啟動php5-fpm

檢查:
# php5-fpm -m | grep json
json

最後打開網頁,json_encode()可以正常使用






2015年7月9日 星期四

docker centos:7 安裝LNMP

啟動docker
docker run -t -i -p 5566:80 xxx/centos7:latest bash

安裝Nginx 

新增CentOS 7 的Ngnix yum repository ,因為Ngnix 不直接由CentOS提供
$ sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
ps. 如果是CentOS 6 則安裝 epel repository ( CentOS )
$ sudo yum install epel-release
安裝ngnix
$ sudo yum install nginx

啟動php-fpm
# php-fpm

啟動ngnix
因為CentOS 7已不使用service啟動服務,而使用systemctl start <service> , 且使用systemctl 啟動服務時會報錯
# systemctl restart nginx
Failed to get D-Bus connection: No connection to service manager.
原因:
https://github.com/docker/docker/issues/7459#issuecomment-52327775
大意是說:
在container內必須使用程式本身的管理工具去啟動。systemctl不會作用。
ex.
啟動nginx
# nginx
停止nginx
# nginx -s stop
啟動mysql(MariaDB)
切換成mysql使用者
# su mysql
This account is currently not available.
解決mysql使用者不能使用
# vim /etc/passwd
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin  =>
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/bin/bash
重新登陸
# su mysql
啟動mysql
$ cd /usr/libexec/
$ ./mysqld
報錯:
...
151023 10:27:11 [ERROR] mysqld: File '/var/lib/mysql/aria_log_control' not found (Errcode: 13)
原來是aria_log_control這隻檔案有問題
# ls /var/lib/mysql/aria_log_control -l
-rw-rw---- 1 root root 52 Jul  8 10:33 /var/lib/mysql/aria_log_control
把它刪掉
# rm /var/lib/mysql/aria_log_control

再度重啟mysql
$ ./mysqld
報錯:
...
151023 10:36:27 [ERROR] mysqld: Can't create/write to file '/var/lib/mysql/aria_log.00000001' (Errcode: 13)
一樣是權限問題
# ls /var/lib/mysql/aria_log.00000001 -l
-rw-rw----  1 root  root     16384 Jul  8 10:33 aria_log.00000001
更變權限
# cd /var/lib/
# chown mysql:mysql -R mysql/
啟動mysql(成功)
$ ./mysqld

停止mysql
# mysqladmin shutdown

MySQL停止的方法
http://stackoverflow.com/questions/11091414/how-to-stop-mysqld
$ /usr/local/mysql/bin/mysqladmin -u root -p shutdown
或
$ sudo mysqld stop
或
$ sudo /usr/local/mysql/bin/mysqld stop
或
$ sudo mysql.server stop

php頁面打不開(連結變成下載php檔案) [ 與 nginx vhost配置 類似問題 ]
在 /etc/nginx/conf.d/default.conf 中server{}區塊解除註解這一段,並修改
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    #root           html;
    root /usr/share/nginx/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
如果上面那段root和fastcgi_param 維持原值任一一個沒改,頁面會出現File not found,nginx error log會報下面錯誤
2015/11/16 09:08:00 [error] 941#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.x.x, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.x.x:5566"


請勿解除註解下面這一段
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
不然會報錯
2015/11/16 08:22:55 [alert] 837#0: 1024 worker_connections are not enough
2015/11/16 08:22:55 [error] 837#0: *1022 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /info.php HTTP/1.0", upstream: "http://127.0.0.1:80/info.php", host: "127.0.0.1"


最後瀏覽器打開
http://192.168.x.x:5566/
就能訪問你docker上的php程式了




參考資料:
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7  # How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7
https://bbs.archlinux.org/viewtopic.php?id=160277 [SOLVED] Cannot start mysqld after switching to MariaDB
https://mariadb.com/kb/en/mariadb/problem-while-starting-mariadb-server/ problem while starting mariadb server?
http://www.ahlinux.com/nginx/4936.html  nginx fastcgi错误primary script unknown如何解决
http://serverfault.com/questions/406158/nginx-php5-fpm-file-not-found  Nginx + php5-fpm = “File not found”