LNMMP:Linux Nginx+MySQL+Memcached+PHP
php
架構圖:html
實現過程node
一 、172.16.13.10 Mysql服務器上安裝MariaDB-10 mysql
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
2、172.16.13.11PHP服務器編譯安裝 php linux
下載源包 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加速 一、安裝 # 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/ 二、編輯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指令行,要確保此新增的行排在第一位。 三、從新啓動php-fpm # service php-fpm restart
3、172.16.13.9 Nginx web服務器 編譯安裝nginx-1.4.7 nginx
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>
4、172.16.13.8 Nginx代理服務器 編譯安裝nginx-1.4.7web
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
客戶端測試sql
1 靜態頁面訪問 數據庫
2 動態頁面訪問vim
已成功實現了靜態頁面和動態頁面的分離;
獲取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 root@172.16.13.9:/www/b.org/
登陸memadmin-master 進行監控
接下來咱們就能夠檢測php-fpm服務器的緩存狀況了
總結:LNMMP架構已經成功搭建!
PS:我的水平有限,若有不妥之處請指出,待完善!