隽永东方设计工作室
Eastern Design Studio Menu

Centos5.6搭建PHP5.3.8/Nginx1.0.9/Mysql5.5

1 星2 星3 星4 星5 星
(1 票, 评分: 5.00 总分 5)  

最近升级了服务器架构为最新的PHP5.3.8+Nginx1.0.9+Mysql5.5.17,现记录步骤如下:

1、yum update

2、#依赖库和开发工具

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 e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

#Nginx
yum -y install pcre-devel zlib-devel

#Php
yum -y install gd-devel libjpeg-devel libpng-devel freetype-devel libxml2-devel curl-devel freetype-devel

#Mysql
yum -y install bison gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libtool-ltdl-devel*

3、时区设置和同步时间

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ntpdate us.pool.ntp.org

第一句,将我们的时区文件复制到系统位置。

第二句,在网上更新时间。

第一句是把当前时区调整为上海就是+8区,想改其他时区也可以去看看/usr/share/zoneinfo目录;然后第二句是利用ntpdate同步标准时间.

如果 ntpdate 无法运行,那么需要 yum install -y ntpdate 安装一下服务

4、下载软件包

#创建目录

mkdir /web
cd /web
#PHP5.3.8
wget
#PHP库文件
wget
wget
wget
wget
eaccelerator-0.9.6.1.tar.bz2
wget
#Nginx1.0.9
wget
#Nginx(pcre)
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.gz
#Mysql5.5.17
wget
#Mysql(cmake)
wget

5、安装Mysql

#安装cmake
tar -zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6/
./configure
gmake && gmake install && cd ../
#添加mysql用户
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
#安装Mysql
tar -zxvf mysql-5.5.17.tar.gz
cd mysql-5.5.17
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc/ -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DMYSQL_TCP_PORT=3306

#设置Mysql
#在support-files目录中有五个配置信息文件:
#my-small.cnf (内存<=64M)
#my-medium.cnf (内存 128M)
#my-large.cnf (内存 512M)
#my-huge.cnf (内存 1G-2G)
#my-innodb-heavy-4G.cnf (内存 4GB)
cd /usr/local/mysql
cp ./support-files/my-medium.cnf /etc/my.cnf
vi /etc/my.cnf
#在 [mysqld] 段增加
datadir = /data/mysql
wait-timeout = 30
max_connections = 512
default-storage-engine = MyISAM
#在 [mysqld] 段修改
max_allowed_packet = 16M
#生成授权表
cd /usr/local/mysql
./scripts/mysql_install_db –user=mysql
#更改密码
/usr/local/mysql/bin/mysqladmin -u root password 123456
#开启mysql
/usr/local/mysql/bin/mysqld_safe &
#测试连接mysql
/usr/local/mysql/bin/mysql -u root -p 123456
show databases;
exit;
#设置相关脚本
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig –add mysqld
chkconfig mysqld on
service mysqld restart
netstat -tnlp |grep 3306
cd /usr/local/bin //进入用户的默认搜索路径下建立mysql命令的软连接,可以直接执行mysql命令
ln -s /usr/local/mysql/bin/mysql mysql
ln -s /usr/local/mysql/bin/mysqlduamp mysqldump
ln -s /usr/local/mysql/bin/mysqladmin mysqladmin
mysqladmin -u root -p password
mysql -u root -p
flush privileges;

vim /etc/my.cnf //Mysql 优化,在[mysqld]配置参数下面添加下面几行(大概37行下面)

innodb_file_per_table
log-bin-trust-function-creators=1
skip-name-resolv
innodb_flush_log_at_trx_commit=1
sync-binlog=1
lower_case_table_names=1
max_connections = 1500 (默认100)
#修改配置文件里下面的参数,去掉前面的#
innodb_buffer_pool_size = 2048M (默认16M,可以为系统内存50%~70%)
innodb_additional_mem_pool_size = 256M (默认2M)
innodb_log_file_size = 512M (默认5M,innodb_buffer_pool_size的四分之一)
max_allowed_packet = 500M
#设置开机启动
vi /etc/rc.d/rc.local
#加入
/usr/local/mysql/bin/mysqld_safe &

6、安装PHP

#1
tar -zxvf libiconv-1.14.tar.gz && cd libiconv-1.14/
./configure –prefix=/usr/local
make && make install && cd ../

#2
tar -zxvf libmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8/
./configure && make && make install
/sbin/ldconfig && cd libltdl/ && ./configure –enable-ltdl-install
make && make install && cd ../

#3
tar -zxvf mhash-0.9.9.9.tar.gz && cd mhash-0.9.9.9/ && ./configure
make && make install && cd ../

#4
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

#5
tar -zxvf mcrypt-2.6.8.tar.gz &&cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make && make install && cd ../

#6
tar -xjvf php-5.3.8.tar.bz2
cd php-5.3.8

#7
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18 #(64Bit)
/sbin/ldconfig

./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-iconv-dir=/usr/local/ –with-freetype-dir –with-mysql=/usr/local/mysql –with-mysqli=/usr/local/mysql/bin/mysql_config –with-jpeg-dir –with-png-dir –with-zlib –with-mhash –enable-sockets –enable-ftp –with-libxml-dir –enable-xml –disable-rpath –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-mbstring –with-mcrypt –with-gd –enable-gd-native-ttf –with-openssl –with-mhash –enable-pcntl –enable-sockets –with-ldap –with-ldap-sasl –enable-fpm –with-xmlrpc –enable-zip –enable-soap –with-gettext –without-pear

