[Web服务器搭建][LNMP]Centos6.3x64+nginx1.6+php5.5(fastcgi)+mysql5.5搭建

来源:互联网 发布:视频音乐制作软件 编辑:程序博客网 时间:2024/05/16 18:06

一,安装前准备

1,,PCRE

在这之前要安装PCRE,它是perl所用的正则表达式,目的让所安软件支持正则表达式,默认情况下nginx只处理静态页面,动态页面脚本需要根据正则查找路径交给php处理。

首先查看系统自带的PCRE

rpm -qa | grep pcre

一般系统都会自带,要删除系统自带的,在这之前要备份一下libpcre.so.0这个文件,因为RPM包关联性强,在删除后没有libpcre.so.0这个文件是安不上pcre的

cp /lib64/libpcre.so.0 /

然后删除pcre

rpm -e --nodeps pcrexxxx

tar zxvf pcrexxx.tar.gz

cd pcrexxx

cp /libpcre.so.0 /lib64/libpcre.so.0

./configure

make && make install

2,安装必须的lib工具库

yum -y install openssl* gd gd2 gd-devel gd2-devel 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 make autoconf automake

二,安装nginx,mysql,php

1,安装nginx

要求:GCC编译器和相关工具,还需要有autoconf和automake工具,用于自动创建功能完善的makefile,当前大多数软件包都是用着一工具生成的makefile的,nginx也是,模块依赖性:nginx一些模块需要第三方库,如gzip模块需要zlib库,rewrite模块需要pcre库,ssl功能需要openssl库。

mkdir -p /usr/local/nginx

/usr/sbin/groupadd nginx

/usr/sbin/useradd -g nginx nginx

ulimit -SHn 65535

tar zxvf nginx-1.6.0.tar.gz

cd nginx-1.6.0

./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http-stub_status_module

make && make install

检查是否安装成功

/usr/local/nginx/sbin/nginx -t

./nginx :error while loading shared libraries:libpcre.so.1:cannot open shared object file:No such file or directory缺少lib文件

ldd $(which /usr/local/nginx/sbin/nginx)


ln -s /usr/local/lib/libpcre.so.1 /lib64

结果显示:
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 

ps -ef | grep nginx



启动,停止

启动:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  "-c"指定配置文件路径,默认加载安装目录下conf下的配置文件

停止:一般通过发送系统信号给Nginx主进程方式停止nginx

ps -ef | grep nginx 

如果在nginx.conf配置文件中指定了pid文件存放的路径(例如:/usr/local/nginx/logs/nginx.pid),如果没有指定pid文件存放的路径,nginx.pid文件默认存放在nginx安装目录的logs目录下

从容停止:kill - QUIT nginx主进程号 或者 kill - QUIT /usr/local/nginx/logs/nginx.pid

快速停止:KILL -TERM nginx主进程号 或者 kill -TERM /usr/local/nginx/logs/nginx.pid

   kill -INT nginx主进程号 或者 kill -INT /usr/local/nginx/logs/nginx.pid

强制停止所有nginx进程

pkill -9 nginx

2,安装mysql

mkdir -p /usr/local/mysql

mkdir -p /var/local/mysql/data

groupadd mysql

useradd -g mysql mysql

chown mysql:mysql -R /var/local/mysql/data

tar zxvf cmake-2.8.4.tar.gz

cd cmake-2.8.4

./configure

make && make install

tar zxvf mysql-5.5.37.tar.gz

cd mysql-5.5.37

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.5 -DMYSQL_DATADIR=/var/local/mysql/data -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DSYSCONFDIR=/etc -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_USER=mysql -DWITH_DEBUG=0 -DENABLED_LOCAL_INFILE=1

编译选项说明:

//mysql安装的主目录,5.5.8版本默认为/usr/local/mysql,所以可以不添加
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
//mysql数据保存的路径,自定义
-DMYSQL_DATADIR=/home/system_username/mysql/mysqldata
//mysql配置文件地址------The default my.cnf option file directory
-DSYSCONFDIR=/etc
 //Compile storage engine xxx statically into server
/*Storage engines are built as plugins. You can build a plugin as a static module (compiled into the server)
*or a dynamic module  (built as a dynamic library that must be installed into the server using the INSTALL
*PLUGIN statement or the --plugin-load option before it can be used). Some plugins might not support static
*or dynamic building.


 */
 -DWITH_INNOBASE_STORAGE_ENGINE=1
 -DWITH_ARCHIVE_STORAGE_ENGINE=1
 -DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
 -DWITH_PARTITION_STORAGE_ENGINE=1
  //Unix socket file
 /**The Unix socket file path on which the server listens for socket connections. This must be an absolute path  *name. The default is /tmp/mysql.sock
*/
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock
 //数据库服务器TCP/IP连接的监听端口,默认为3306
-DMYSQL_TCP_PORT=3306
 //Whether to enable LOCAL capability in the client library for LOAD DATA INFILE
 //默认为关闭,这里开启
-DENABLED_LOCAL_INFILE=1
 /数据库编码设置
-DWITH_EXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci

make && make install

cp support-files/my-huge.cnf /etc/my.cnf

内存1-2g选huge比较好

