L=linux 丨 N=nginx 丨 A=apache 丨 P=php 丨 T=tomcatphp
Nginx和Apache都是Web应用服务器html
相对来讲,Nginx处理静态数据快,Apache处理动态快mysql
Nginx对系统资源占用小,Apache资源占用高linux
Nginx用得是epoll模型(异步非阻塞IO),Apache用的是select模型(同步阻塞IO)nginx
大约在2010年之前,互联网公司最经常使用的经典Web服务环境组合就是LAMP(即Linux,Apache,MySQL,PHP),近几年随着Nginx Web服务的逐渐流行,
又出现了新的Web服务环境组合--LNMP或LEMP,其中LNMP为Linux,Nginx,MySQL,PHP等首字母的缩写,而LEMP中的E则表示Nginx,
它取自Nginx名字的发音(engine x)。如今,LNMP已经逐渐成为国内大中型互联网公司网站的主流组合环境,所以,咱们必须熟练掌握LNMP环境的搭建,优化及维护方法。c++
在深刻学习LNMP组合以前,有必要先来了解如下LNMP环境组合的基本原理,也就是它们之间究竟是怎样互相调度的?
在LNMP组合工做时,首先是用户经过浏览器输入域名请求Nginx Web服务,若是请求是静态资源,则由Nginx解析返回给用户;若是是动态请求(.php结尾),
那么Nginx就会把它经过FastCGI接口(生产经常使用方法)发送给PHP引擎服务(FastCGI进程php-fpm)进行解析,若是这个动态请求要读取数据库数据,
那么PHP就会继续向后请求MySQL数据库,以读取须要的数据,并最终经过Nginx服务把获取的数据返回给用户,
这就是LNMP环境的基本请求顺序流程。这个请求流程是企业使用LNMP环境的经常使用流程。sql
用户的请求必定是遵循HTTP协议的所以是经过浏览器过来的
数据库
(1)Nginx=http格式数据包,9http数据包特色格式很松散,所以解析的速度慢,可是数据包比fastcgi格式数据包小apache
(2)接口规则=快速接口,fastcgi数据包,这种规则的数据包,格式很是严谨,所以PHP解析的速度很是快,可是越严谨的数据包越大,因此fastcgi数据包大小要绝对大于http数据包vim
(3)软件的设计架构分为C/S和B/S,做为客户端Client的fastcgi和做为Server端的PHP-fpm,由于常常用,fastcgi_pass和Nginx合并,而PHP-fpm和PHP功能合并了
(4)nginx和apache其实同样,若是没有PHP支持,那么都处理不了所谓的静态请求,它们自身其实都只能处理静态,只能apache转发动态数据包的速度快,可是只是单个包速度快,apache并发低
(5)代码是绝对不往存储里放的,代码是不须要作备份的,保证安全的是图片和视频。
(6)PHP须要访问网页,需不须要挂存储,要看它什么功能,若是是读网页不须要挂存储。
http数据包特色:格式松散,所以解析速度慢,可是数据包比fastsgi格式数据包小
fastsgi数据包:格式严谨,所以解析速度快,可是严谨的数据包大,因此fastsgi格式数据包大于http数据包
全都部署在一台上 LNMP
全都不在一台上 N+P+M
只分离MySQL NP+M
CGI的全称为“通用网关接口”(Common Gateway Interface),为HTTP服务器与其余机器上的程序服务通讯交流的一种工具,CGI程序须运行在网络服务器上。
传统CGI接口方式的主要缺点是性能较差,由于每次HTTP服务器遇到动态程序时都须要从新启动解析器来执行解析,以后结果才会被返回给HTTP服务器。这在处理高并发访问时几乎是不可用的,所以就诞生了FastCGI。另外,传统的CGI接口方式安全性也不好,故而如今已经不多被使用了。
FastCGI是一个可伸缩的,高速地在HTTP服务器和动态脚本语言间通讯的接口(在Linux下,FastCGI接口即为socket,这个socket能够是文件socket,也能够是IP socket),主要优势是把动态语言和HTTP服务器分离出来。多数流行的HTTP服务器都支持FastCGI,包括Apache,Nginx和Lighttpd等。
同时,FastCGI也被许多脚本语言所支持,例如当前比较流程的脚本语言PHP。FastCGI接口采用的是C/S架构,它能够将HTTP服务器和脚本解析服务器分开,同时还能在脚本解析服务器上启动一个或多个脚原本解析守护进程。当HTTP服务器遇到动态程序时,能够将其直接交付给FastCGI进程来执行,而后将获得的结果返回给浏览器。这种方式可让HTTP服务器专注地处理静态请求,或者将动态脚本服务器的结果返回给客户端,这在很大程度上提升了整个应用系统的性能。
FastCGI的重要特色以下:
HTTP服务器和动态脚本语言间通讯的接口或工具。
可把动态语言解析和HTTP服务器分离开。
Nginx,Apache,Lighttpd,以及多数动态语言都支持FastCGI。
FastCGI接口方式采用C/S结构,分为客户端(HTTP服务器)和服务器端(动态语言解析服务器)
PHP动态语言服务器端能够启动多个FastCGI的守护进程(例如php-fpm(fcgi process mangement))
HTTP服务器经过(例如Nginx fastcgi_pass)FastCGI客户端和动态语言FastCGI服务器端通讯(例如php-fpm)
Nginx不支持对外部动态程序的直接调用或者解析,全部的外部程序(包括PHP)必须经过FastCGI接口来调用。FastCGI接口在Linux下是socket,为了调用CGI程序,还须要一个FastCGI的wrapper(能够理解为用于启动另外一个程序的程序),这个wrappper绑定在某个固定的socket上,如端口或文件socket。当Nginx将CGI请求发送给这个socket的时候,经过FastCGI接口,wrapper接收到请求,而后派生出一个新的线程,这个线程调用解释器或外部程序处理脚原本读取返回的数据;接着,wrapper再将返回的数据经过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运做过程。
yum install -y pcre-devel openssl-devel gcc gcc-c++ make automake
wget -q http://nginx.org/download/nginx-1.10.2.tar.gz
没有wget命令须要手动安装
yum -y install wget
useradd -s /sbin/nologin -M www
tar xf nginx-1.10.2.tar.gz -C /usr/src/
cd /usr/src/nginx-1.10.2
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin
cd /usr/local/nginx/conf
egrep -v "#|^$" nginx.conf.default > nginx.conf
由于安装PHP须要MySQL的安装环境,因此须要先安装MySQL,否则PHP编译会报错
如何没有安装MySQL须要把--with-mysql=/usr/local/mysql改为--with-mysql=mysqlnd
useradd -s /sbin/nologin -M mysql
连接:https://pan.baidu.com/s/1dzCH1GsOiMR3i_omOZliHA
提取码:u6h8
tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -s mysql-5.5.32-linux2.6-x86_64 mysql
cd /usr/local/mysql
/bin/cp support-files/my-small.cnf /etc/my.cnf
echo "192.168.200.129 LNMP" >> /etc/hosts
chown -R mysql.mysql /usr/local/mysql
yum -y install libaio
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/etc/init.d/mysqld start
ss -antup | grep 3306
chkconfig --add mysqld
chkconfig mysqld on
chkconfig --list mysqld
ln -s /usr/local/mysql/bin/* /usr/local/bin
which mysql
mysqladmin -uroot password '123321'
yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar xf libiconv-1.14.tar.gz -C /usr/src/
cd /usr/src/libiconv-1.14/
./configure --prefix=/usr/local/libiconv && make && make install
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install libmcrypt-devel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install mhash
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install mcrypt
tar xf php-5.3.28.tar.gz -C /usr/src/
cd /usr/src/php-5.3.28/
./configure --prefix=/usr/local/php5.3.28 --with-mysql=/usr/local/mysql --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=www --with-fpm-group=www --enable-ftp
最后的正确输出提示为:Thank you for using PHP.
--prefix=/usr/local/php5.2.28
表示指定PHP的安装路径为/usr/local/php5.3.28
--with-mysql=/usr/local/mysql
表示须要指定MySQL的安装路径,安装PHP须要的MySQL相关内容。固然,若是没有MySQL软件包,也能够不单独安装,这样的状况可以使用--with-mysql=mysqlnd替代--with-mysql=/usr/local/mysql,由于PHP软件里已经自带了链接MySQL的客户端工具。
--with-fpm-user=www
nginx表示指定PHP-FPM进程管理的用户为www,此处最好和Nginx服务用户统一
--with-fpm-group=www
表示指定PHP-FPM进程管理的组为www,此处最好与Nginx服务用户组统一。
--enable-fpm
表示激活PHP-FPM方式服务,即以FastCGIF方式运行PHP服务。
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64
touch ext/phar/phar.phar
make
Build complete.
Don't forget to run 'make test'.
make install
ln -s /usr/local/php5.3.28/ /usr/local/php
生产环境:php.ini-development 开发环境:php.ini-production
开发环境更多的是开启日志,调试信息,而生产环境都是关闭状态
cd /usr/src/php-5.3.28
cp php.ini-production /usr/local/php/lib/php.ini
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
关于php-fpm.conf,暂时可用默认的配置,先把服务搭好,之后再进行优化。
vim php-fpm.conf
listen = 127.0.0.1:9000 --->如要分离式部署,须要把本地IP改为远程IP
/usr/local/php/sbin/php-fpm
ss -antup | grep 9000
cd /usr/local/nginx
vim conf/nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.yunjisuan.com;
location / {
root html/blog; 网页目录
index index.html index.htm;
}
location ~ .*\.(php|php5)?$ { 动态网页
root html/blog;
fastcgi_pass 127.0.0.1:9000; 推到127.0.0.1:9000
fastcgi_index index.php; 动态网页首页
include fastcgi.conf; 引进fastcgi.conf模板
}
}
}
/usr/local/nginx/sbin/nginx -t
检查Nginx配置文件的语法/usr/local/nginx/sbin/nginx
ss -antup | grep 80
此步在生产环境很关键,如不提早检查语法,重启后发现语法错误会致使Nginx没法提供服务,,给用户访问体验带来很差的影响。
cd html
mkdir blog
echo "`
hostname -I`
blog.yunjisuan.com" > blog/index.html
vim /etc/hosts
192.168.200.70 LNMP blog.yunjisuan.com
curl blog.yunjisuan.com
echo "<?php phpinfo(); ?>" > blog/test_info.php 或者vim写入
C:\Windows\System32\drivers\etc --->windows映射文件位置
192.168.200.70 blog.yunjisuan.com
在浏览器输入blog.yunjisuan.com/test_info.php
cd blog
vim test_mysql.php
<?php
$link_id=mysql_connect('localhost','root','123321'); 链接mysql的信息交给一个变量,本地,帐号,密码
if($link_id){ 若是能链接上
echo "mysql successful by zz !\n"; 若是能链接上输出
}else{
echo mysql_error(); 若是链接不上显示错误页面
}
?>
若是没有PHP命令就是没作软连接
ln -s /usr/local/php/bin/* /usr/local/bin
php test_mysql.php
用PHP命令激活这个脚本
blog.yunjisuan.com/test_mysql.php
WordPress 是一套利用PHP语言和MySQL数据库开发的开源免费的blog(博客,网站)程序,用户能够在支持PHP环境和MySQL数据库的服务器上创建blog站点。它的功能很是强大,拥有众多插件,易于扩充功能。其安装和使用也都很是方便。目前WordPress已经成为搭建blog平台的主流,不少发布平台都是根据WordPress二次开发的,若是你也想像他们同样拥有本身的blog,可购买网上的域名及空间,而后搭建LNMP环境,部署WordPress程序后就能够轻松成就本身的梦想了。
WordPress是单用户我的博客与多用户博客是有区别的。
mysql -uroot -p123321 --->登录MySQL数据库
create database wordpress; --->建立一个名字为wordpress的数据库存放blog数据
grant all on wordpress.* to wordpress@'localhost' identified by '123321';
建立一个专用的WordPress blog管理用户
localhost只容许本机经过wordpress用户访问数据库flush privileges; --->刷新权限,使得建立用户生效
exit
vim /usr/local/nginx/conf/nginx.conf
须要在index后补一个首页文件index.php
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
www.wordpress.org下载博客程序
连接:https://pan.baidu.com/s/1hGyd1xFuL0oPBsFeROuUYQ
提取码:tmgk
tar xf wordpress-4.9.8.tar.gz
cd /usr/local/nginx/html/blog
rm -rf *
mv ~/wordpress/* .
chown -R www.www /usr/local/nginx/html/blog
浏览器输入blog.yunjisuan.com,进入界面安装,选择中文
数据库名 --->前面建立的数据库 用户 --->专用的用户名 密码 --->数据库密码 数据库主机 --->容许访问的数据库主机,这里是LNMP一体的环境因此localhost最安全 表前缀 --->为了安全改一下,防止黑客猜你的表名
站点标题 --->博客主页的名字
用户名 --->登录博客的用户
密码 --->登录博客的密码
您的电子邮件 --->有问题发送的邮箱
对搜索引擎的可见性 --->测试环境须要取消,有的时候会打不开,会致使浏览速度很慢
安装完WrodPress后直接登录便可,进入的是后台页面
网页输入域名测试
实现此功能时,首先要在Settings-固定连接-自定义结构-添加代码-保存更改
/archives/%post_id%.html
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name blog.yunjisuan.com;
location / {
root html/blog;
index index.php index.html index.htm;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
location ~ .*\.(php|php5)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
}
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
blog.yunjisuan.com
建立文章,点击文章能够看见:
上传图片须要作2个操做,图片放到存储里,放在存储的位置须要写到数据库里
vim /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 xxx.yunjisuan.com;
location / {
root html/xxx;
index index.html index.htm;
}
}
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
cd /usr/local/nginx/html
mkdir xxx
chown -R www.www xxx/
vim xxx/index.html
在把须要用的图片,导入虚拟机里
<img src="http://192.168.200.132/666.jpg" />
加入本地映射 vim /etc/hosts
windows映射就不演示了,本身添加一下就好
网站测试
cd /usr/local/nginx/logs
>access.log
浏览器单独发起请求在去拿图片,图片都是后拿的