CentOS 5.5 下配置Nginx+mysql+php

LNMP配置 php

根據網上的一些資料,綜合總結了在Centos5.5 下安裝NMP的方式。同時經過修改配置文件達到解決框架的路由問題。 html

1、安裝前首先使用yum命令安裝、升級所需的程序庫(可選,可是安裝過程當中若是遇到缺乏依賴包的話則必須得安裝了) node

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel ssse2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers mysql

2、安裝MySQL5.1.57 linux

檢查是否已安裝mysql nginx

Rpm –qa|grep mysql c++

Rpm包可用如下命令刪除 rpm –e mysqlXXX –-nodeps git

源碼編譯安裝可用 web

進入上次安裝mysql的源碼包 算法

執行 make uninstall make clean

#useradd mysql 添加用戶

#tar zxvf mysql-5.1.57.tar.gz 解壓包

#cd mysql-5.1.57 進入mysql目錄

編譯前配置

#./configure

--prefix=/usr/local/mysql 安裝位置

--datadir=/videodata/mysqldata/data 數據位置

--with-extra-charsets=all charsets擴展

--with-charset=gbk

--with-charset=utf8 支持字符集GBKUTF-8)

--with-innodb 啓用innodb引擎

注:mysql 默認不支持innodb若是要啓用innodb須要在編譯的時候 --with-innodb

或者–with-plugins=innobase (根據版本不同選擇)

編譯

#make && make install

安裝後的調整:
cp support-files/my-medium.cnf /etc/my.cnf 產生配置文件


初使數據庫:
/usr/local/mysql/bin/mysql_install_db --user=mysql


設置用戶及用戶組:
chown -R root:mysql /usr/local/mysql
chown -R mysql /usr/local/mysql/var

啓動數據庫:
/usr/local/mysql/bin/mysqld_safe --user=mysql &
mysql用戶啓動mysql

修改密碼

#/usr/local/mysql/bin/mysqladmin -u root password 123456 設置MySQL密碼

開機啓動:

#echo 「/usr/local/mysql/bin/mysqld_safe &」 >>/etc/rc.local

備註:

能夠進入mysql 使用show engines;查看是否有innodb引擎

3.安裝nginx

由於nginx的路徑和重寫是須要靠prce來支持的。因此先安裝pcre

檢查是否安裝有pcre

Rpm –qa|grep pcre

#cp /lib/libpcre.so.0 / 保存出來 後面須要用

刪除

Rpm –e pcreXX

#tar zvxf pcre-xxx 解壓pcre源碼包

#cd pcrexxx 進入源碼文件夾

#cp /libpcre.so.0 /lib/ 複製進去

#./configure

//配置PCRE,由於PCRE是一個庫,而不是像pachephp、postfix等這樣的程序,因此咱們安裝時選擇默認路徑便可,這樣會在後面安裝其它東西時避免一些沒必要要的麻煩,執行完這部後會顯示出下圖,上面顯示了咱們對PCRE的配置

編譯安裝

#make && make install

安裝nginx

#tar zvxf nginx-xxxx

#./configure

--with-http_stub_status_module 開啓模塊

--prefix=/usr/local/nginx 安裝路徑

編譯安裝
make&& make install

備註:

若想測試安裝是否成功,可使用 nginx安裝路徑/sbin/nginx 開啓nginx 而後輸入localhost測試,若成功會顯示 welcome to nginx

4.安裝PHP擴展庫

1. libiconv安裝

#tar zxvf libiconv-1.8.tar.gz

#cd libiconv-1.8/

#./configure

--prefix=/usr/local/webserver/libiconv

#make && make install

2. 安裝zlib2 (zlib是提供數據壓縮用的函式庫)

#tar zxvf zlib-1.2.3.tar.gz
#cd zlib-1.2.3
#./configure

--prefix=/usr/local/zlib
#make
#make install

3. 安裝PNG (圖像文件存儲格式)

#tar zxvf libpng-1.2.26.tar.gz
#cd libpng-1.2.26
#cp scripts/makefile.linux ./makefile
#./configure

--prefix=/usr/local/libpng
#make
#make install

4. 安裝freetype (freetype是字體的支持 )

#tar zxvf freetype-2.3.5.tar.gz
#cd freetype-2.3.5
#./configure

--prefix=/usr/local/freetype
#make
#make install

