1. 程式人生 > >Ubuntu 搭建LNMP環境並支援thinkphp框架

Ubuntu 搭建LNMP環境並支援thinkphp框架

環境:ubuntu 16.04
1、快速安裝NMP:

1.1 apt-get update 更新源列表

1.2 apt-get install nginx 安裝nginx;dpkg -S nginx 命令可以搜尋 nginx相關檔案,Nginx的安裝路徑為/etc/nginx;其配置檔案nginx.conf也是在該目錄下,並且在 etc/init.d 下有 nginx的啟動程式,該目錄下的程式都會在系統開啟時啟動。
此外,Nginx的預設網站目錄是 /usr/share/nginx/html/,預設Nginx網站配置檔案為 /etc/nginx/sites-available/default 。

1.3 apt-get install php7.0 php7.0-fpm

安裝php;

1.4 apt-get install mysql-server mysql-client php7.0-mysql 安裝mysql;安裝過程需要為root使用者設定密碼;

2、配置nginx與php-fpm:
注:2.1/2.2沒有配置的話,會出現nginx 502 bad gateway 的提示;
2.1、sock配置(無訪問許可權不可用:會出現111: Connection refused)
php7.1-fpm.sock檔案的位置其實是/var/run/php/php7.1-fpm.sock;所以使用時,我們需要把/etc/php/7.1/fpm/pool.d/www.conf 裡的listen = /run/php/php7.1-fpm.sock

改為 listen = /var/run/php/php7.1-fpm.sock
同樣的,我們需要把/etc/nginx/asites-available/default 裡的改為fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;

2.2、其實上面提到的sock監聽沒有給予許可權是不可用的。所以還有另外一種辦法:
default檔案配置改為:fastcgi_pass 127.0.0.1:9000;
www.conf檔案配置改為:listen = 127.0.0.1:9000
注意把原先的兩條sock監聽的配置註釋了;

2.3、fastcgi.conf 中新增:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

2.4、default檔案中,還要把location ~ \.php$ {}include snippets/fastcgi-php.conf;
的註釋去掉

3、thinkphp 隱藏入口檔案
location / {}里加入這段程式碼:

if (!-e $request_filename) {
    rewrite  ^(.*)$  /index.php?s=/$1  last;
    break;
}

4、mysql配置
3.1 將/etc/mysql/mysql.conf.d/mysqld.cnf 檔案裡bind-address = 127.0.0.1改為 bind-address = 0.0.0.0;否則遠端連線時會出現10061:unknown error的報錯;
3.2 進入mysql,將user表裡user=root 的 host 從localhost 改為 ‘%’,否則會出現Host 'xxx.xxx.xx.xx' is not allowed to connect this MYSQL server