centos php 开启libgdgd_CentOS搭建PHP环境
說明:
操作系統:CentOS?6.x
準備篇:
一、配置防火墻,開啟80端口、3306端口
vi?/etc/sysconfig/iptables
-A?INPUT?-m?state?--state?NEW?-m?tcp?-p?tcp?--dport?80?-j?ACCEPT(允許80端口通過防火墻)
-A?INPUT?-m?state?--state?NEW?-m?tcp?-p?tcp?--dport?3306?-j?ACCEPT(允許3306端口通過防火墻)
:wq!?#保存
/etc/init.d/iptables?restart?#重啟防火墻使配置生效
四?、系統約定
軟件源代碼包存放位置:/usr/local/src
源碼包編譯安裝位置:/usr/local/軟件名字
五、下載軟件包
pcre?(支持nginx偽靜態)
安裝篇
安裝編譯工具及庫文件(使用?CentOS?yum?命令安裝)
yum?install?make?m4?autoconf?automake?curl?curl-devel?gcc?gcc-c++?zlib-devel?bison?bison-devel?openssl?openssl-devel?pcre-devel?mpfr?cpp?glibc?libgomp?libstdc++-devel?ppl?cloog-ppl?libevent?libcom_err-devel?libsepol-devel?libselinux-devel?krb5-devel?ncurses*?bzip2-devel?libxml2?libxml2-devel?libpng?libpng-devel?freetype?libfreetype-devel(改成freetype-devel)?libjpeg?libjpeg-devel?gd?gd-devel?perl
安裝依賴庫
1、安裝?pcre
cd?/usr/local/src
mkdir?/usr/local/pcre
tar?zxvf?pcre-8.36.tar.gz
cd?pcre-8.36
./configure
make
make?install
2、安裝?zlib
cd?/usr/local/src
tar?xvf?zlib-1.2.8.tar.gz
cd?zlib-1.2.8
./configure
make
make?install
3、安裝?openssl
cd?/usr/local/src
mkdir?/usr/local/openssl
tar?zxvf?openssl-1.0.2c.tar.gz
cd?openssl-1.0.2c
./config
make
make?install
二、安裝cmake?(MySQL5.5以上版本采用CMAKE編譯)
cd?/usr/local/src
tar?xvf?cmake-2.8.12.2.tar.gz
cd?cmake-2.8.12.2
./bootstrap
gmake
gmake?install
四、安裝?MySQL
準備工作
groupadd?mysql?#添加?mysql?用戶組
useradd?-g?mysql?mysql?-s?/bin/false?#創建用戶?mysql?并加入到?mysql?組,不允許?mysql?用戶直接登錄系統
mkdir?-p?/data/mysql/data?#創建?mysql?數據庫存放目錄
chown?-R?mysql:mysql?/data/mysql/data?#設置?mysql?數據庫目錄權限
#編譯安裝
cd?/usr/local/src
tar?zxvf?mysql-5.6.42.tar.gz
cd?mysql-5.6.42
cmake?\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql?\
-DMYSQL_DATADIR=/data/mysql/data?\
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock?\
-DDEFAULT_CHARSET=utf8?\
-DDEFAULT_COLLATION=utf8_general_ci?\
-DEXTRA_CHARSETS=all?\
-DWITH_MYISAM_STORAGE_ENGINE=1?\
-DWITH_INNOBASE_STORAGE_ENGINE=1?\
-DENABLED_LOCAL_INFILE=1?\
-DMYSQL_TCP_PORT=3306
make
make?install
注:cmake?時如出現錯誤“Could?NOT?find?Curses?(missing:?CURSES_LIBRARY?CURSES_INCLUDE_PATH)”
執行以下操作再重新編譯即可:
rm?-rf?CMakeCache.txt?CMakeFiles/
yum?install?ncurses-devel
#配置
cd?/usr/local/mysql
mv?/etc/my.cnf?/etc/my.cnf.bak
cp?-f?./support-files/my-default.cnf?./my.cnf
cp?./support-files/mysql.server?/etc/init.d/mysqld?#加入系統啟動
chmod?a+x?/etc/init.d/mysqld?#增加執行權限
chkconfig?--add?mysqld
chkconfig?mysqld?on?#加入開機啟動
ln?-sf?/usr/local/mysql/bin/*?/usr/local/bin/
#將?mysql/bin?目錄加入環境變量
vi?/etc/profile?#在最后一行加入:
############################################################?PATH=$PATH之間不能有空格
PATH=$PATH:/usr/local/mysql/bin
export?PATH
############################################################
:wq!?保存
source?/etc/profile?#使其生效
echo?$PATH?#查看是否添加成功
#初始化
./scripts/mysql_install_db?--basedir=/usr/local/mysql?--datadir=/data/mysql/data?--user=mysql
#啟動
service?mysqld?start
#設置?root?用戶密碼
方法1:
mysql?-u?root
mysql>?SET?PASSWORD?=?PASSWORD('123456');
方法2:
mysqladmin?-u?root?-p?password?123456
接下來會提示輸入舊密碼,直接回車即可。
注:方法2命令中的密碼字符串外面不要加引號
#若要設置?root?用戶可以遠程訪問,執行:
mysql>?GRANT?ALL?PRIVILEGES?ON?*.*?TO?'root'@'%'?IDENTIFIED?BY?'password'?WITH?GRANT?OPTION;
mysql>?FLUSH?PRIVILEGES;
五、安裝?Nginx
groupadd?www?#添加www用戶組
useradd?-g?www?www?-s?/bin/false?#創建?nginx?運行用戶?www?并加入到?www?組,不允許?www?用戶直接登錄系統
cd?/usr/local/src
tar?zxvf?nginx-1.9.2.tar.gz
cd?nginx-1.9.2
./configure?\
--prefix=/usr/local/nginx?\
--user=www?\
--group=www?\
--with-pcre=/usr/local/src/pcre-8.36?\
--with-zlib=/usr/local/src/zlib-1.2.8?\
--with-openssl=/usr/local/src/openssl-1.0.2c?\
--with-http_ssl_module
make
make?install
注:?--with-pcre?--with-zlib?--with-openssl?指向的是源碼包解壓后的路徑,而不是安裝的路徑。
配置nginx開機啟動
vi?/etc/init.d/nginx?#編輯啟動文件并添加下面內容
############################################################
#!/bin/bash
#?nginx?Startup?script?for?the?Nginx?HTTP?Server
#?it?is?v.0.0.2?version.
#?chkconfig:?-?85?15
#?description:?Nginx?is?a?high-performance?web?and?proxy?server.
#?It?has?a?lot?of?features,?but?it's?not?for?everyone.
#?processname:?nginx
#?pidfile:?????/var/run/nginx.pid
#?config:??????/usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
#?Source?function?library.
.?/etc/rc.d/init.d/functions
#?Source?networking?configuration.
.?/etc/sysconfig/network
#?Check?that?networking?is?up.
[?${NETWORKING}?=?"no"?]?&&?exit?0
[?-x?$nginxd?]?||?exit?0
#?Start?nginx?daemons?functions.
start()?{
if?[?-e?$nginx_pid?];then
echo?"nginx?already?running...."
exit?1
fi
echo?-n?$"Starting?$prog:?"
daemon?$nginxd?-c?${nginx_config}
RETVAL=$?
echo
[?$RETVAL?=?0?]?&&?touch?/var/lock/subsys/nginx
return?$RETVAL
}
#?Stop?nginx?daemons?functions.
stop()?{
echo?-n?$"Stopping?$prog:?"
killproc?$nginxd
RETVAL=$?
echo
[?$RETVAL?=?0?]?&&?rm?-f?/var/lock/subsys/nginx?/usr/local/nginx/logs/nginx.pid
}
reload()?{
echo?-n?$"Reloading?$prog:?"
#kill?-HUP?`cat?${nginx_pid}`
killproc?$nginxd?-HUP
RETVAL=$?
echo
}
#?See?how?we?were?called.
case?"$1"?in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status?$prog
RETVAL=$?
;;
*)
echo?$"Usage:?$prog?{start|stop|restart|reload|status|help}"
exit?1
esac
exit?$RETVAL
############################################################
:wq!?保存退出。
chmod?+x?/etc/init.d/nginx?#賦予執行權限
/sbin/chkconfig?nginx?on?#設置開機啟動
#啟動
service?nginx?start
六、安裝?PHP
安裝?libmcrypt
cd?/usr/local/src
tar?zxvf?libmcrypt-2.5.7.tar.gz
cd?libmcrypt-2.5.7
./configure
make
make?install
注:編譯時如提示?'Pyobject'?undeclared...?錯誤,需先安裝?python-devel?軟件包(yum?-y?install?python-devel),
安裝?php
cd?/usr/local/src
tar?zxvf?php-5.4.42.tar.gz
cd?php-5.4.42
./configure?\
--prefix=/usr/local/php?\
--with-config-file-path=/usr/local/php/etc?\
--with-mysql=mysqlnd?\
--with-mysqli=mysqlnd?\
--with-pdo-mysql=mysqlnd?\
--with-zlib?\
--with-gd?\
--with-png-dir?\
--with-jpeg-dir?\
--with-freetype-dir?\
--enable-gd-native-ttf?\
--with-iconv?\
--enable-xml?\
--enable-bcmath?\?#支持大整數計算的擴展
--enable-shmop?\
--enable-sysvsem?\
--enable-inline-optimization?\
--with-curlwrappers?\
--enable-mbregex??\
--enable-fpm?\
--enable-mbstring?\
--with-openssl?\
--enable-pcntl?\
--enable-sockets?\
--with-xmlrpc?\
--enable-zip?\
--enable-soap?\
--without-pear?\
--with-gettext=shared?\
--enable-session?\
--with-mcrypt?\
--with-curl
make?#編譯
make?install?#安裝
cp?php.ini-production?/usr/local/php/etc/php.ini?#復制php配置文件到安裝目錄
rm?-rf?/etc/php.ini?#刪除系統自帶配置文件
ln?-s?/usr/local/php/etc/php.ini?/etc/php.ini?#添加軟鏈接
cp?/usr/local/php/etc/php-fpm.conf.default?/usr/local/php/etc/php-fpm.conf?#拷貝模板文件為php-fpm配置文件
vi?/usr/local/php/etc/php-fpm.conf
pid?=?run/php-fpm.pid?#取消前面的分號
user?=?www?#設置php-fpm運行賬號為www
group?=?www?#設置php-fpm運行組為www
設置?php-fpm?開機啟動
cp?/usr/local/src/php-5.4.42/sapi/fpm/init.d.php-fpm?/etc/init.d/php-fpm?#拷貝php-fpm到啟動目錄
chmod?+x?/etc/init.d/php-fpm?#添加執行權限
chkconfig?php-fpm?on?#設置開機啟動
PHP?配置
vi?/usr/local/php/etc/php.ini?#編輯配置文件
找到:;date.timezone?=
修改為:date.timezone?=?PRC?#設置時區
找到:expose_php?=?On
下面紅色部分未做修改。
修改為:expose_php?=?OFF?#禁止顯示php版本的信息
找到:display_errors?=?On
修改為:display_errors?=?OFF?#關閉錯誤提示
七、配置?nginx?支持?php
vi?/usr/local/nginx/conf/nginx.conf
修改/usr/local/nginx/conf/nginx.conf?配置文件,做如下修改
user?www?www;?#首行user去掉注釋,修改?Nginx?運行組為?www?www;盡量與?/usr/local/php5/etc/php-fpm.conf?中的?user,?group?配置相同,否則php可能運行出錯
index?index.php?index.html?index.htm;?#添加index.php
#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;
include?????????fastcgi_params;
fastcgi_param???SCRIPT_FILENAME????$document_root$fastcgi_script_name;
fastcgi_param???SCRIPT_NAME??????????$fastcgi_script_name;
}
/etc/init.d/nginx?restart?#重啟nginx
安裝?php?redis?擴展
cd?/usr/local/src
tar?zxvf?redis-2.2.7.tgz
cd?redis-2.2.7
/usr/local/php/bin/phpize
./configure??--with-php-config=/usr/local/php/bin/php-config
make??&&??make??install
修改?php.ini?文件:
vi?/usr/local/php/etc/php.ini
增加:
extension=redis.so
/etc/init.d/php-fpm?restart??#重啟?php-fpm?生效
安裝?php?memcached?擴展
安裝依賴庫?libmemcached
cd?/usr/local/src
tar?zxvf?libmemcached-1.0.18.tar.gz
cd?libmemcached-1.0.18
./configure?--with-memcached
make
make?install
安裝?memcached?擴展
cd?/usr/local/src
cd?memcache-2.2.0
/usr/local/php/bin/phpize
./configure?--with-php-config=/usr/local/php/bin/php-config?--enable-memcached?--disable-memcached-sasl
make
make?install
修改?php.ini?文件:
vi?/usr/local/php/etc/php.ini
增加:
extension=memcached.so
/etc/init.d/php-fpm?restart??#重啟?php-fpm?生效
安裝?php?memcache?擴展
cd?/usr/local/src
wget?http://pecl.php.net/get/memcache-3.0.8.tgz
tar?zxvf?memcache-3.0.8.tgz
cd?memcache-3.0.8
/usr/local/php/bin/phpize
./configure?--with-php-config=/usr/local/php/bin/php-config
make
make?install
修改?php.ini?文件:
vi?/usr/local/php/etc/php.ini
增加:
extension=memcache.so
/etc/init.d/php-fpm?restart??#重啟?php-fpm?生效
安裝?php?Zend?Opcache?擴展(即?Optimizer+,推薦加速器)
cd?/usr/local/src
wget?http://pecl.php.net/get/zendopcache-7.0.5.tgz
tar?zxvf?zendopcache-7.0.5.tgz
cd?zendopcache-7.0.5
/usr/local/php/bin/phpize
./configure?--with-php-config=/usr/local/php/bin/php-config?--enable-shared
make
make?install
修改?php.ini?文件:
vi?/usr/local/php/etc/php.ini
增加:
zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
/etc/init.d/php-fpm?restart??#重啟?php-fpm?生效
注:Zend?Opcache?與?eAccelerator?相沖突,安裝使用?Zend?Opcache?可能需要先卸載或禁用?eaccelerator?擴展。
六、安裝?Redis
安裝需要的支持環境
cd?/usr/local/src
wget?http://downloads.sourceforge.net/tcl/tcl8.6.4-src.tar.gz
tar?zxvf?tcl8.6.4-src.tar.gz
cd??tcl8.6.3/unix/
./configure
make
make?install
安裝?Redis
cd?/usr/local/src
tar?zxvf?redis-3.0.2.tar.gz
cd?redis-3.0.2
make
make?PREFIX=/usr/local/redis?install
ln?-sf?/usr/local/redis/bin/*?/usr/local/bin/?#增加軟鏈接
cp?./redis.conf?/usr/local/redis/
vi?/usr/local/redis/redis.conf
daemonize?yes??#redis將以守護進程的方式運行,默認為no會暫用你的終端
timeout?300???#當客戶端閑置多長時間后關閉連接,如果指定為0,表示關閉該功能
設置自動啟動
vi?/etc/init.d/redis
############################################################
#!/bin/sh
#
#?redis????????Startup?script?for?Redis?Server
#
#?chkconfig:?-?80?12
#?description:?Redis?is?an?open?source,?advanced?key-value?store.
#
#?processname:?redis-server
#?config:?/etc/redis.conf
#?pidfile:?/var/run/redis.pid
source?/etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/redis.conf"
PIDFILE="/var/run/redis.pid"
###?Read?configuration
[?-r?"$SYSCONFIG"?]?&&?source?"$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis?Server"
start()?{
if?[?-e?$PIDFILE?];then
echo?"$desc?already?running...."
exit?1
fi
echo?-n?$"Starting?$desc:?"
daemon?$BIN/$prog?$CONFIG
RETVAL=$?
echo
[?$RETVAL?-eq?0?]?&&?touch?/var/lock/subsys/$prog
return?$RETVAL
}
stop()?{
echo?-n?$"Stop?$desc:?"
killproc?$prog
RETVAL=$?
echo
[?$RETVAL?-eq?0?]?&&?rm?-f?/var/lock/subsys/$prog?$PIDFILE
return?$RETVAL
}
restart()?{
stop
start
}
case?"$1"?in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[?-e?/var/lock/subsys/$prog?]?&&?restart
RETVAL=$?
;;
status)
status?$prog
RETVAL=$?
;;
*)
echo?$"Usage:?$0?{start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit?$RETVAL
############################################################
:wq!?保存退出。
chmod?+x?/etc/init.d/redis?#賦予執行權限
啟動或關閉服務
service?redis?start
service?redis?stop
測試
cd?/usr/local/redis/bin
./redis-cli
127.0.0.1:6379>?set?foo?bar
OK
127.0.0.1:6379>?get?foo
"bar"
安裝到這里。
安裝?git
cd?/usr/local/src
wget?https://codeload.github.com/git/git/tar.gz/v2.4.4
tar?zxvf?v2.4.4
cd?git-2.4.4
make?prefix=/usr/local/git?all?//?需要yum安裝?openssl-devel
make?prefix=/usr/local/git?install
注:若出現錯誤?“錯誤:‘XML_Parser’未聲明”,則需安裝?expat-devel?開發包,然后重新執行?make?命令。
yum?-y?install?expat-devel.x86_64
若出現錯誤?“asciidoc:?command?not?found”,則需安裝?asciidoc,然后重新執行?make?命令。
yum?-y?install?asciidoc
git?--version??#查看版本
安裝?Memcache
安裝?libevent
cd?/usr/local/src
tar?zxvf?libevent-2.0.22-stable.tar.gz
cd?libevent-2.0.22-stable
./configure?--prefix=/usr/local/libevent
make
make?install
安裝?memcache
cd?/usr/local/src
tar?zxvf?memcached-1.4.24.tar.gz
cd?memcached-1.4.24
./configure?--prefix=/usr/local/memcached?--with-libevent=/usr/local/libevent
make
make?install
ln?-s?/usr/local/memcached/bin/memcached?/usr/local/bin/memcached
啟動
memcached?-d?-m?512?-u?root?-p?11211?-c?1024?-P?/tmp/memcached.pid
啟動參數說明:
-d?選項是啟動一個守護進程。
-u?root?表示啟動?memcached?的用戶為?root
-m?是分配給?Memcache?使用的內存數量,單位是MB,默認64MB
-M?return?error?on?memory?exhausted?(rather?than?removing?items)
-u?是運行?Memcache?的用戶,如果當前為?root?的話,需要使用此參數指定用戶
-p?是設置?Memcache?的?TCP?監聽的端口,默認為11211
-c?選項是最大運行的并發連接數,默認是1024
-P?是設置保存?Memcache?的?pid?文件
停止
先查看進程?id
ps?-ef|grep?memcached
root????15144????1??0?08:43??????????00:00:00?/usr/local/memcached/bin/memcached?-d?-m?512?-u?root?-p?11211?-c?1024?-P?/tmp/memcached.pid
15144?為pid
則停止命令為:kill?-9?15144
測試篇
cd?/usr/local/nginx/html
rm?-rf?/usr/local/nginx/html/*??#刪除默認測試頁
echo?"<?php ?phpinfo();??>"?>>?/usr/local/nginx/html/index.php
chown?-R?www.www?/usr/local/nginx/html/??#設置目錄所有者
chmod?-R?700?/usr/local/nginx/html/??#設置目錄權限
在客戶端瀏覽器輸入服務器?IP?地址,可以看到相關的配置信息
service?nginx?restart??#重啟nginx
service?mysqld?restart??#重啟mysql
service?php-fpm?start??#啟動php-fpm
service?php-fpm?restart?#重啟php-fpm
service?php-fpm?stop?#停止php-fpm
service?php-fpm?start?#啟動php-fpm
##############################################
備注:
nginx?默認站點目錄是:/usr/local/nginx/html/
權限設置:chown?www.www?/usr/local/nginx/html/?-R
MySQL?數據庫目錄是:/data/mysql/data
權限設置:
chwon?-R?www:www?/data/www
chown?-R?mysql.mysql?/data/mysql/data
到此,運行環境搭建完成。
總結
以上是生活随笔為你收集整理的centos php 开启libgdgd_CentOS搭建PHP环境的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EUV也救不了命 内存快到头了:明年迎来
- 下一篇: 车船险怎么赔付 车船险如何赔