前面介绍了nginx和memcached的工作机制,接下来我们主要配置下LNMP的编译安装以及将Memcached整合到LNMP中(基于Red Hat Enterprise Linux Server release 5.8)
一、安装Nginx:
1、解决依赖关系
编译安装nginx需要事先需要安装开发包组"Development Tools"和 "Development Libraries"。同时,还需要专门安装pcre-devel包:
- # yum -y install pcre-devel
- # yum -y groupinstall "group_name"
2、安装
首先添加用户nginx,实现以之运行nginx服务进程:
- # groupadd -r nginx
- # useradd -r -g nginx nginx
接着开始编译和安装:nginx-1.2.3.tar.gz下载路径:ftp://172.16.0.1/pub/Sources/nginx
- # tar xvf nginx-1.2.3.tar.gz
- # cd nginx-1.2.3
- # ./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/nginx.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
- # make && make install
说明:如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。
3、为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
而后就可以启动服务并测试了(这一步要保证你的httpd没有在运行):
- # service nginx start
二、安装mysql-5.5.28
1、准备数据存放的文件系统
新建一个逻辑卷,并将其挂载至特定目录即可。(这里不再给出过程)
假设其逻辑卷的挂载目录为/data,而后需要创建/data/mydata目录做为mysql数据的存放目录。
2、新建用户以安全方式运行进程:
- # groupadd -r mysql
- # useradd -g mysql -r -s /sbin/nologin -M -d /data/mydata mysql
- # chown -R mysql:mysql /data/mydata
3、安装并初始化mysql-5.5.28
首先下载平台对应的mysql版本至本地,这里是32位平台,因此,选择的为mysql-5.5.28-linux2.6-i686.tar.gz,其下载位置为ftp://172.16.0.1/pub/Sources/mysql-5.5。
- # tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
- # cd /usr/local/
- # ln -sv mysql-5.5.28-linux2.6-i686 mysql
- # cd mysql
- # chown -R :mysql .
- # scripts/mysql_install_db --user=mysql --datadir=/mydata/data
4、为mysql提供主配置文件:
- # cd /usr/local/mysql
- # cp support-files/my-large.cnf /etc/my.cnf
并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
- thread_concurrency = 2
- datadir = /data/mydata #指定文件存放的位置
5、为mysql提供sysv服务脚本:
- # cd /usr/local/mysql
- # cp support-files/mysql.server /etc/rc.d/init.d/mysqld
添加至服务列表:
- # chkconfig --add mysqld
- # chkconfig mysqld on
为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:
6、输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
- MANPATH /usr/local/mysql/man
7、输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:
- # ln -sv /usr/local/mysql/include /usr/include/mysql
8、输出mysql的库文件给系统库查找路径:
- # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后让系统重新载入系统库:
- # ldconfig -v
9、修改PATH环境变量,让系统可以直接使用mysql的相关命令。
- vim /etc/profile.d/mysql.sh
- # /usr/local/mysql/bin
10、之后便可以启动服务并测试了。
三、编译安装php-5.4.4
1、解决依赖关系:
请配置好yum源(可以是本地系统光盘)后执行如下命令:
- # yum -y groupinstall "X Software Development"
如果想让编译的php支持mcrypt、mhash扩展和libevent,此处还需要下载ftp://172.16.0.1/pub/Sources/ngnix目录中的如下几个rpm包并安装之:
- libmcrypt-2.5.7-5.el5.i386.rpm
- libmcrypt-devel-2.5.7-5.el5.i386.rpm
- mhash-0.9.2-6.el5.i386.rpm
- mhash-devel-0.9.2-6.el5.i386.rpm
- mcrypt-2.6.8-1.el5.i386.rpm
最好使用升级的方式安装上面的rpm包,命令格式如下:
# rpm -Uvh
另外,也可以根据需要安装libevent,系统一般会自带libevent,但版本有些低。因此可以升级安装之,它包含如下两个rpm包。下载路径ftp://172.16.0.1/pub/Sources/memcached
- libevent-2.0.17-2.i386.rpm
- libevent-devel-2.0.17-2.i386.rpm
安装这两个包时,它可能会依赖于其他包的,所以可以用yum -y install 命令来安装
说明:libevent是一个异步事件通知库文件,其API提供了在某文件描述上发生某事件时或其超时时执行回调函数的机制,它主要用来替换事件驱动的网络服务器上的event loop机制。目前来说, libevent支持/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。
2、编译安装php-5.4.8
首先下载源码包至本地目录,下载位置ftp://172.16.0.1/pub/Sources/new_lamp。
- # 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
说明:如果前面第1步解决依赖关系时安装mcrypt相关的两个rpm包,此./configure命令还可以带上--with-mcrypt选项以让php支持mycrpt扩展。--with-snmp选项则用于实现php的SNMP扩展,但此功能要求提前安装net-snmp相关软件包。
- make && make install
为php提供配置文件:
- # cp php.ini-production /etc/php.ini
为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-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文件(如下最后一行):
- pm.max_children = 50
- pm.start_servers = 5
- pm.min_spare_servers = 2
- pm.max_spare_servers = 8
- pid = /usr/local/php/var/run/php-fpm.pid
说明:以上数值可以根据实际需要进行调整
接下来就可以启动php-fpm了:
- # service php-fpm start
使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
- # ps aux | grep php-fpm
四、整合nginx和php
1、编辑/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;
- }
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;
并在所支持的主页面格式中添加php格式的主页,类似如下:(这个直接添加上index.php 即可)
- location / {
- root html;
- index index.php index.html index.htm;
- }
而后重新载入nginx的配置文件:
- # service nginx reload | restart
3、在/usr/html新建index.php的测试页面,测试php是否能正常工作:
- # vim /usr/html/index.php
- <?php
- phpinfo();
- ?>
接着就可以通过浏览器访问此测试页面了。
五、下面为加速请求过程安装缓存:Memcached
Memcached依赖于libevent API,因此要事先安装,我们刚刚已经安装过了,所以这块可以不用再重新安装,如果你想要安装更新版本,可以到ftp://pub/Sources/memcached/下载libevent-2.0.16-stable.tar.gz进行编译安装
我们这里给大家演示下编译安装的过程:
- # tar xf libevent-2.0.20-stable.tar.gz
- # cd libevent-2.0.20-stable
- # ./configure --prefix=/usr/local/libevent
- # make && make install
- # echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
- # ldconfig -v
2、安装配置memcached
1)安装memcached
- # tar xf memcached-1.4.15.tar.gz
- # cd memcached-1.4.15
- # ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
- # make && make install
2)memcached SysV的startup脚本代码如下所示,将其建立为/etc/rc.d/init.d/memcached文件:
- #!/bin/bash
- #
- # Init file for memcached
- #
- # chkconfig: - 86 14
- # description: Distributed memory caching daemon
- #
- # processname: memcached
- # config: /etc/sysconfig/memcached
- . /etc/rc.d/init.d/functions
- ## Default variables
- PORT="11211"
- USER="nobody"
- MAXCONN="1024"
- CACHESIZE="64"
- OPTIONS=""
- RETVAL=0
- prog="/usr/local/memcached/bin/memcached"
- desc="Distributed memory caching"
- lockfile="/var/lock/subsys/memcached"
- start() {
- echo -n $"Starting $desc (memcached): "
- daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE $OPTIONS
- RETVAL=$?
- echo
- [ $RETVAL -eq 0 ] && touch $lockfile
- return $RETVAL
- }
- stop() {
- echo -n $"Shutting down $desc (memcached): "
- killproc $prog
- RETVAL=$?
- echo
- [ $RETVAL -eq 0 ] && rm -f $lockfile
- return $RETVAL
- }
- restart() {
- stop
- start
- }
- reload() {
- echo -n $"Reloading $desc ($prog): "
- killproc $prog -HUP
- RETVAL=$?
- echo
- return $RETVAL
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- restart
- ;;
- condrestart)
- [ -e $lockfile ] && restart
- RETVAL=$?
- ;;
- reload)
- reload
- ;;
- status)
- status $prog
- RETVAL=$?
- ;;
- *)
- echo $"Usage: $0 {start|stop|restart|condrestart|status}"
- RETVAL=1
- esac
- exit $RETVAL
使用如下命令配置memcached成为系统服务:
- # chmod +x /etc/init.d/memcached
- # chkconfig --add memcached
- # service memcached start
3)使用telnet命令测试memcached的使用
Memcached提供一组基本命令用于基于命令行调用其服务或查看服务器状态等。
eg:
- [[email protected] ~]# telnet 127.0.0.1 11211
- Trying 127.0.0.1...
- Connected to localhost.localdomain (127.0.0.1).
- Escape character is '^]'.
- set key 0 60 3
- abd
- STORED
- get key
- VALUE key 0 3
- abd
- END
关于Memcached的命令还有很多,下面会给大家附上一份有关其命令的文件。
六、安装Memcache的PHP扩展
1、安装PHP的memcache扩展
- # tar xf memcache-2.2.5.tgz
- # cd memcache-2.2.5
- /usr/local/php/bin/phpize
- # ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
- # make && make install
上述安装完后会有类似以下的提示:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
2、编辑/etc/php.ini,在“扩展模块”相关的位置如(; extension_dir = "ext")类似的行下添加如下一行来载入memcache扩展:
- extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so
重启服务,而后对memcached功能进行测试
3、安装Memcached管理与监控工具memadmin-master.zip
- unzip memadmin-master.zip
- cd memadmin-master
- mv * /usr/html/
刷新所访问页面,会出现如下管理网页:
默认用户名和密码是admin,你可以在配置文件/usr/html/config.php 中进行修改,登陆进去:
点击开始管理,就可以进入后台对服务器进行监控了。。
七、使用libmemcached的客户端工具:
访问memcached的传统方法是使用基于perl语言开发的Cache::memcached模块,这个模块在大多数perl代码中都能良好的工作,但也有着众所周知的性能方面的问题。libMemcached则是基于C语言开发的开源的C/C++代码访问memcached的库文件,同时,它还提供了数个可以远程使用的memcached管理工具,如memcat, memping,memstat,memslap等。
1) 编译安装libmemcached
- # tar xf libmemcached-1.0.2.tar.gz
- # cd libmemcached-1.0.2
- # ./configure
- # make && make install
- # ldconfig -v
接下来就可以在命令行里调用服务或查看服务状态了
2) 在/usr/local/bin下有一系列客户端工具,可以直接使用了
例如:
- [[email protected] ~]# memping --server=127.0.0.1:11211
- [[email protected] ~]# memstat --server=127.0.0.1:11211
- Server: 127.0.0.1 (11211)
- pid: 29198
- uptime: 3660
- time: 1352786258
- version: 1.4.15
- libevent: 2.0.20-stable
- pointer_size: 32
- rusage_user: 0.003999
- rusage_system: 0.037994
- curr_connections: 10
- ………………………………
八、Nginx整合memcached:
- vim /etc/nginx/nginx.conf
- server {
- listen 80;
- server_name www.magedu.com;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- set $memcached_key $uri;
- memcached_pass 127.0.0.1:11211;
- default_type text/html;
- error_page 404 @fallback;
- }
- location @fallback {
- proxy_pass http://172.16.0.1;
- }
- }
ok,到这里LNMP编译安装以及memcached缓存系统的安装就告一段落了,动手尝试一下~~
转载于:https://blog.51cto.com/90112526/1059979