部署LNMP应用平台
一、LNMP应用平台概述:
1.概述:LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Mysql是一个小型关系型数据库管理系统。PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
2.优势:稳定、高效、功能丰富、维护简易;
二、内部实现原理:
1.处理http请求流程:
第一步:浏览器发送http request请求到服务器(Nginx),服务器响应并处理web请求,将一些静态资源(CSS,图片,视频等)保存服务器上。
第二步:将php脚本通过接口传输协议(网关协议)PHP-FCGI(fast-cgi)传输给PHP-FPM(进程管理程序),PHP-FPM不做处理,然后PHP-FPM调用PHP解析器进程,PHP解析器解析php脚本信息。PHP解析器进程可以启动多个,进行并发执行。
第三步:将解析后的脚本返回到PHP-FPM,PHP-FPM再通过fast-cgi的形式将脚本信息传送给Nginx。
第四步:服务器再通过Http response的形式传送给浏览器。浏览器再进行解析与渲染然后进行呈现。
2.Nginx + FastCGI实现:
Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过Fast-CGI接口来调用;
wrapper:为了调用CGI程序,还需要一个Fast-CGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过Fast-CGI接口,wrapper接收到请求,然后Fork(派生)出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过Fast-CGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据(html页面或者图片)发送给客户端。这就是Nginx+Fast-CGI的整个运作过程。
- nginx与php-fpm连接的两种方式
在linux中,nginx服务器和php-fpm可以通过tcp socket和unix socket两种方式实现。
unix socket是一种终端,可以使同一台操作系统上的两个或多个进程进行数据通信。这种方式需要再nginx配置文件中填写php-fpm的pid文件位置,效率要比tcp socket高。
tcp socket的优点是可以跨服务器,当nginx和php-fpm不在同一台机器上时,只能使用这种方式。
windows系统只能使用tcp socket的通信方式
配置方法
tcp socket
tcp socket通信方式,需要在nginx配置文件中填写php-fpm运行的ip地址和端口号。
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
unix socket
unix socket通信方式,需要在nginx配置文件中填写php-fpm运行的pid文件地址。
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
}
php-fpm的运行端口号和socket文件的地址都是在php-fpm.conf中配置的。
php-fpm.conf文件在php安装文件的/etc目录下,
比如你的php安装在/opt/php目录,则应该是/opt/php/php-fpm.conf。
4.lamp与lnmp的区别:
Lamp:Apache一般是把PHP当做自己的一个模块来启动;
Lnmp:Ngnix则是把http请求变量转发给PHP进程,即PHP独立进程,与Nginx相独立;
三、案例:搭建LNMP并测试项目;
案例环境:
系统类型 |
IP地址 |
主机名 |
所需软件 |
Centos 6.5 |
192.168.100.150 |
www.linuxfan.cn |
nginx-1.6.2.tar.gz、 cmake-2.8.6.tar.gz、 mysql-5.5.22.tar.gz、 libmcrypt-2.5.8.tar.gz、 mhash-0.9.9.9.tar.gz、 mcrypt-2.6.8.tar.gz、 php-5.3.28.tar.gz、 SKYUC.v3.4.2.SOURCE.zip |
案例步骤:
- 安装并配置Nginx服务;
- 安装mysql数据库服务;
- 配置初始化mysql及其优化;
- 安装php服务程序;
- 优化调整php服务;
- 配置nginx服务使用php的FPM模块来调用php环境来处理php的页面请求:
- 配置Nginx支持php解析,编写服务控制脚本:
- 编辑测试页面;
- 客户端访问测试;
- 发布php项目:电影系统;
- 客户端访问测试;
- 安装并配置Nginx服务;
[[email protected] ~]# yum -y install pcre-devel zlib-devel popt-devel openssl-devel openssl
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
[[email protected] ~]# tar zxvf nginx-1.6.2.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/nginx-1.6.2/
[[email protected] nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install
[[email protected] nginx-1.6.2]# cd
[[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] ~]# nginx
[[email protected] ~]# netstat -utlpn |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5618/nginx
- 安装mysql数据库服务;
[[email protected] ~]# rpm -e mysql mysql-server --nodeps
[[email protected] ~]# yum -y install ncurses ncurses-devel
[[email protected] ~]# tar zxvf cmake-2.8.6.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/cmake-2.8.6/
[[email protected] ~]# ./configure
[[email protected] ~]# make &&make install
[[email protected] ~]# cd
[[email protected] ~]# tar zxvf mysql-5.5.22.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/mysql-5.5.22/
[[email protected] mysql-5.5.22]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc/
[[email protected] mysql-5.5.22]# make &&make install
- 配置初始化mysql及其优化;
[[email protected] mysql-5.5.22]# groupadd mysql
[[email protected] mysql-5.5.22]# useradd -M -s /sbin/nologin -g mysql mysql
[[email protected] mysql-5.5.22]# chown mysql:mysql /usr/local/mysql/ -R
[[email protected] mysql-5.5.22]# cp support-files/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[[email protected] mysql-5.5.22]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql-5.5.22]# chmod +x /etc/init.d/mysqld
[[email protected] mysql-5.5.22]# chkconfig --add mysqld
[[email protected] mysql-5.5.22]# echo "PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
[[email protected] mysql-5.5.22]# source /etc/profile
[[email protected] mysql-5.5.22]# cd /usr/local/mysql/scripts/
[[email protected] scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
OK
Filling help tables...
OK
[[email protected] scripts]# cd
[[email protected] ~]# /etc/init.d/mysqld start
Starting MySQL... [确定]
[[email protected] ~]# mysqladmin -uroot password 123123
[[email protected] ~]# mysql -uroot -p123123
mysql> quit;
- 安装php服务程序;
[[email protected] ~]# rpm -e php --nodeps
[[email protected] ~]# tar zxvf /root/libmcrypt-2.5.8.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/libmcrypt-2.5.8/
[[email protected] ~]# ./configure &&make &&make install
[[email protected] ~]# ln -s /usr/local/lib/libmcrypt.* /usr/lib64/
[[email protected] ~]# tar zxvf /root/mhash-0.9.9.9.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/mhash-0.9.9.9/
[[email protected] ~]# ./configure &&make &&make install
[[email protected] ~]# ln -s /usr/local/lib/libmhash* /usr/lib64/
[[email protected] ~]# tar zxvf /root/mcrypt-2.6.8.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/mcrypt-2.6.8/
[[email protected] ~]# ./configure &&make &&make install
[[email protected] ~]# yum -y install libxml2-devel gd zlib-devel libpng-devel libjpeg-devel
[[email protected] ~]# tar zxvf php-5.3.28.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/php-5.3.28/
[[email protected] php-5.3.28]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib
注解:
--prefix=/usr/local/php5 ##指定安装位置
--with-gd ##加载gd扩展库工具
--with-zlib ##加载压缩工具
--with-mysql=/usr/local/mysql ##加载mysql数据库的安装位置
--with-config-file-path=/usr/local/php5 ##加载php的配置文件存放位置
--enable-mbstring ##加载多字符的支持
--enable-fpm ##加载fpm管理程序
--with-jpeg-dir=/usr/lib ##加载识别图片格式
[[email protected] php-5. 3.28]# make &&make install
[[email protected] php-5. 3.28]# ls /usr/local/php5/
bin etc include lib php sbin var
- 优化调整php服务;
[[email protected] php-5. 3.28]# cp php.ini-development /usr/local/php5/php.ini
[[email protected] php-5. 3.28]# cd
[[email protected] ~]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[[email protected] ~]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
[[email protected] ~]# tar zxvf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz ##安装ZendGuardLoader模块
[[email protected] ~]# cp ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so /usr/local/php5/
[[email protected] ~]# vi /usr/local/php5/php.ini ##在最后位置添加以下内容:
[Zend Guard]
zend_extension=/usr/local/php5/ZendGuardLoader.so
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
注释:
zend_extension=/usr/local/php5/ZendGuardLoader.so ##加载模块位置
zend_loader.enable=1 ##开启该模块
zend_loader.disable_licensing=0 ##禁用许可证检查(提高性能)
zend_loader.obfuscation_level_support=3 ##让 Zend Guard Loader 支持混淆级别
- 配置nginx服务使用php的FPM模块来调用php环境来处理php的页面请求:
[[email protected] ~]# cd /usr/local/php5/etc/
[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf
[[email protected] etc]# vi php-fpm.conf ##新建管理cgi接口的fpm模块的配置文件
[global]
pid = run/php-fpm.pid
[www]
listen = 127.0.0.1:9000
user = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
:wq
解释:
[global] ##全局配置区域
pid = run/php-fpm.pid ##指定程序运行时的pid文件
[www] ##www网站区域
listen = 127.0.0.1:9000 ##fpm管理程序监听的地址和端口
user = nginx ##运行的用户和组
group = nginx
pm = dynamic ##指定fpm进程池的模式,动态模式
pm.max_children = 50 ##静态方式下开启的进程
pm.start_servers = 20 ##动态方式下启动时开始的进程数
pm.min_spare_servers = 5 ##动态最小进程数
pm.max_spare_servers = 35 ##动态最大进程数
[[email protected] ~]# /usr/local/sbin/php-fpm ##启动php-fpm
[[email protected] ~]# netstat -utpln |grep php
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5587/php-fpm
- 配置Nginx支持php解析,编写服务控制脚本:
[[email protected] ~]# vi /etc/init.d/lnmp
#!/bin/bash
# chkconfig: 35 95 30
# description: This script is for LNMP Management!
NGF=/usr/local/nginx/sbin/nginx
NGP=/usr/local/nginx/logs/nginx.pid
FPMF=/usr/local/php5/sbin/php-fpm
FPMP=/usr/local/php5/var/run/php-fpm.pid
case $1 in
start)
$NGF &&echo "nginx is starting! "
$FPMF && echo "php-fpm is starting! "
;;
stop)
kill -QUIT $(cat $NGP) &&echo "nginx is stoped! "
kill -QUIT $(cat $FPMP) &&echo "php-fpm is stoped! "
;;
restart)
$0 stop
$0 start
;;
reload)
kill -HUP $(cat $NGP)
kill -HUP $(cat $FPMP)
;;
status)
netstat -utpln |grep nginx &>/dev/null
if [ $? -eq 0 ]
then
echo "nginx is running! "
else
echo "nginx is not running! "
fi
netstat -upltn |grep php-fpm &>/dev/null
if [ $? -eq 0 ]
then
echo "php-fpm is runing! "
else
echo "php-fpm is not running! "
fi
;;
*)
echo "Usage $0 {start|stop|status|restart}"
exit 1
;;
esac
[[email protected] ~]# chmod +x /etc/init.d/lnmp
[[email protected] ~]# chkconfig --add lnmp
[[email protected] ~]# /etc/init.d/lnmp status
nginx is running!
php-fpm is runing!
[[email protected] conf]# vi /usr/local/nginx/conf/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 www.linuxfan.cn;
charset utf-8;
location / {
root /var/www/linuxfan/;
index index.html index.htm index.php;
}
location ~\.php { ##访问.php页面的配置段
root /var/www/linuxfan/; ##php网页文档根目录,html代表安装nginx的目录下的html目录
fastcgi_pass 127.0.0.1:9000; ##php-fpm的监听地址和端口号
fastcgi_index index.php; ##指定php首页文件
include fastcgi.conf; ##包括fastcgi.conf样本配置
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[[email protected] conf]# /etc/init.d/lnmp restart
- 编辑测试页面;
[[email protected] ~]# mkdir -p /var/www/linuxfan
[[email protected] ~]# vi /var/www/linuxfan/index.html
www.linuxfan.cn
[[email protected] ~]# vi /var/www/linuxfan/testa.php
<?php
phpinfo();
?>
[[email protected] ~]# vi /var/www/linuxfan/testm.php
<?php
$link=mysql_connect('localhost','root','123123');
if($link) echo "mysql ok!";
mysql_close();
?>
- 客户端访问测试;
http://www.linuxfan.cn
http://www.linuxfan.cn/testa.php
http://www.linuxfan.cn/testa.php
- 发布php项目:电影系统;
[[email protected] ~]# yum -y install unzip
[[email protected] ~]# unzip SKYUC.v3.4.2.SOURCE.zip
[[email protected] ~]# cd SKYUC.v3.4.2.SOURCE/
[[email protected] ~]# cp -rf wwwroot /var/www/linuxfan/skyuc
[[email protected] ~]# cd /var/www/linuxfan/skyuc
[[email protected] ~]# chown -R nginx:nginx admincp/ data/ templates/ upload/
[[email protected] ~]#mysql -uroot -p123123
> create database skyucdb;
> grant all on skyucdb.* to 'runskyuc'@'localhost' identified by '123123';
> quit;
- 客户端访问测试;
http://www.linuxfan.cn/skyuc/install/index.php ##安装该系统
http://www.linuxfan.cn/skyuc ##普通用户访问该系统
http://www.linuxfan.cn/skyuc/admincp ##后台管理界面,用户密码为安装时指定