Php環境搭建php
http://php.net/manual/zh/install.unix.nginx.php html
http://blog.sina.com.cn/s/blog_4991d72c0102vu2u.htmlmysql
1、安裝相關包nginx
yum -y install epel-release
yum groupinstall "development tools"
yum -y install mhash mhash-devel libmcrypt libmcrypt-devel
yum -y install zlib zlib-devel libjpeg libjpeg-devel freetype freetype-devel gd gd-devel php-gd curl curl-devel libxml2 libxml2-devel libxslt libxslt-devel libmcrypt libmcrypt-devel libc-client-devel postgresql-devel
ln -s /usr/lib64/libc-client.so /usr/lib/libc-client.sosql
2、PHP下載vim
http://php.net/get/php-5.6.11.tar.gz/from/a/mirror後端
3、解壓下載文件瀏覽器
tar zxf php-x.x.xbash
4、配置並構建 PHP。在此步驟您可使用不少選項自定義 PHP,例如啓用某些擴展等。 運行 ./configure --help 命令來得到完整的可用選項清單。 在本示例中,咱們僅進行包含 PHP-FPM 和 MySQL 支持的簡單配置。curl
cd ../php-x.x.x
./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --with-kerberos --with-pcre-regex --enable-exif --enable-mbregex --enable-sysvshm --enable-inline-optimization --disable-rpath --with-pgsql --enable-calendar --with-zlib-dir --enable-xmlreader --enable-xmlwriter --enable-static --with-xsl --enable-ftp --enable-opcache --disable-fileinfo
make
make clean
sudo make install
安裝過程當中若是進示錯誤:
xml2-config not found. Please check your libxml2 installation
則安裝:
yum install libxml2
yum install libxml2-devel -y
而後從新執行:./configure
5、添加環境變量
export PATH=/usr/local/php/bin:$PATH
vim /etc/profile
最後一行添加
export PATH="/usr/local/php/bin:$PATH"
source /etc/profile
6、建立配置文件,並將其複製到正確的位置。
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
useradd www-data;
找到如下內容並修改:
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data
可經過:
vi /usr/local/php/etc/php-fpm.conf
listen = 127.0.0.1:8080
修改php的cgi服務 端口
啓動服務:/usr/local/php/sbin/php-fpm
或者
[將 php-fpm 配置爲服務]
#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php-fpm
# config: /usr/local/php/etc/php.ini
# Source function library.
. /etc/rc.d/init.d/functions
PHP_PATH=/usr/local
DESC="php-fpm daemon"
NAME=php-fpm
DAEMON=$PHP_PATH/php/sbin/$NAME
CONFIGFILE=$PHP_PATH/php/etc/php-fpm.conf
INI_CONFIGFILE=$PHP_PATH/php/etc/php.ini
PIDFILE=$PHP_PATH/php/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
rh_start() {
$DAEMON -y $CONFIGFILE -c $INI_CONFIGFILE || echo -n " already running"
}
rh_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
rh_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
rh_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
rh_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
rh_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
rh_stop
sleep 1
rh_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
;;
esac
exit 0
chmod +x /etc/init.d/php-fpm
service php-fpm restart
chkconfig --level 35 php-fpm on
7、須要着重提醒的是,若是文件不存在,則阻止 Nginx 將請求發送到後端的 PHP-FPM 模塊, 以免遭受惡意腳本注入的攻擊。
將 php.ini 文件中的配置項 cgi.fix_pathinfo 設置爲 0 。
打開 php.ini:
vim /usr/local/php/php.ini
定位到 cgi.fix_pathinfo= 並將其修改成以下所示:
cgi.fix_pathinfo=0
8、配置 Nginx 使其支持 PHP 應用:
vim /usr/local/nginx/conf/nginx.conf
修改默認的 location 塊,使其支持 .php 文件:
location / {
root html;
index index.php index.html index.htm;
}
下一步配置來保證對於 .php 文件的請求將被傳送到後端的 PHP-FPM 模塊, 取消默認的 PHP 配置塊的註釋,並修改成下面的內容:
location ~* \.php$ {
root /home/project/myphptest;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:8080;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
重啓 Nginx。
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx
9、建立測試文件。
rm /usr/local/nginx/html/index.html
echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php
打開瀏覽器,訪問 http://localhost,將會顯示 phpinfo() 。