make ZEND_EXTRA_LIBS=’-liconv’

#注意这里容易出现 make: *** [ext/phar/phar.php] 错误 127

#出现mysql client解决方法

#ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib/
#ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

#或者chmod: 无法访问 “ext/phar/phar.phar”: 没有那个文件或目录
#make: [ext/phar/phar.phar] 错误 1 (忽略)
#解决方法在编译的时候加–without-pear参数
#如果还不行,make的时候不添加 ZEND_EXTRA_LIBS=’-liconv’ 参数

make install

#选择PHP.ini配置文件

cp php.ini-production /usr/local/php/etc/php.ini

7、更改PHP-FPM

#添加WWW用户

/usr/sbin/groupadd www && /usr/sbin/useradd -g www www
mkdir -p /var/log/nginx && chmod +w /var/log/nginx &&chown -R www:www /var/log/nginx
mkdir -p /data/www && chmod +w /data/www && chown -R www:www /data/www

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf

#去掉/更改 配置文件中的;

pm.max_children = 64
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1024
user = www
group = www

#检查语法是否正确
/usr/local/php/sbin/php-fpm -t
#出现NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful 测试成功
/usr/local/php/sbin/php-fpm &
#设置开机启动
vi /etc/rc.d/rc.local
#在行末加入
/usr/local/php/sbin/php-fpm &

#返回安装包目录
cd /web

8、安装PHP扩展

eaccelerator是一个自由开放源码PHP加速器,优化和动态内容缓存,提高了性能php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。使您的PHP程序代码执效率能提高1-10倍;

tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure –enable-eaccelerator=shared –with-php-config=/usr/local/php/bin/php-config
make
make install
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

9、安装Nginx

#安装pcre库
tar -zxvf pcre-8.13.tar.gz && cd pcre-8.13/ && ./configure
make && make install && cd ../

#安装Nginx
tar -zxvf nginx-1.0.9.tar.gz && cd nginx-1.0.9 &&
tar -zxvf nginx-1.0.9.tar.gz && cd nginx-1.0.9 && ./configure –user=www –group=www –prefix=/usr/local/nginx –sbin-path=/usr/local/nginx/sbin/nginx –conf-path=/usr/local/nginx/conf/nginx.conf –with-http_stub_status_module –with-http_ssl_module –with-pcre –lock-path=/var/run/nginx.lock –pid-path=/var/run/nginx.pid –with-ipv6

make && make install && cd ../

vim /etc/init.d/nginxd/ //添加下面内容,让nginx 支持service 启动关闭

#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: – 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ “$NETWORKING” = “no” ] && exit 0
nginx=”/usr/local/nginx/sbin/nginx”
prog=$(basename $nginx)
NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/run/nginx.lock
make_dirs() {
# make required directories
user=nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -
options=$nginx -V 2>&1 | grep 'configure arguments:'
for opt in $options; do
if [ echo $opt | grep '.*-temp-path' ]; then
value=echo $opt | cut -d "=" -f 2
if [ ! -d “$value” ]; then
# echo “creating” $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $”Starting $prog: ”
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $”Stopping $prog: ”
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $”Reloading $prog: ”
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case “$1″ in
start)
rh_status_q && exit 0
$1
;;

stop)
rh_status_q || exit 0
$1
;;

restart|configtest)
$1
;;

reload)
rh_status_q || exit 7
$1
;;

force-reload)
force_reload
;;

status)
rh_status
;;

condrestart|try-restart)
rh_status_q || exit 0
;;
*)

echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”

exit 2

esac

赋予权限:
chmod +x /etc/init.d/nginxd

service nginxd start

chkconfig –add nginxd

chkconfig nginxd on

chkconfig –list nginxd

通过浏览器访问服务器,出现welcome to nginx!表示成功

#更改配置
vi /usr/local/nginx/conf/nginx.conf

#修改一些参数,别直接替换文件,这只是一部分
user www

events {
use epoll;
worker_connections 1024;
}

server {
listen 80;
server_name eastdesign.net www.eastdesign.net;
index index.html index.htm index.php;
root /home/eastdesign;
#include wp_rewrite.conf;

location ~ \.php$ {
root /home/eastdesign;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 1d;
}

access_log /data1/logs/eastdesign.net.access.log;
error_log /data1/logs/eastdesign.net.error.log;
location ~ /\.ht {
deny all;
}
}
#注意这里
#$document_root$fastcgi_script_name;

#检测配置文件
/usr/local/nginx/sbin/nginx -t

#提示表示成功
#nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
#nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

#开启Nginx
/usr/local/nginx/sbin/nginx

#平滑重启Nginx
/usr/local/nginx/sbin/nginx -s reload

#添加开机启动
vi /etc/rc.d/rc.local
#最后移行加入
/usr/local/nginx/sbin/nginx

10、过程总结:
一共花了将近三天的时间搞定所有的编译安装和配置,个人感觉网上所有的教程或多或少都有一些问题,教程制作者发布教程的初衷也大多是为了方便自己下次使用,或者经验总结,所以教程不可能囊括安装过程中发生的所有问题,因此导致如果按照教程按部就班一步一步操作下来,一般都会出现一些问题,所以不要单纯指望根据某个教程做好所有的设置,可以综合借鉴几个不错的教程,再结合自己摸索体验,当然DIY的体验过程是很有成就感的。



       


  售前在线询价单
  全网营销套餐
  隽永东方学院