一、准备所需要的环境包组“Development tools”和“Server Platform Development”
#yum groupinstall "Development tools""Server Platform Development"
二、安装nginx
1、编译安装
# yum -y install pcre-devel
#tar xf nginx-1.4.2.tar.gz -C /usr/local/
#cd /usr/local/nginx-1.4.2
# groupadd -r nginx
# useradd -r -g nginx nginx
# ./configure \
--prefix=/usr \ #指定nginx的安装路径
--sbin-path=/usr/sbin/nginx \ #指定sbin的路径
--conf-path=/etc/nginx/nginx.conf \#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 \ #以nginx用户身份
--group=nginx \ #以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
编译过程中如果提示我们还需要依赖某个包时,我们要安装对应的devel包
# 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/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/rc.d/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on
# service nginx start
三、安装mysql
1:分区格式化挂载
#fdisk /dev/sda #创建磁盘分区
#pvcreate /dev/sda{3,5} #创建物理分区
#vgcreate myvg /dev/sda{3,5} #创建卷组为myvg
#lvcreate -L 20G -n mydata myvg #创建逻辑卷mydat
#mke2fs -t ext4 -L MYDATA -b 2048 /dev/myvg/mydata
#mkdir /mydata
cat /etc/fstab
LABEL=MYDATA /mydata ext4 defaults 0 0
#mount -a
#mkdir /mydata/data
2:解压安装
#useradd -r mysql
#chown -R mysql:mysql /mydata/data
#tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local
#cd /usr/local/
#ln -sv mysql-5.5.33-linux2.6-x86_64 mysql
#chown -R root:mysql /usr/local/mysql/*
#cd mysql/support-files #有样例,我们可以根据自己的需要选择
#cp my-large.cnf /etc/my.cnf
3:修改配置文件/etc/my.cnf
找到相关设置进行修改操作
thread_concurrency = 4 #并发核心数, 如果物理核心为2,这里就可以填写4
datadir = /mydata/data
innodb_file_per_table = 1 #每表一个表空间
#cp mysql.server /etc/rc.d/init.d/mysqld
#chmod +x /etc/rc.d/init.d/mysqld
#cd /usr/local/mysql/
#scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ #初始化成功
#chkconfig --add mysqld
#service mysqld start
# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
# . /etc/profile.d/mysql.sh #执行后就可以连mysql了
4:因为我们的安装不在标准路径下,所以还要导出相关文件
#vim /etc/man.config #导出帮助文件
MANPATH /usr/local/mysql/man
#ln -sv /usr/local/mysql/include /usr/local/mysql
#vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
#mysql
#mysql> use mysql
mysql> select user,host,password from user; #查看有哪些用户
mysql> drop user ''@localhost; #删除不安全的用户
mysql> update user set password=PASSWORD('mypass')where user='root';
mysql> flush privileges; #刷新授权表
四、安装php-fpm
1、php-5.4.19.tar.bz2
# tar xf php-5.4.19.tar.bz2
# cd php-5.4.19
# ./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 install
2、为php提供配置文件:
# cp php.ini-production /etc/php.ini
# 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
# 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
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
接下来就可以启动php-fpm了:
# service php-fpm start
# ps aux | grep php-fpm
服务脚本启动时没有OK字样我么可以编辑服务脚本,在其中加入这么两行
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions # 脚本首行
daemon $php_fpm_BIN --daemonize $php_opts
#service php-fpm restart #重新启动php-fpm
五、整合nginx和php-fpm
1、#vim /etc/nginx/nginx.conf,启用如下选项:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
并在所支持的主页面格式中添加php格式的主页,类似如下:
location / {
root html;
index index.php index.html index.htm;
}
2、编辑/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的配置文件:
# service nginx reload
3、在/usr/html新建index.php的测试页面
<h1>new</h1>
<?php
phpinfo();
?>
然后通过浏览器访问此测试页面
六、安装xcache (为php进行加速)
1、xcache-3.0.3.tar.bz2
# tar xfxcache-3.0.3.tar.bz2
#cd xcache-3.0.3
# /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-zts-20100525/
2、整合php和xcache
首先将xcache提供的样例配置导入php.ini
# mkdir /etc/php.d
# cp xcache.ini /etc/php.d
#vim /etc/php.d/xcache.ini
找到extension开头的行,修改为如下行:
注意:如果php.ini文件中有多条extension指令行,要确保此新增的行排在第一位。
3、重新启动php-fpm
# service php-fpm restart
可以通过别的虚拟机对我们配置的服务进行压力测试,就能体会到php加速器的效果了
#ab -c 1000 -n 10000 http://172.16.4.7/index.php
七、安装phpMyAdmin
phpMyAdmin-4.0.5-all-languages.zip
# unzip phpMyAdmin-4.0.5-all-languages.zip
#cd phpMyAdmin-4.0.5-all-languages
#mv * /usr/html/
然后通过浏览器进入登陆页面,所以我们还要在mysql中授权一个用户
授权
GRANT ALL ON *.* TO 'user'@'172.16.%.%' IDENTIFIED BY 'mypass';有特殊符号需要用引号引起来,这里要根据需要进行授权以增强其安全性