LNMMP:Linux Nginx+MySQL+Memcached+PHP
架构图:
实现过程
一 、172.16.13.10 Mysql服务器上安装MariaDB-10
1创建逻辑卷用于存放数据 #fdisk /dev/sda n +5G t 10 8e w 创建了/dev/sda10 #pvcreate /dev/sda10 #vgcreate myvg /dev/sda10 #lvcreate -L 5G -n mydata /dev/myvg/mydata #mke2fs -t ext4 /dev/myvg/mydata 设置开机自动挂载 #vim /etc/fstab /dev/myvg/mydata /mydata/data ext4,acl defaults 0 0 #mount -a #mount 查看挂载情况 2二进制程序包安装 下载二进制程序包 mariadb-10.0.10-linux-x86_64.tar.gz #tar xf mariadb-10.0.10-linux-x86_64.tar.gz -C /usr/local #cd /usr/local/ #ln -sv mariadb-10.0.10-linux-x86_64/ mysql #cd mysql #chown -R root.mysql ./* 初始化数据库 #cd /usr/local/mysql # scripts/mysql_install_db --user=mysql --datadir=/mydata/data # mkdir /etc/mysql -pv # cp support-files/my-large.cnf /etc/mysql/my.cnf 提供配置文件 # cp support-files/mysql.server /etc/rc.d/init.d/mysqld 提供服务脚本 # chmod +x /etc/rc.d/init.d/mysqld # vim /etc/mysql/my.cnf 添加如下两项 datadir = /mydata/data innodb_file_per_table = ON #chkconfig --add mysqld #chkconfig mysqld on #chkconfig --list mysqld #ss -ntl | grep 3306
二、172.16.13.11PHP服务器编译安装 php
下载源包 php-5.4.26.tar.bz2 解决依赖关系 # yum -y install libcurl-devel bzip2-devel gd-devel libxml2-devel mhash-devel libmcrypt-devel # 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
提供php配置文件 # cd php-5.4.26 # cp php.ini-production /etc/php.ini 提供php-fpm服务脚本 #cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #chmod +x /etc/rc.d/init.d/php-fpm 为php-fpm服务脚本提供配置文件 #cd /usr/local/php/etc/ #mv php-fpm.conf.default php-fpm.conf # vim php-fpm.conf 编辑配置文件 启动下列选项并修改参数如下 pid = /usr/local/php/var/run/php-fpm.pid error_log = /var/log/php-fpm.log listen = 172.16.13.11:9000 rlimit_files = 10240 pm = dynamic pm.max_children = 128 pm.start_servers = 5 pm.min_spare_servers = 3 pm.max_spare_servers = 5 pm.status_path = /status ping.path = /ping ping.response = pong # chkconfig --add php-fpm #chkconfig php-fpm on #chkconfig --list php-fpm #service php-fpm start # ss -ntl | grep 9000
提供一个一个测试页 #mkdir /www/b.org -pv #vim /www/b.org/index.php <?php phpinfo (); ?>
安装xcache 为php加速 1、安装 # tar xf xcache-2.0.0.tar.gz # cd xcache-2.0.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-20100525/ 2、编辑php.ini,整合php和xcache: 首先将xcache提供的样例配置导入php.ini # mkdir /etc/php.d # cp xcache.ini /etc/php.d 说明:xcache.ini文件在xcache的源码目录中。 接下来编辑/etc/php/php.ini,找到extension开头的行,修改为如下行: extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so 注意:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。 3、重新启动php-fpm # service php-fpm restart
三、172.16.13.9 Nginx web服务器 编译安装nginx-1.4.7
nginx的编译安装请参照我的博客; 定义虚拟主机和URI #vim /etc/nginx/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /www/b.org; index index.html index.htm; } } }
定义一个网页 # mkdir -pv /www/b.org # vim /www/b.org <h1>node9.linux.com</h1>
四、172.16.13.8 Nginx代理服务器 编译安装nginx-1.4.7
nginx的编译安装请参照我的博客: 启动fastcgi_pass模块 # vim /etc/nginx/nginx.conf user nginx; worker_processes 2; worker_cpu_affinity 0101 1010; worker_rlimit_nofile 65535; worker_priority -15; error_log /var/log/nginx/error.log notice; pid /var/run/nginx/nginx.pid; master_process on; timer_resolution 100ms; events { use epoll; worker_connections 20480; accept_mutex on; accept_mutex_delay 200ms; multi_accept on; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.b.org; location / { root /www/b.org; index index.php index.html index.htm; proxy_pass http://172.16.13.9; } error_page 500 502 503 504 /50x.html; location ~ \.php$ { root /www/b.org/; fastcgi_pass 172.16.13.11:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name; include fastcgi_params; } } } 修改nginx代理服务器的参数fastcgi_params #vim /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; # service nginx reload 创建网站根目录,并提供index.html 和index.php文件 # mkdir -pv /www/b.org # vim /www/b.org/index.html 无需填入内容 # vim /www/b.org/index.php 无需填入内容 # ls /www/b.org
客户端测试
1 静态页面访问
2 动态页面访问
已成功实现了静态页面和动态页面的分离;
获取php-fpm的状态 1编辑配置文件 #vim /etc/nginx/nginx.conf 在虚拟主机中添加如下location location ~* /(status|ping) { root /www/b.org; fastcgi_pass 172.16.13.11:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; include fastcgi_params; } #service nginx reload
2 客户端访问
五 Memcached服务器 172.16.13.12 安装Memcached
yum安装 #yum -y install memcached 编辑配置文件 # vim /etc/sysconfig/memcached 保持默认配置即可 启动服务进程 # service memcached start
六 php-fpm服务器 172.16.13.11 编译安装memcache
准备给php-fpm服务器做缓存,以减轻后边的MySQL服务器的负担; 下载源码包 memcache-2.2.7.tgz # tar xf memcache-2.2.7.tgz # cd memcache-2.2.7 # /usr/local/php/bin/phpize 让php识别该模块 # ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache # make && make install /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/ # vim /etc/php.ini 添加如下拓展模块 extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so # service php-fpm restart 重新装载模块 客户端查看php-fpm是否已经具有memcache功能
测试memcached功能 php-fpm服务器上添加一个php测试页 #vim /www/b.org/memcache.php <?php $mem = new Memcache; $mem->connect("172.16.13.12", 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."; ?> 客户端测试
php-fpm服务已成功关联到memcached服务器。下边我们来测试其缓存效果
配置memadmin-master 监控 php-fpm服务器配置 memadmin-master #下载 memadmin-master.zip #uzip memadmin-master.zip #cd memadmin-master #ls 同时将memadmin-master拷贝到 nginx web服务器一份 172.16.13.9 #scp -r memadmin-master [email protected]:/www/b.org/
登录memadmin-master 进行监控
接下来我们就可以检测php-fpm服务器的缓存情况了
总结:LNMMP架构已经成功搭建!
PS:个人水平有限,如有不妥之处请指出,待完善!