5. 安裝JPEG6 ((圖像文件存儲格式)

#tar zxvf jpegsrc.v6b.tar.gz
#cd jpeg-6b
#mkdir -pv /usr/local/libjpeg/{,bin,lib,include,man/man1,man1}
#./configure

--prefix=/usr/local/libjpeg

--enable-shared --enable-static

#make
#make install

6. 安裝 libxml2 (xml c語言版的解析器)
#tar zxvf libxml2-2.6.31.tar.gz
#cd libxml2-2.6.31
#./configure

--prefix=/usr/local/libxml2
#make
#make install
#cp xml2-config /usr/bin

7. 安裝 libmcrypt-2.5.7.tar.gz (是加密算法擴展庫)
#
tar zxvf libmcrypt-2.5.7.tar.gz
#cd libmcrypt-2.5.7
#./configure
#make
#make install

8. 安裝Fontconfig (字體管理)
#
tar -zxvf fontconfig-2.4.2.tar.gz
#cd fontconfig-2.4.2
#./configure

--prefix=/usr/local/fontconfig --with-freetype-config=/usr/local/freetype/bin/freetype-config

#make
#make install


9. mhash安裝

#tar zxvf mhash-0.9.9.9.tar.gz

#cd mhash-0.9.9.9/

#./configure

#make && make install

(忽略)#/sbin/ldconfig

10 mcrypt安裝

#tar zxvf mcrypt-2.6.8.tar.gz

#cd mcrypt-2.6.8/

#./configure

#make && make install

安裝到mcrypt的時候出現了問題
./configure
提示出錯,首先提示
*** Could not run libmcrypt test program, checking why…
*** The test program failed to compile or link. See the file config.log for the
*** exact error that occured. This usually means LIBMCRYPT was incorrectly installed
*** or that you have moved LIBMCRYPT since it was installed. In the latter case, you
*** may want to edit the libmcrypt-config script: no
configure: error: *** libmcrypt was not found
找了相關資料,得出解決方法明明我安裝了libmcrypt 2.5.8 ,但是居然提示沒有發現!而後在/usr/local/bin/找到了LIBMCRYPT_CONFIG ,這一步明確了是PATH變量的問題
方法是ln -s /usr/local/bin/libmcrypt_config /usr/bin/libmcrypt_config
以後繼續進行./configure
仍然報錯:
*** Could not run libmcrypt test program, checking why…
*** The test program compiled, but did not run. This usually means
*** that the run-time linker is not finding LIBMCRYPT or finding the wrong
*** version of LIBMCRYPT. If it is not finding LIBMCRYPT, you’ll need to set your
*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point
*** to the installed location Also, make sure you have run ldconfig if that
*** is required on your system
***
*** If you have an old version installed, it is best to remove it, although
*** you may also be able to get things to work by modifying LD_LIBRARY_PATH
***
configure: error: *** libmcrypt was not found

繼續查找,仍是變量的問題
解決方法以下:運行 export LD_LIBRARY_PATH=/usr/local/lib: LD_LIBRARY_PATH
而後make make install
完成

11. 安裝GD
#
tar zxvf gd-2.0.35.tar.gz
#cd gd-2.0.35
#./configure

--prefix=/usr/local/libgd

--with-png=/usr/local/libpng

--with-freetype=/usr/local/freetype

--with-jpeg=/usr/local/libjpeg

--with-fontconfig=/usr/local/fontconfig

看到下面的信息, 呵呵 不少都顯示支持了
Support for PNG library: yes
Support for JPEG library: yes
Support for Freetype 2.x library: yes
Support for Fontconfig library: yes
Support for Xpm library: no
Support for pthreads: yes
#make
#make install


(
make時可能會是出現如下錯誤
cd . && /bin/sh /root/gd-2.0.35/config/missing --run aclocal-1.9 -I config
aclocal:configure.ac:64: warning: macro `AM_ICONV' not found in library
cd . && /bin/sh /root/gd-2.0.35/config/missing --run automake-1.9 --foreign
cd . && /bin/sh /root/gd-2.0.35/config/missing --run autoconf
configure.ac:64: error: possibly undefined macro: AM_ICONV
If this token and others are legitimate, please use m4_pattern_allow.
See the Autoconf documentation.
make: *** [configure] Error 1
把報錯信息「configure.ac:64: error: possibly undefined macro: AM_ICONV」拿到google裏搜索,獲得「But you need to have gettext」沒有gettext這個包。
而後直接運行:yum install gettext 在作make 就行了(centos5.5 安裝iso裏面也有 複製gettextXXX.RPMgettext-develepXX.RPM) 安裝

#cp /usr/local/png/include/png.h ./

#cp /usr/local/png/include/pngconf.h ./

#make && make install

5、安裝PHP

若是pdolinux下出現exception 'PDOException' with message 'could not find driver'則問題是php編譯時少加--with-pdo-mysql=/usr/local/mysql

(用這個的話,自動啓動PDO支持mysql了,不須要再次編譯了)

一.編譯的時候開啓php擴展庫

#tar zxvf php-5.2.9.tar.gz

#gzip -cd php-5.2.9-fpm-0.5.10.diff.gz | patch -d php-5.2.9 -p1

#cd php-5.2.9/

#./configure

--prefix=/usr/local/webserver/php

--with-config-file-path=/usr/local/webserver/php/etc

--with-mysql=/usr/local/webserver/mysql

--with-mysqli=/usr/local/webserver/mysql/bin/mysql_config

--with-iconv-dir=/usr/local/webserver/libconv

--with-freetype-dir =/usr…. gd庫須要使用

--with-jpeg-dir =/usrgd庫須要使用

--with-png-dir =/usr gd庫須要使用

--with-zlib zlib是提供數據壓縮用的函式庫

--with-zlib-dir=/usr/local/webserver/zlib

--with-libxml-dir=/usr 使用xml

--enable-xml

--disable-rpath

--enable-discard-path

--enable-safe-mode

--enable-bcmath

--enable-shmop

--enable-sysvsem

-enable-inline-optimization

--with-curl

--with-curlwrappers

--enable-mbregex

--enable-fastcgi

--enable-fpm 啓和nginx的通訊 pphp 5.3.3之後集成了 能夠直接開啓使用

--enable-force-cgi-redirect

--enable-mbstring 開啓mbstring函數

--with-mcrypt

--with-gd 使用gd

--enable-gd-native-ttf

--with-openssl

--with-mhash

--enable-pcntl

--enable-sockets

--with-ldap

--with-ldap-sasl

#make

#make install

#cp php.ini-dist /usr/local/webserver/php/etc/php.ini

#cd ../

注:php5.3.3之後集成了fpm 能夠 ./configure –-prefix=/usr/local/php

– -enable-fpm

不須要再裝php-fpm插件了

可能會出現下面的問題 php5.4.4

Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/sky/soft/php/php-5.3.8/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/

解決方法

5.3.3以上版本

#cp php.ini-production /usr/local/php/lib/php.ini


#vim /usr/local/php/lib/php.ini (
增長/usr/local/php/lib/phpinclude_path)


執行

#/usr/sky/soft/php/php-5.3.8/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin


#ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar

Php5.4.4集成了php-fpm因此可使用如下啓動nginx

#/usr/local/php/sbin/php-fpm –c php.ini(位置)

查看phpnginx是否啓動

#ps -aux | grep nginx

#ps -aux | grep php

Php-fpm 開機啓動

#echo 「/usr/local/php/sbin/php-fpm start」 >>/etc/rc.local

修改nginx配置文件以支持php-fpm

nginx安裝完成後,修改nginx配置文件爲 nginx/conf/nginx.conf

其中server段增長以下配置,注意標紅內容配置,不然會出現No input file specified.錯誤

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

建立測試php文件

建立php文件

/usr/local/nginx/html下建立index.php文件,輸入以下內容

<? echo phpinfo(); ?>

啓動服務

啓動php-fpmnginx

/usr/local/php/sbin/php-fpm (手動打補丁的啓動方式/usr/local/php/sbin/php-fpm start)

sudo /usr/local/nginx/nginx

上傳2M多的附件時,出現以下錯誤
413 Request Entity Too Large
nginx/0.8.48

這是因爲上傳的附件超過了nginx的設置(nginx默認容許上傳的附件的大小爲1M
#vi /usr/local/nginx/conf/nginx.conf


lient_max_body_size 20m; #
能夠根據本身的實際須要去定義上傳附件的大小;

修改完以後,重啓

果斷打開nginx/conf/nginx.conf,在http模塊中增長

client_max_body_size 10m;

nginx/sbin/nginx -s reload

注意:默認nginx是不支持path_info 因此ci等框架的

Localhost/ci/index.php/class/function是用不了的

須要修改nginx文件

新版nginxpathinfo配置小結

(1)設置fastcgi_param SCRIPT_NAME $fastcgi_script_name;

php-cgi若是找不到文件或者沒有權限訪問的話 會提示No input file specified. 或者Access denied.

(2)php.inicgi.fix_pathinfo

cgi.fix_pathinfo boolean

CGI提供了真正的PATH_INFO/PATH_TRANSLATED支持。之前PHP的行爲是將 PATH_TRANSLATED 設爲 SCRIPT_FILENAME,而無論 PATH_INFO 是什麼。有關 PATH_INFO 的更多信息見 cgi 規格。將此值設爲 1 將使 PHP CGI 修正其路徑以遵照規格。設爲 0 將使 PHP 的行爲和從前同樣。默認爲零。用戶應該修正其腳本使用 SCRIPT_FILENAME 而不是 PATH_TRANSLATED

when cgi.fix_pathinfo was set to 「1 it caused a lot of checks in order to find which part of SCRIPT_FILENAME is a file name and which is PATH_INFO. In case of missing file it caused NULL in path_translated, which caused the crash.

因而,修改nginx.confphp部分配置參數以下

紅色爲須要注意的部分

location ~ \.php(/|$) {

#root html

fastcgi_pass 127.0.0.1:9000;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_index index.php;

include fastcgi_params;

}

註釋掉 fastcgi_param 裏的這裏# fastcgi_param SCRIPT_FILENAME $request_filename;

大功告成 ;-)

相關文章
相關標籤/搜索