vi /etc/my.cnf

可以根据需要在[mysql]下增加

//同时设置才生效

interactive_timeout = 100

wait_timeout = 100

max_connections = 1500

max_connect_errors =1500

default-storage-engine=MyISAM

创建启动脚本

cp support-files/mysql.server /etc/init.d/mysqld

chmod 755 /etc/init.d/mysqld

初始化mysql并生成授权库

bash scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/var/local/mysql

启动mysql

/etc/init.d/mysqld start

登陆

/usr/local/mysql/bin/mysql -uroot -p

修改密码

use mysql

update user set password=PASSWORD("xxxxx") where user = 'root'

FLUSH PRIVILEGES

将mysql加入启动项,设置成服务启动

chkconfig -add mysqld

chkconfig --level 345 mysqld on

service mysqld restart

注意 mysql5.5以后的tart包是cmake安装的,而不是之前的autotools即configure方式

3,安装php(fast-cgi)

安装基本依赖库

tar zxvf jpegsrc.v9.tar.gz
cd jpeg-9/
./configure
make && make install
cd ../

tar zxvf libpng-1.6.2.tar.gz
cd libpng-1.6.2/
./configure 
make && make install
cd ../

tar zxvf freetype-2.4.12.tar.gz
cd freetype-2.4.12/
./configure
make && make install
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure 
make
make install
cd libltdl/
./configure  --enable-ltdl-install
make
make install

cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure --prefix=/server/libs
make
make install
cd ../

对共享库做符号链接

ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

#编辑lib库查询路径变量
vi /etc/ld.so.conf
#在文件末尾添加后保存
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/mysql/lib
#使更改生效
ldconfig

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
export LDFLAGS="-L/usr/local/lib -L/usr/lib"
export CFLAGS="-I/usr/local/include -I/usr/include"
touch malloc.h
./configure --prefix=/usr/local --with-libmcrypt-prefix=/usr/local/lib
make
make install
cd ../

安装php

mkdir -p /usr/local/php5.5

tar zxvf php-5.5.10

cd php-5.5.10

export LIBS="-lm -ltermcap -lresolv"
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
export LD_LIBRARY_PATH="/usr/local/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib

./configure --prefix=/usr/local/php5.5 --with-config-file-path=/etc --with-iconv-dir=/usr/local/ --with-freetype-dir --with-mysql=/usr/local/mysql5.5 --with-mysqli=/usr/local/mysql5.5/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-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 --without-pear--disable-fileinfo(内存小宇1G编译不成功,加上这个成功)

make ZEND_EXTRA_LIBS='-liconv'

make install

cp php.ini-development /etc/php.ini
cd ../
cp /usr/local/php5.5/etc/php-fpm.conf.default /usr/local/php5.5/etc/php-fpm.conf
修改
user = nobody
group = nobody

user = www
group = www
将;pid = run/php-fpm.pid前的;去掉并修改为
pid = /usr/local/php5.5/var/run/php-fpm.pid
启动php-fpm
/usr/local/php5.5/sbin/php-fpm

三,php,php-fpm,nginx支持php配置

#复制拷贝php主配置文件
cp php.ini-production /etc/php.ini
#复制php-fpm启动脚本(可以采用诸如service php-fpm start|restart|stop命令的脚本)
#该启动脚本编译的时候已经将必须的配置路径设置好了, 复制即可使用
cp ./sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

#设置启动权限并加入开机启动
chmod +x /etc/rc.d/init.d/php-fpm #添加执行权限 
chkconfig php-fpm on #设置php通过nginx的php-fpm方式的开机启动

#ps补充mysql的
chmod +x /etc/rc.d/init.d/mysqld #mysql添加mysql的执行权限 
chkconfig mysqld on #设置mysql开机启动

#php-fpm配置文件
cd /usr/local/php5.5/etc
cp php-fpm.conf.default php-fpm.conf

vi php-fpm.conf
#修改如下(php-fpm与nginx的执行用户必须相同)
user = nginx
group = nginx

#nginx支持php-fpm
cd /usr/local/nginx/conf
vi nginx.conf
#修改如下所述部分
user nginx nginx;
pid /server/data/nginx.pid
#index 的位置添加 index.php
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; 
}

四,nginx启动文件、启动nginx、mysql、php-fpm

将nginx设置成 service nginx start|restart|stop等形式

#nginx启动脚本
vi /etc/init.d/nginx
#写入如下内容[文件另提一个代码段],保存之后授予该执行文件权限

chmod 755 /etc/init.d/nginx 
#或者chmod +x /etc/rc.d/init.d/nginx 是一个意思

脚本:

#Server nginx - this script starts and stops the nginx daemon
# By www.jjonline.cn
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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/lock/subsys/nginx

make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
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
;;
test)
configtest || exit 0
;;
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

启动php,php-fpm,nginx

service mysqld start
service nginx start
service php-fpm start

五,问题

启动php-fpm时提示“Starting php-fpm /server/apps/php/sbin/php-fpm: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory  failed”


做个文件连接即可:


ln -s /usr/local/mysql/lib/mysql /usr/lib64/mysql 
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18 #注意64位和32位的路径区别

0 0
原创粉丝点击