本系列的lnmp的大框架基本上是按照http://www.linuxzen.com/lnmphuan-jing-da-jian-wan-quan-shou-ce-si-lnmpda-jian-yuan-ma-an-zhuang.html來寫的
#########################update-2015-11-29##########################################
下面的nginx配置文件和php配置文件都有一個小錯誤,之前誤打誤撞通過了,今天復習的時候發現又報 connect() to unix:/var/run/php-fpm/php-fpm.sock failed (2: No such file or directory) 這個錯誤,然后就開始了艱苦卓絕的找錯。后來在stack overflow上找到了一個解決方法:
大意是在/usr/local/php/etc/php-fpm.conf中listen的目錄在/var/run/php-fpm/php-fpm.sock中,但是/var/run是個動態目錄,開機后目錄下的文件都會重新加載初始化,而php-fpm這個目錄根本就不存在,系統也不會為你自動創建,所以改成listen=/var/run/php-fpm.sock即可。同時/usr/local/nginx/nginx.conf的內容也要改。把fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;改成fastcgi_pass unix:/var/run/php-fpm.sock;
user和group網上都是www或者www-data而我用會報錯permission denied,用nobody可以通過。
#########################update-2015-11-29##########################################
今天又花了一天時間裝了php,感覺php的源碼安裝也挺麻煩的,整個過程各種報錯。
下篇文章准備整合所有查找到的報錯和解決方法。
php和nginx一樣,在安裝前需要裝一堆的依賴。有libmcrypt、mcrypt、mhash。其實遠不止這三個包,在后面的configure和make中還會陸陸續續安裝一些依賴。
1、安裝libmcrypt、mcrypt、mhash(整個過程都在/usr/local/src/php目錄下)
#創建/usr/local/src/php目錄並進入
mkdir /usr/local/src/php
cd /usr/local/src/php
#下載libmcrypt,解壓,編譯,安裝
wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz tar -xzvf libmcrypt-2.5.8.tar.gz cd libmcrypt-2.5.8 ./configure make -j2 make install #下載mhash,解壓,編譯,安裝 wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz tar -xzvf mhash-0.9.9.9.tar.gz cd mhash-0.9.9.9 ./configure make -j2 make install # 這兩個包安裝完成后要把動態鏈接庫做一個軟連接到/usr/lib,以為接下來的mcrypt依賴於這兩個包 ln -s /usr/local/lib/libmcrypt* /usr/lib ln -s /usr/local/lib/libmhash.* /usr/lib/ ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config #下載mcrypt,解壓,安裝,編譯 wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download tar -zxvf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 ./configure make -j2 make install
2、生成配置文件(整個過程都在/usr/local/src/php/php-5.6.15目錄下)
wget http://cn2.php.net/distributions/php-5.6.15.tar.gz tar -xzvf php-5.6.15.tar.gz cd php-5.6.15 sudo ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-zlib --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --enable-fpm --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap
在configure的時候要注意,--enable-safe-mode --enable-discard-path --enable-fastcgi --enable-force-cgi-redirect參數從5.3版本開始就默認開啟了,5.4開始不支持這些選項,所以在參考其他教程時要注意有些選項不需要寫到自己的configure中了。我看到的90%的參考教程都寫了一兩個不支持的參數的。當然你如果寫了也沒關系,系統不會計算進去。
有一個選項需要強調一下,就是--enable-fpm,如果你在搭建環境時是需要php和nginx結合的,那么這個選項必寫。由於我是搭建的LNMP,所以這個參數加上了。想要詳細了解選項的,使用./configure -h查看。
我在configure遇到的問題:
①configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/
解決方法:需要安裝curl-devel,ubuntu源中沒有,去官網下載並編譯安裝(也可以直接apt-get下載php5-curl、curl、libcurl4-gnutls-dev)
cd /usr/local/src/php
wget http://curl.haxx.se/download/curl-7.45.0.tar.gz sudo tar xzvf curl-7.45.0.tar.gz cd curl-7.45.0.tar.gz sudo ./configure sudo make -j2 sudo make install
再次configure又遇到問題
②configure: error: sasl.h not found!
解決方法:sudo apt-get install libsasl2-dev
再次使用上面的configure,生成配置文件,沒有再報錯了。
3、編譯安裝(在/usr/local/src/php/php-5.6.15下)
cd /usr/local/src/php/php-5.6.15
sudo make -j2
報錯①:
usr/bin/ld: ext/ldap/.libs/ldap.o: undefined reference to symbol 'ber_strdup@@OPENLDAP_2.4_2'
//usr/lib/x86_64-linux-gnu/liblber-2.4.so.2: error adding symbols: DSO missing from command line
解決方法:
一開始我查資料說是沒有安裝openldap,然后就查了openldap的安裝方法,結果openldap用db做存儲方案,就是說還要編譯安裝db(這個db不是mysql,就叫db)。結果搞了半天db和openldap都安裝完再編譯發現還是報這個錯。最后在網上一個一個試,找到了一個解決方法。原文是這么說的
遇到這種類似的情況,說明「./configure 」沒抓好一些環境變數值。 在PHP源碼目錄下 vi Makefile 找到 EXTRA_LIBS 行,在行末添加 ‘ -llber ‘ 保存退出再次make即可。
cd /usr/local/src/php/php-5.6.15
vim Makefile
找EXTRA_LIBS行,在末尾添加--llber
希望遇到這個問題的同學可以引以為戒不要走彎路了。
再次編譯,又報錯②:
/usr/local/src/php/php-5.6.15/sapi/cli/php: error while loading shared libraies: libmysqlclient.so.20: cannot open shared object file: No such file or directory
解決辦法:
找到libmysqlclient.so.20,並做軟鏈接
shell> find / -name "libmysqlclient.so.20" /usr/local/mysql/lib/libmysqlclient.so.20 /usr/local/src/mysql-5.7.9/libmysql/CMakeFiles/CMakeRelink.dir/libmysqlclient.so.20 /usr/local/src/mysql-5.7.9/libmysql/libmysqlclient.so.20 shell> echo "/usr/local/lib" >>/etc/ld.so.conf shell> echo "/usr/local/mysql/lib" >>/etc/ld.so.conf shell> sudo ldconfig shell> make
再次編譯,又他媽報錯了,此時我的心情真的是崩潰的
報錯③:
chmod: 無法訪問"ext/phar/phar.phar": 沒有那個文件或目錄
解決辦法:
mkdir -p ext/phar/phar.phar
再次編譯,沒有錯了。
make install
4、配置php(在/usr/local/src/php/php-5.6.15下)
cd /usr/local/src/php/php-5.6.15
cp php.ini-production /usr/local/php/php.ini # 如果是開發就復制php.ini-development cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ln -s /usr/local/php/bin/php /usr/bin/
echo "listen = /var/run/php-fpm/php-fpm.sock" >>/usr/local/php/etc/php-fpm.conf
echo “listen = /var/run/php-fpm.sock” >> /usr/local/php/etc/php-fpm.conf
使用nginx服務器等待檢測php是否安裝成功
編輯nginx配置文件:
shell> vi /usr/local/nginx/conf/nginx.conf location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; include fastcgi.conf; }
在/usr/local/nginx/html下創建index.php
shell> cd /usr/local/nginx/html shell> vi /usr/local/nginx/html/index.php <?php phpinfo(); ?>
5、啟動php
mkdir /var/run/php-fpm(這一步在每次啟動系統時都要做,因為/var/run是個動態目錄,系統重啟php-fpm就沒了). /usr/local/php/sbin/php-fpm
報錯①:
ERROR: [pool www] cannot get gid for group 'nobody'
解決方法:
groupadd nobody
關閉php在開啟(不知道php有沒有重啟選項,只能這樣做了)
sudo killall php-fpm
/usr/local/php/sbin/php-fpm
沒報錯了,此時我以為我已經成功了,呵呵,too young too simple
開始檢測是否安裝成功。
開啟nginx:
sudo /usr/lcoal/nginx/nginx
在瀏覽器輸入localhost/index.html
結果:
呵呵呵。。。。。。。。。。。。。。。。。。。。。。。。。
就快要成功了我會輕易放棄?果斷點了個error log,以為他會引領我找到錯誤日志,結果是這么個鳥玩意
等於沒說。后來我一想這是nginx服務器啊,應該看nginx的錯誤日志,進入nginx,果然發現了logs
tail -f error.log,實時查看錯誤日志。再打開一個終端,執行php-fpm,此時error.log刷出了這樣的信息:
此時報錯:connect() to unix:/var/run/php-fpm/php-fpm.sock failed (13: Permission denied) while connecting to upstream
我檢查了一下 /tmp/php-cgi.sock 發現該文件擁有者是root,而nginx和php-fpm都是www用戶來運行的,按理講,這個sock文件也應該是www才對。於是修改php-fpm.conf文件
sudo vim /usr/local/php/etc/php-fpm.conf user=nobody
group=nobody
listen.owner = nobody listen.group = nobody listen.mode = 0660
結果如下圖:
成功了,但是沒有www用戶,猛然想起之前增加的組是nobody,於是又把php-fpm.conf改了
sudo vim /usr/local/php/etc/php-fpm.conf listen.owner = www listen.group = www listen.mode = 0660
在瀏覽器中輸入localhost/index.php
如圖,成功!!!!!!!!