首页 > 代码库 > Lnmmp动静分离简易架构
Lnmmp动静分离简易架构
拓扑如下图所示;
本次环境全部为: Centos6.5-64位;
一、先安装Mariadb数据库(编译安装与mysql的安装方法一样,此处不做详解);通用二进制格式安装;
# mkdir -pv /mydata/data/ # chown -R mysql:mysq /mydata/data/ # tar xf mariadb-10.0.10-linux-x86_64.tar.gz -C /usr/local/ # ln -sv mariadb-10.0.10-linux-x86_64 mysql # cd mysql # chown -R root:mysql ./* # scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ mkdir /etc/mysql # cp support-files/my-large.cnf /etc/mysql/my.cnf # cp support-files/mysql.server /etc/init.d/mysqld # chmod +x /etc/init.d/mysqld # chkconfig --add mysqld # vim /etc/mysql/my.cnf datadir = /mydata/data innodb_file_per_table = ON #service mysqld start # cat .my.conf [Client] user = ‘root‘ password = ‘redhat‘ host = ‘localhost‘ #mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 1340 Server version: 10.0.10-MariaDB-log MariaDB Server Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> 授权用户访问网段; MariaDB [(none)]> grant all on *.* to ‘root‘@‘172.16.34.%‘ identified by ‘redhat‘; 创建一个数据库后面操作需要; MariaDB [(none)]> create database nbadb; MariaDB [(none)]>
数据库安装完成,操作暂且到此;
二、编译安装Nginx;
1、在安装开始前,先装几个开发包组及创建nginx用户,组:
"Development Tools"、"Server Platform Development"、并安装"pcre-devel包" # yum groupinstall -y "Development Tools""Server Platform Development" # yum install pcre-devel openssl-devel zlib-devel # groupadd -r nginx # useradd -r -g nginx nginx |
2、编译安装;
# tar xf nginx-1.4.7^C # cd nginx-1.4.7 # ./configure --prefix=/usr \ 安装目录 --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 \ nginx 用户 --group=nginx \ nginx组 --with-http_ssl_module \ 加载ssl模块 --with-http_flv_module \ 加载流媒体模块 --with-http_stub_status_module \ 页面状态 --with-http_gzip_static_module \ 加载gzip页面压缩功能 --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
3、为nginx提供Sysve脚本;
#!/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=http://www.mamicode.com/`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
4、为脚本添加执行权限及让其开机自启动;
#chmod +x nginx #chkconfig --add nginx #service nginx start #ss -tnl 查看web服务端口是否启动; LISTEN 0 128 *:80 *:* users:(("nginx",23510,6),("nginx",23512,6),("nginx",23513,6),("nginx",23514,6),("nginx",23515,6),("nginx",23516,6))
nginx安装完成,暂且到此;
三、安装PHP;
1、安装前先解决php所依赖一些软件包;
# yum install -y libmcrypt-devel libcurl-devel gd-devel libxm12-devel mhash-devel libcurl-devel bzip2 bzip2-devel
2、编译安装php-5.4.26.tar.bz2;
#tar xf php-5.4.26.tar.bz2 #cd php-5.4.26 # ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr/local/libxml2/ --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl #make && make install
3、为php提供配置文件;
# cp php.ini-production /etc/php.ini
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
5、为php-fpm提供配置文件:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
6、编辑php-fpm的配置文件:配置fpm的相关选项为你所需要的值,并启用pid文件;
# 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 pm.status_path = /status listen = 172.16.34.3:9000
7、接下来就可以启动php-fpm了:
# service php-fpm start
<h1> PHP server</h1> <?php $link = mysql_connect(‘172.16.34.5‘,‘root‘,‘redhat‘); if ($link) echo "Success..."; else echo "Failure..."; mysql_close(); phpinfo(); ?>
四、安装memcached缓存服务器;
Memcached是一款开源、高性能、分布式内存对象缓存系统,可应用各种需要缓存的场景,其主要目的是通过降低对Database的访问来加速web应用程序。它是一个基于内存的“键值对”存储,用于存储数据库调用、API调用或页面引用结果的直接数据,如字符串、对象等。
memcached是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件。现在
已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。
Memcached是一款开发工具,它既不是一个代码加速器,也不是数据库中间件。其设计哲学思想主要反映在如下方面:
1. 简单key/value存储:服务器不关心数据本身的意义及结构,只要是可序列化数据即可。存储项由“键、过期时间、可选的标志及数据”四个部分组成;
2. 功能的实现一半依赖于客户端,一半基于服务器端:客户负责发送存储项至服务器端、从服务端获取数据以及无法连接至服务器时采用相应的动作;服务端负责接收、存储数据,并负责数据项的超时过期;
3. 各服务器间彼此无视:不在服务器间进行数据同步;
4. O(1)的执行效率
5. 清理超期数据:默认情况下,Memcached是一个LRU缓存,同时,它按事先预订的时长清理超期数据;但事实上,memcached不会删除任何已缓存数据,只是在其过期之后不再为客户所见;而且,memcached也不会真正按期限清理缓存,而仅是当get命令到达时检查其时长;
1、解决依赖关系;
#yum install -y libevent-devel
2、编译安装;
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 instal
3、为memcached提供sysv服务启动脚本;
#vim /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 -o "$OPTIONS" RETVAL=$? [ $RETVAL -eq 0 ] && success && touch $lockfile || failure echo return $RETVAL } stop() { echo -n $"Shutting down $desc (memcached): " killproc $prog RETVAL=$? [ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure echo return $RETVAL } restart() { stop start } reload() { echo -n $"Reloading $desc ($prog): " killproc $prog -HUP RETVAL=$? [ $RETVAL -eq 0 ] && success || failure 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
4、为脚本添加执行权限及添加至开机自启动;
chmod +x /etc/init.d/memcached chkconfig --add memcached service memcached start
启动时报如下错误;俱体原因不详;但是找到了解决办法;
/usr/local/memcached/bin/memcached -d -m 1024 -p 11211 -u root
5、使用telnet测试是否可以连接成功;如下图
五、在php-fpm服务器上安装mamcache扩展;
1、编译安装memcache-2.2.6.tgz;
# tar xf memcache-2.2.6.tgz # cd memcache-2.2.6 # /usr/local/php/bin/phpize 执行命令,让php识别这个模块 Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 # ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache # make && make install
2、上述安装完后会有类似以下的提示:
3、编辑etc/php.ini,在“动态模块”相关的位置添加如下一行来载入memcache扩展:
# vim /etc/php.ini 打开php的配置文件,添加如下项; extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so # service php-fpm restart
4、而后对memcached功能进行测试,在网站目录中建立测试页面memcache.php,添加如下内容:
vim /usr/local/nginx/html/test.php
<?php $mem = new Memcache; $mem->connect("172.16.34.2", 11211) or die("Could not connect"); $version = $mem->getVersion(); echo "Server‘s version: ".$version."<br/>\n"; $mem->set(‘hellokey‘, ‘Hello World‘, 0, 600) or die("Failed to save data at the memcached server"); echo "Store data in the cache (data will expire in 600 seconds)<br/>\n"; $get_result = $mem->get(‘hellokey‘); echo "$get_result is from memcached server."; ?>
5、测试其功能是否正常工作;
六、对前端代理nginx操作,实现动静分离;
1、修改配置;vim /etc/nginx/nginx.conf
worker_processes 5; 进程个数 events { worker_connections 1024; 每个worker进程能够响应的最大请求数 } http { include mime.types; 支持多媒体类型 default_type application/octet-stream; sendfile on; 由内核转发 keepalive_timeout 5; 持久连接时间 gzip on; 压缩功能 server { listen 80; server_name www.ouyang.com; add_header X-via $server_addr; location / { root html; index index.php index.html index.html; } location ~* \.(jpg|jpeg|png|gif|js|css)$ { root html; } location ~ \.php$ { root html; fastcgi_pass 172.16.34.3:9000; 代理后端动态php服务器 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } }
2、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容;
#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;
3、重启nginx服务;
[root@ou nginx]# service nginx restart nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful Stopping nginx: [ OK ] Starting nginx: [ OK ]
4、动静分离测试;
1、在后端php服务器/usr/local/nginx/html目录下创建index.php(在前端代理服务器相同的路径下也需要一份),由于前端代理服务要响应静态内容的页面,因此在放入一张图片,用于测试;后端就不需
# cd /usr/local/nginx/html/ [root@ou html]# pwd /usr/local/nginx/html 50x.html index.html index.php nba.jpg
[root@php html]# pwd /usr/local/nginx/html [root@php html]# ls index.php
2、写入如下内容,用于测试;vim index.php
<h1> PHP server</h1> <?php $link = mysql_connect(‘172.16.34.5‘,‘root‘,‘redhat‘); if ($link) echo "Success..."; else echo "Failure..."; mysql_close(); phpinfo(); ?>
由于本次实验没有做DNS域名解析,因此需要把www.ouyang.com域名写入到win7的hosts文件当中;C:\Windows\System32\drivers\etc\hosts
# localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 172.16.34.1 www.ouyang.com
3、现在即可测试;
静态页面;
动态测试;数据库也连接成功;
七、安装wordpress(前端nginx和后端的php都需要安装wordpress),配置一样;
cd /usr/local/nginx/html # unzip wordpress-3.3.1-zh_CN.zip # cd wordpress # cp wp-config-sample.php wp-config.php # vim wp-config.php define(‘DB_NAME‘, ‘nbadb‘); /** MySQL 数据库用户名 */ define(‘DB_USER‘, ‘root‘); /** MySQL 数据库密码 */ define(‘DB_PASSWORD‘, ‘redhat‘); /** MySQL 主机 */ define(‘DB_HOST‘, ‘172.16.34.5‘); /** 创建数据表时默认的文字编码 */ define(‘DB_CHARSET‘, ‘utf8‘); /** 数据库整理类型。如不确定请勿更改 */ define(‘DB_COLLATE‘, ‘‘);
ok!
八、安装memadmi-master监控memcached状态(前端代理与php都要安装memadmin-master)两端操作一样;
#cd /usr/local/nginx/html #unzip memadmin-master.zip
九、在前端设置前端代理的缓存功能;
fastcgi_param;定义传递给fpm服务器的参数
没有定义缓存的测试如果;# ab -n 1000 -c 100 http://www.ouyang.com/
定义vim /etc/nginx/nginx.conf
再次测试;# ab -n 1000 -c 100 http://www.ouyang.com/
本文出自 “春天里” 博客,请务必保留此出处http://wfnygah.blog.51cto.com/7150625/1405191