实验系统:CentOS 6.6_x86_64
实验前提:大部分软件使用编译安装,请提前准备好编译环境,防火墙和selinux都关闭
实验软件:nginx-1.9.3 mariadb-10.0.20 php-5.6.11 memcache-2.2.7 xcache-3.2.0
实验拓扑:
一、安装nginx
1.解决依赖关系:
需要专门安装pcre-devel包:
yum -y install pcre-devel
2.添加nginx用户:
useradd -r nginx
3.解压并编译安装nginx:
tar xf nginx-1.9.3.tar.gz
cd nginx-1.9.3
./configure \
--prefix=/usr/local/nginx \ //安装位置
--sbin-path=/usr/local/nginx/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 \ //pid文件位置
--lock-path=/var/lock/nginx.lock \ //锁文件位置
--user=nginx \ //运行进程时使用的用户身份
--group=nginx \ //运行进程时使用的用户组
--with-http_ssl_module \ //支持ssl模块
--with-http_flv_module \ //支持flv模块
--with-http_stub_status_module \ //支持stub_status模块
--with-http_gzip_static_module \ //支持gzip_static模块
--http-client-body-temp-path=/var/tmp/nginx/client/ \ //存储HTTP客户端请求body体的临时文件位置
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \ //存储HTTP代理的临时文件位置
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ //存储fasycgi临时文件位置
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ //存储uwsgi临时文件位置
--http-scgi-temp-path=/var/tmp/nginx/scgi \ //存储scgi临时文件位置
--with-pcre //支持pcre库
make && make install
4.提供脚本文件:
vim /etc/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/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' -`
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/nginx
chkconfig --add nginx
chkconfig nginx on
service nginx start
5.测试访问页面,nginx安装完毕:
二、安装mysql
1.添加mysql用户和创建数据目录:
useradd -r mysql
mkdir -pv /mydata/data
chown -R mysql:mysql /mydata/data
2.解压并初始化mysql:
tar xf mariadb-10.0.20-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.0.20-linux-x86_64 mysql
cd mysql/
chown -R root:mysql .
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
3.提供配置文件:
cp support-files/my-large.cnf /etc/my.cnf
vim /etc/my.cnf
修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如:thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:datadir = /mydata/data
4.提供脚本文件:
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
使用mysql目录的下的bin/mysql去连接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
三、安装PHP
1.解决依赖关系:
yum -y install libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel
2.编译安装php:
./configure --prefix=/usr/local/php \ //安装位置
--with-mysql \ //支持mysql
--with-pdo-mysql \ //支持pdo模块
--with-mysqli \ //支持mysqli模块
--with-openssl \ //支持openssl模块
--enable-fpm \ //支持fpm模式
--enable-sockets \ //启用socket支持
--enable-sysvshm \ //启用系统共享内存支持
--enable-mbstring \ //使多字节字符串的支持
--with-freetype-dir \ //设置FreeType安装前缀路径
--with-jpeg-dir \ //设置libjpeg安装前缀路径
--with-png-dir \ //设置libpng安装前缀路径
--with-zlib-dir \ //设置libz安装前缀路径
--with-libxml-dir=/usr \ //设置libxml2安装路径
--enable-xml \
--with-mhash \ //支持mhash
--with-mcrypt \ //支持mcrypt
--with-config-file-path=/etc \ //配置文件路径
--with-config-file-scan-dir=/etc/php.d \ //配置文件扫描路径
--with-bz2 \ //支持BZip2
--with-curl //支持curl
make && make install
3.提供配置文件:
cp php.ini-production /etc/php.ini
4.为php-fpm提供脚本:
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
5.提供php-fpm配置文件并编辑:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
------------------------------------------------------
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid
6.启动php-fpm服务:
service php-fpm start
四、整合nginx与PHP
1.nginx服务器建立网页文件存放目录/www,并修改其权限:
mkdir /www
chown -R nginx:nginx /www
2.修改nginx配置文件:
vim /etc/nginx/nginx.conf
--------------------------------------
location / {
root /www;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /www;
fastcgi_pass 192.168.19.92:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3.修改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;
4.在PHP服务器上,建立nginx用户,要保证和nginx服务器上的nginx用户id号、组id号一致:
5.修改php-fpm配置文件,并重启:
vim /usr/local/php/etc/php-fpm.conf
---------------------------------------------
listen = 192.168.19.92:9000 //监听物理网卡地址,供其它机器调用
user = nginx //php-fpm以nginx用户运行
group = nginx
---------------------------------------------
service php-fpm restart
6.在PHP服务器上创建/www目录,并开启nfs服务:
mkdir /www
chown -R nginx:nginx /www
vim /etc/exports
---------------------------------------------
/www 192.168.19.0/24(rw,no_root_squash)
---------------------------------------------
service nfs start
7.nginx服务器挂载nfs文件,并测试php,测试成功后删除index.php:
mount -t nfs 192.168.19.92:/www /www
vim /www/index.php
---------------------------------------
<?php
phpinfo();
?>
--------------------------------------
service nginx restart
五、整合PHP与MYSQL
在mysql服务器上创建php服务器能够访问的数据库和用户:
/usr/local/mysql/bin/mysql
--------------------------------------------
CREATE DATABASE wordpress;
GRANT ALL ON wordpress.* TO 'wordpress'@'192.168.19.92' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
六、安装wordpress
1.在/www文件夹下放入网页文件
2.访问http://192.168.19.83,并按提示进行安装,配置没问题则会安装成功
七、为php安装xcache
1.解压并安装:
tar xf xcache-3.2.0.tar.gz
cd xcache-3.2.0
/usr/local/php/bin/phpize
./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
make && make install
完成后,会出现:Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
2.加载模块:
vim /etc/php.ini
-----------------------------------
extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so //找到extension配置的地方,加上此句
3.两个加速模块都安装完毕,重启php-fpm:
service php-fpm restart
4.创建一个php测试页,并查看模块是否加载成功: