先介绍下Nginx: Nginx的代码是由一个核心和一系列的模块组成, 核心主要用于提供Web Server的基本功能,以及Web和Mail反向代理的功能;还用于启用网络协议,创建必要的运行时环境以及确保不同的模块之间平滑地进行交互。不过,大多跟协议相关的功能和某应用特有的功能都是由nginx的模块实现的.
- useradd -r -M -s /sbin/nologin nginx
- ./configure \
- --prefix=/usr/local/lnmp \
- --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/nginx.lock \
- --user=nginx \
- --group=nginx \
- --with-http_ssl_module \ 启用ssl的加密
- --with-http_flv_module \ 启用flv模块
- --with-http_stub_status_module \ 启用监控nginx运行状态的模块
- --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 启用http-rewrite
- ./configure: error: the HTTP rewrite module requires the PCRE library.
- You can either disable the module by using --without-http_rewrite_module
- option, or install the PCRE library into the system, or build the PCRE library
- statically from the source with nginx by using --with-pcre=<path> option.
- yum -y install pcre-devel
- #!/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
- [root@server30 ~]# vim /etc/rc.d/init.d/nginx
- [root@server30 ~]# chmod +x /etc/rc.d/init.d/nginx
- [root@server30 ~]# chkconfig --add nginx
- [root@server30 ~]# chkconfig nginx on
- [root@server30 ~]# service nginx start
- [root@server30 ~]# useradd -r -s /sbin/nologin mysql
- [root@server30 ~]# tar xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local/lamp/
- [root@server30 ~]# ln -s /usr/local/lamp/mysql-5.5.24-linux2.6-i686/ /usr/local/mysql
- [root@server30 ~]# fdisk /dev/sda
- [root@server30 ~]# partprobe /dev/sda
- [root@server30 ~]# pvcreate /dev/sda5
- Writing physical volume data to disk "/dev/sda5"
- Physical volume "/dev/sda5" successfully created
- [root@server30 ~]# vgcreate myvg /dev/sda5
- Volume group "myvg" successfully created
- [root@server30 ~]# lvcreate -L 5G -n mydata myvg
- Logical volume "mydata" created
- [root@server30 ~]# mke2fs -j -L MYDATA /dev/myvg/mydata
- [root@server30 ~]# mkdir /data
- [root@server30 ~]# vim /etc/fstab
- [root@server30 ~]# mount -a
- [root@server30 mysql]# mkdir /data/mysql
- [root@server30 mysql]# chown -R mysql:mysql /data/mysql/
(2)初始化mysql,指定运行mysql的用户,与数据存储的位置
- [root@server30 mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
(3)为mysql提供配置文件,并修改其中的参数,/etc/my.cnf
thread_concurrency = 4 将原来的8修改成4。
datadir = /data/mysql 并添加这一行
- [root@server30 mysql]# cp support-files/my-large.cnf /etc/my.cnf
- [root@server30 mysql]# vim /etc/my.cnf
(4)为mysql提供sysv服务脚本。
- [root@server30 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- [root@server30 mysql]# chkconfig --add mysqld
- [root@server30 mysql]# chkconfig mysqld on
- [root@server30 mysql]# service mysqld start
(5)为mysql提供man文档编辑/etc/man.config,添加一行
MANPATH=/usr/local/mysql/man
(6)输出mysql的头文件至系统头文件路径/usr/include
- [root@server30 ~]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
(6)输出mysql的库文件给系统库查找路径:
- [root@server30 ~]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
- [root@server30 ~]# ldconfig (通知系统更新库文件)
(7)修改PATH环境变量,让系统可以直接使用mysql的相关命令。编辑/etc/profile,添加一行PATH=/usr/local/mysql/bin:$PATH
并执行export PATH=/usr/local/mysql/bin:$PATH
到此mysql安装完。
三,(1)编译安装php,如果想让php支持加密需要安装mcrypt,则mcrpyt由libmcrypt-2.5.7-5.el5.i386.rpm,libmcrypt-devel-2.5.7-5.el5.i386.rpm提供库。因此安装需要先安装这三个rpm包,启用mhash编码安装mhash-0.9.2-6.el5.i386.rpm ,mhash-devel-0.9.2-6.el5.i386.rpm.启用libevent,需要安装libevent-2.0.17-2.i386.rpmlibevent-devel-2.0.17-2.i386.rpm。
- [root@server30 ~]# rpm -ivh libmcrypt-*
- [root@server30 ~]# rpm -ivh mhash-*
- [root@server30 ~]# rpm -ivh mcrypt-2.6.8-1.el5.i386.rpm
- [root@server30 ~]# rpm -ivh libevent-2.0.17-2.i386.rpm
- [root@server30 ~]# rpm -ivh libevent-devel-2.0.17-2.i386.rpm
- [root@server30 php-5.4.4]#./configure --prefix=/usr/local/lamp/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
- [root@server30 php-5.4.4]# make && make intall
(2)为php提供配置文件
- [root@server30 php-5.4.4]# cp php.ini-production /etc/php.ini
(3)为php-fpm提供脚本,并将其添加到服务列表:
- [root@server30 php-5.4.4]# cp php.ini-production /etc/php.ini
- [root@server30 php-5.4.4]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
- [root@server30 php-5.4.4]# chmod +x /etc/rc.d/init.d/php-fpm
- [root@server30 php-5.4.4]# chkconfig --add php-fpm
- [root@server30 php-5.4.4]# chkconfig php-fpm on
(4)为php-fpm提供配置文件:
- [root@server30 php-5.4.4]# cp /usr/local/lamp/php/etc/php-fpm.conf.default /usr/local/lamp/php/etc/php-fpm.conf
(5)编辑php-fpm的配置文件:
# vim /usr/local/php/etc/php-fpm.conf
配置pm.的相关选项为你所需要的值,并启用pid文件
- pid = /usr/local/php/var/run/php-fpm.pid (启用pid文件,这个路径在php-fpm脚本中定义了,这一行需要启用)
- pm.max_children = 50 (最大子进程)
- pm.start_servers = 10 (刚启动时启动多少个进程)
- pm.min_spare_servers = 5 (最小空闲进程)
- pm.max_spare_servers = 10 (最多空闲进程)
(6)接下来就可以启动php-fpm了:
- [root@server30 php-5.4.4]# service php-fpm start
- Starting php-fpm . done
使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
- [root@server30 php-5.4.4]# ps aux |grep php-fpm
- root 10934 0.0 0.5 33848 2784 ? Ss 20:38 0:00 php-fpm: master process (/usr/local/lamp/php/etc/php-fpm.co
四,整合nginx和php5
(1)编辑/etc/nginx/nginx.conf,启用如下选项:
- location / {
- root html;
- index index.php index.html index.htm; (添加index.php格式的网页)
- }
- location ~ \.php$ {
- root html;
- fastcgi_pass 127.0.0.1:9000; (php与nginx安装在同一台主机,因此监听的IP为本机)
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- include fastcgi_params;(fastcgi_param的参数)
- }
(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的配置文件:
- [root@server30 php-5.4.4]# service nginx reload
(4)在/usr/local/lamp/html/新建index.php的测试页面,测试php是否能正常工作。
- <?php
- phpinfo();
- ?>
重新载入nginx的配置文件,并在浏览器中输入ip,能不能正常访问。
五、安装xcache,为php加速:
1、安装
- [root@server30 ~]# tar xcache-2.0.0.tar.bz2
- tar: Old option `b' requires an argument.
- Try `tar --help' or `tar --usage' for more information.
- [root@server30 ~]# tar xf xcache-2.0.0.tar.bz2
- [root@server30 ~]# cd xcache-2.0.0
- [root@server30 xcache-2.0.0]# /usr/local/lamp/php/bin/phpize
- Configuring for:
- PHP Api Version: 20100412
- Zend Module Api No: 20100525
- Zend Extension Api No: 220100525
- [root@server30 xcache-2.0.0]# ./configure --enable-xcache --with-php-config=/usr/local/lamp/php/bin/php-config
- [root@server30 xcache-2.0.0]# make
- [root@server30 xcache-2.0.0]# make install
- Installing shared extensions: /usr/local/lamp/php/lib/php/extensions/no-debug-non-zts-20100525/
- 这个路径很重要,需要将此路径添加到xcache.ini中
2、编辑php.ini,整合php和xcache:
首先将xcache提供的样例配置导入php.ini
- [root@server30 xcache-2.0.0]# mkdir -pv /etc/php.d (此目录在编译php已经指定了)
- mkdir: created directory `/etc/php.d'
- [root@server30 xcache-2.0.0]# cp xcache.ini /etc/php.d/
- [root@server30 xcache-2.0.0]# vim /etc/php.d/xcache.ini
- 在xcache.ini中修改原来的zend_extension的路径
- zend_extension =/usr/local/lamp/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
3、重新启动php-fpm:service php-fpm restart
打开网页,查看没有出现xcache。如果出现了,则表示正常。