平时安装LNMP是把它们安装到同一台机器上,我想这个对大家来说丝毫没有挑战,下面我们实现把他们剥离到不同的机器上,让各个服务器直接分担原来的压力,也可以增加节点实现负载均衡,如:多增加一台php,让两台机器轮询的编译php,也可以在增加一台nginx,实现dns的轮询负载均衡。
规划: nginx :172.16.1.1
php(FASTCGI):172.16.1.2
mysql:172.16.1.3
环境:redhat 5.8 32位,yum可以正常使用,开发包组"Development Tools" "Development Libraries" "X Software Development"已经安装好,如果没有请先安装。SElinux确保已经关闭,iptables先关闭之。
操作步骤:
一.在172.16.1.1编译安装nginx
1.先安装pcre-devel,nginx的rewrite功能依赖pcre提供的库。
# yum -y install pcre-devel
2.为nginx建立用户,实现安全运行,指定uid的原因是为了与php通过nfs共享时权限方便
# groupadd -r -g 5000 nginx
# useradd -r -g nginx -u 5000 nginx
3.下载并编译安装nginx
# wget http://www.nginx.org/download/nginx-1.2.4.tar.gz
# tar xvf nginx -1.2.4.tar.gz
# cd nginx -1.2.4
#./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/ nginx \
--conf-path=/etc/ nginx / nginx .conf \
--error-log-path=/var/log/ nginx /error.log \
--http-log-path=/var/log/ nginx /access.log \
--pid-path=/var/run/ nginx / nginx .pid \
--lock-path=/var/lock/ ngin x .lock \
--user= nginx \
--group= nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/ nginx /client/ \
--http-proxy-temp-path=/var/tmp/ nginx /proxy/ \
--http-fastcgi-temp-path=/var/tmp/ nginx /fcgi/ \
--http-uwsgi-temp-path=/var/tmp/ nginx /uwsgi \
--http-scgi-temp-path=/var/tmp/ nginx /scgi \
--with-pcre
##各个选项意思就不讲解了,如果需要理解,请找google吧
# make && make install
4.为nginx提供SysV init脚本
新建文件/etc/rc.d/init.d/ nginx ,内容如下:
#!/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/sbin/ nginx "
prog=$(basename $ nginx )
NGINX_CONF_FILE="/etc/ nginx / ng inx .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' -`
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/rc.d/init.d/ nginx
添加至服务管理列表,并让其开机自动启动:
# chkconfig --add nginx
# chkconfig nginx on
启动服务并测试
# service nginx start
直接访问172.16.1.1查看是否有nginx的欢迎信息,如果有代表nginx安装一切正常。
二.在172.16.1.3上部署mysql-5.5.28
1.为mysql准备数据目录与用户
# mkdir -pv /data/mysql
##其实这里最好为mysql准备一个逻辑卷,为了方便书写我省略了
#useradd -r mysql
2.下载安装mysql,这里采用已经编译好的
# wget http://cdn.mysql.com/Downloads/M ... inux2.6-i686.tar.gz
# tar xvf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local/
# cd /usr/local/
# ln -sv mysql-5.5.24-linux2.6-i686 mysql
# cd mysql
##更改目录权限
# chown -R root:mysql .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
3.为mysql提供配置文件
# cd /usr/local/mysql
# cp support-files/my-huge.cnf /etc/my.cnf
并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2
适当位置添加一行,指定mysql数据文件的存放位置:
datadir = /mydata/data
4.为mysql提供sysv init服务脚本:
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
添加至服务列表:
# chkconfig --add mysqld
# chkconfig mysqld on
测试启动 service mysqld start 一切正常mysql安装正常
5.输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man
6.输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:
# ln -sv /usr/local/mysql/include /usr/include/mysql
7.输出mysql的库文件给系统库查找路径:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后让系统重新载入系统库:
# ldconfig -v
8.修改PATH环境变量,让系统可以直接使用mysql的相关命令。
# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
# source /etc/profile.d/mysql.sh
三.172.16.1.2上编译安装php-5.4.8
1.把mysql在这台机器上安装一遍,php要利用一下,否则连接不上mysql,在安装discuz时提示mysql_connect()不支持,如果你有更好办法请给我留言
2.安装扩展包
如果想让编译的php支持mcrypt、mhash扩展,地址: http://www.kuaipan.cn/file/id_33139203151757930.html
libmcrypt-2.5.8-4.el5.centos.i386.rpm
libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm
mhash-0.9.9-1.el5.centos.i386.rpm
mhash-devel-0.9.9-1.el5.centos.i386.rpm
mcrypt-2.6.8-1.el5.i386.rpm
3.下载编译安装php-5.4.8
##php网站今天速度不行有空补上php的下载连接
# tar xf php-5.4.8.tar.bz2
# cd php-5.4.8
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
# make
# make intall
4.为php-fpm提供Sysv init脚本,并提供配置文件,并将其添加至服务列表:
# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on
为php提供配置文件:
# cp php.ini-production /etc/php.ini
为php-fpm提供配置文件:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
# vim /usr/local/php/etc/php-fpm.conf
配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pid = /usr/local/php/var/run/php-fpm.pid ##注释此项并修改
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
接下来就可以启动php-fpm了:
# service php-fpm start
使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
# ps aux | grep php-fpm
到此软件基本安装完毕,下面我们来整合nginx与php,通过php调用mysql
四.整合nginx 与 php
对172.16.1.1的nginx修改
1.计划把网页放在/www下,建立该目录,并修改权限
# mkdir /www
# chown nginx : nginx /www
2.编译 /etc/ nginx / nginx .conf,注释一些选项 ##编辑前请备份原文件
location / {
root /www;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /www;
fastcgi_pass 172.16.1.2:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
3.编辑/etc/ nginx /fastcgi_params,将其内容更改为如下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx ;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
##先不要重启nginx。
在172.16.1.2上修改Php
4.为了以后与nginx共享文件方便建立nginx用户
# groupadd -r -g 5000 nginx
# useradd -r -g nginx -u 5000 nginx
5.修改php-fpm配置文件 /usr/local/php/etc/php-fpm.conf
# vim /usr/local/php/etc/php-fpm.conf
listen = 172.16.1.2:9000 #监听物理网卡地址,供其它机器调用
user = nginx ##php-fpm以nginx用户运行
group = nginx
重启php-fpm
#service php-fpm restart
6.下载解压discuz,更改权限
#wget http://cn.wordpress.org/wordpress-3.4.2-zh_CN.tar.gz
#tar xvf wordpress-3.4.2-zh_CN.tar.gz
#mv wordpress-3.4.2-zh_CN /www
#chown -R nginx : nginx /www
在/www建立test.php 作为测试文件
#vim /www/test.php
<?php
phpinfo();
?>
7.启动nfs,编辑配置文件以供nginx挂载
#vi /etc/exports
/www 172.16.1.1(rw)
#service nfs start
8.在172.16.1.1上挂载172.16.1.2的/www
#mount -t nfs 172.16.1.2:/www /www
浏览器打开172.16.1.1/test.php查看是否能正常调用Phpinfo()函数如果能则继续,如果不能请查找原因
9.安装discuz 浏览器运行172.16.1.1/install,根据提示修改Php.ini的一个选项
#vi /etc/php.ini
short_open_tag = On
10.在172.16.1.3上为discuz建立数据
#mysql
>create database discuz;
>grant all on discuz.* to 'discuz'@'172.16.1.2' identified by 'redhat';
>flush privileges;
>quit
11.继续安装discuz访问172.16.1.1/install,根据提示输入刚才建立的账号密码mysql的地址,到此discuz应该可以安装成功