上一课学习了LAMP架构,如今流行LNMP架构;他们的区别就是提供web服务为apache和nginx,目前nginx发展迅速,取代apache指日可待。javascript
- LAMP=linux+apache+mysql+php
- LNMP=linux+nginx+mysql+php
当咱们讲LAMP的时候PHP是做为一个模块在Apache中,可是在LNMP中,PHP是一个服务,当用户请求的时候nginx会把它交给php 而后对mysql进行交互!像这种静态的,例如图片或者html,nginx会直接处理,从而加快访问速度! 谈到速度,其实若是一个普通的站点你是看不到什么效果的,可是若是要是访问一个纯静态站点,nginx就能体现出它的优点!nginx的另一个优点得益于它的高并发支持!php
和LAMP不一样的是,提供web服务的是Nginx 而且php是做为一个独立服务存在的,这个服务叫作php-fpm Nginx直接处理静态请求,动态请求会转发给php-fpmcss
不管LAMP和LNMP架构中,M表明MySQL,都是独立存在于架构中;所以LNMP的MySQL安装步骤和LAMP同样的;html
详细查看:Linux 第17课 3、MySQL安装java
这里的PHP安装和LAMP安装PHP方法有差异,须要开启php-fpm服务node
首先把以前编译过的php配置删除;删除后就至关于php包刚解压的时候;mysql
[root@ying01 ~]# cd /usr/local/src/ [root@ying01 src]# ls apr-1.6.3 nginx-1.4.7 apr-1.6.3.tar.gz nginx-1.4.7.tar.gz apr-util-1.6.1 php-5.6.32 apr-util-1.6.1.tar.bz2 php-5.6.32.tar.bz2 httpd-2.4.33 php-7.1.6 httpd-2.4.33.tar.gz php-7.1.6.tar.bz2 httpd-2.4.33.tar.gz.1 phpredis-develop mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz phpredis-develop.zip mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz [root@ying01 src]# cd php-5.6.32/ [root@ying01 php-5.6.32]# make clean find . -name \*.gcno -o -name \*.gcda | xargs rm -f find . -name \*.lo -o -name \*.o | xargs rm -f find . -name \*.la -o -name \*.a | xargs rm -f find . -name \*.so | xargs rm -f find . -name .libs -a -type d|xargs rm -rf rm -f libphp5.la sapi/cli/php sapi/cgi/php-cgi libphp5.la modules/* libs/*
此时的php-5.6.32目录,如同刚解压的时候时候;此时经过**./configure**进行定制相关功能,使其生成makefile;linux
[root@ying01 php-5.6.32]# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl
相关名称释义:nginx
- --prefix=指定安装位置
- --with-config-file-path=指定配置文件目录
- --enable-fpm=启动fpm服务
- --with-fpm-user=指定用户
- --with-fpm-group=指定用户组
- --with-mysql=指定mysql的路径
自动检查过程当中,出现如下报错:web
configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
需安装devel安装包,安装完从新加载配置文件;
[root@ying01 php-5.6.32]# yum install libcurl-devel
开始编译
[root@ying01 php-5.6.32]# make
编译完成后,开始安装
[root@ying01 php-5.6.32]# make install
此时会在/usr/local目录下,生成了php-fpm新目录;
[root@ying01 php-5.6.32]# ls /usr/local/php-fpm/ //比以前的php多了sbin和var目录 bin etc include lib php sbin var [root@ying01 php-5.6.32]# ls /usr/local/php bin etc include lib php [root@ying01 php-5.6.32]# ls /usr/local/php-fpm/sbin/ //sbin下有php-fpm php-fpm [root@ying01 php-5.6.32]# ls /usr/local/php-fpm/var/ //var下有log日志目录和PID目录 log run
安装下来以后咱们发现其实比以前安装PHP要多了两个目录 sbin 和 var
- sbin:实际上就是启动php-fpm服务的目录
- var:实际上就是存放PHP日志的,固然这个咱们能够指定的
- log: php日志目录;
- run:进程PID目录
php-fpm 选项参数用法:-i -m -t
[root@ying01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -i //查看PHP信息 [root@ying01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -m //查看PHP模块 [root@ying01 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t //测试PHP配置语法
php解压目录中的php.ini-production文件复制到php-fpm/etc/目录中,而且重命名为php.ini
[root@ying01 php-5.6.32]# ls /usr/local/php-fpm/etc/ pear.conf php-fpm.conf.default [root@ying01 php-5.6.32]# cp php.ini-production /usr/local/php-fpm/etc/php.ini [root@ying01 php-5.6.32]# cd /usr/local/php-fpm/etc/ [root@ying01 etc]# ls pear.conf php-fpm.conf.default php.ini //复制成功
在php-fpm/etc/下有默认的php-fpm.conf.default配置文件,如今咱们须要从新复制一份,方便试验操做;
[root@ying01 etc]# vi php-fpm.conf //新建立 ....... 如下为配置文件内容 [global] pid = /usr/local/php-fpm/var/run/php-fpm.pid error_log = /usr/local/php-fpm/var/log/php-fpm.log [www] listen = /tmp/php-fcgi.sock #listen =127.0.0.1:9000 listen.mode = 666 user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024 ......
相关名词释义
- [global]=定义全局参数
- [www]=自定义模块
- listen = 监听的地址
- 或者可使用这样的方式↓
- listen = 127.0.0.1:9000 (port默认为9000,也能够更改)
- listen.mode = 666 用来定义listen = /tmp/php-fcgi.sock的权限,只有这个sock打开的状况下才生效!
把解压目录下的脚本sapi/fpm/init.d.php-fpm 复制到/etc/init.d下
[root@ying01 etc]# cd /usr/local/src/php-5.6.32/ [root@ying01 php-5.6.32]# ls sapi/ aolserver apache2handler cgi embed litespeed phpdbg roxen tux apache apache_hooks cli fpm milter phttpd tests webjames apache2filter caudium continuity isapi nsapi pi3web thttpd [root@ying01 php-5.6.32]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
而后给予755权限,增长开机服务等操做
[root@ying01 php-5.6.32]# chmod 755 /etc/init.d/php-fpm //给予755权限 [root@ying01 php-5.6.32]# chkconfig --add php-fpm //增长服务 [root@ying01 php-5.6.32]# chkconfig php-fpm on [root@ying01 php-5.6.32]# service php-fpm start //开启php-fpm服务 Starting php-fpm done
查看php-fpm启动信息
[root@ying01 php-5.6.32]# ps aux |grep php-fpm root 60635 0.0 0.2 123652 4940 ? Ss 16:10 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf) php-fpm 60636 0.0 0.2 123652 4704 ? S 16:10 0:00 php-fpm: pool www php-fpm 60637 0.0 0.2 123652 4704 ? S 16:10 0:00 php-fpm: pool www php-fpm 60638 0.0 0.2 123652 4704 ? S 16:10 0:00 php-fpm: pool www php-fpm 60639 0.0 0.2 123652 4704 ? S 16:10 0:00 php-fpm: pool www php-fpm 60640 0.0 0.2 123652 4708 ? S 16:10 0:00 php-fpm: pool www php-fpm 60641 0.0 0.2 123652 4708 ? S 16:10 0:00 php-fpm: pool www php-fpm 60642 0.0 0.2 123652 4708 ? S 16:10 0:00 php-fpm: pool www php-fpm 60643 0.0 0.2 123652 4708 ? S 16:10 0:00 php-fpm: pool www php-fpm 60644 0.0 0.2 123652 4708 ? S 16:10 0:00 php-fpm: pool www php-fpm 60645 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60646 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60647 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60648 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60649 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60650 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60651 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60652 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60653 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60654 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www php-fpm 60655 0.0 0.2 123652 4712 ? S 16:10 0:00 php-fpm: pool www root 60663 0.0 0.0 112724 984 pts/0 S+ 16:11 0:00 grep --color=auto php-fpm [root@ying01 php-5.6.32]# ls -l /tmp/php-fcgi.sock //sock的权限符合配置赋予的权限666 srw-rw-rw- 1 root root 0 7月 3 16:10 /tmp/php-fcgi.sock
由于nginx处理静态文件的能力要比apache好不少,因此不少企业在建站的时候通常都是用java写的,而后会选择tomcat,可是tomcat处理静态文件的能力不是太好就会叠加选择nginx。
nginx特色:
- 体积小
- 处理能力强
- 并发高
- 可扩展性好
Nginx应用场景:web服务、反向代理、负载均衡
Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都同样,和Nginx的最大区别在于Tenging增长了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并 Nginx核心+lua(开发语言)相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928
下载配置安装Nginx,并解压
[root@ying01 php-5.6.32]# cd /usr/local/src/ [root@ying01 src]# wget http://nginx.org/download/nginx-1.4.7.tar.gz --2018-07-03 16:51:10-- http://nginx.org/download/nginx-1.4.7.tar.gz 正在解析主机 nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2606:7100:1:69::3f, ... 正在链接 nginx.org (nginx.org)|95.211.80.227|:80... 已链接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:769153 (751K) [application/octet-stream] 正在保存至: “nginx-1.4.7.tar.gz” 100%[===============================================>] 769,153 34.6KB/s 用时 11s 2018-07-01 16:51:21 (68.8 KB/s) - 已保存 “nginx-1.4.7.tar.gz” [769153/769153]) [root@ying01 src]# ls apr-1.6.3 mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz apr-1.6.3.tar.gz nginx-1.4.7.tar.gz apr-util-1.6.1 php-5.6.32 apr-util-1.6.1.tar.bz2 php-5.6.32.tar.bz2 httpd-2.4.33 php-7.1.6 httpd-2.4.33.tar.gz php-7.1.6.tar.bz2 httpd-2.4.33.tar.gz.1 phpredis-develop mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz phpredis-develop.zip [root@ying01 src]# tar zxf nginx-1.4.7.tar.gz //解压
进入解压目录,进行./configure定制服务,使其生成makefile文件;
[root@ying01 src]# cd nginx-1.4.7/ [root@ying01 nginx-1.4.7]# ./configure --prefix=/usr/local/nginx
编译生成的makefile文件
[root@ying01 nginx-1.4.7]# make
开始安装
[root@ying01 nginx-1.4.7]# make install
打开各层目录,查看相关子目录
[root@ying01 nginx-1.4.7]# ls /usr/local/nginx/ conf html logs sbin [root@ying01 nginx-1.4.7]# ls /usr/local/nginx/conf/ fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default [root@ying01 nginx-1.4.7]# ls /usr/local/nginx/html/ 50x.html index.html [root@ying01 nginx-1.4.7]# ls /usr/local/nginx/logs/ [root@ying01 nginx-1.4.7]# ls /usr/local/nginx/sbin/ nginx [root@ying01 nginx-1.4.7]# ls /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx
/usr/local/nginx目录下的子目录:
子目录 释义 conf nginx配置文件 html 主页样例文件 logs 站点日志 sbin 核心进程文件
nginx -t 选项: 测试配置语法错误
[root@ying01 nginx-1.4.7]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
在init.d目录下,新建nginx脚本文件
[root@ying01 nginx-1.4.7]# vim /etc/init.d/nginx 如下为脚本内容..... #!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL
而后增长权限,以及启动服务;
[root@ying01 nginx-1.4.7]# chmod 755 /etc/init.d/nginx [root@ying01 nginx-1.4.7]# chkconfig --add nginx [root@ying01 nginx-1.4.7]# chkconfig nginx on
进入nginx/conf/目录下,把默认的配置文件做为备份;
[root@ying01 nginx-1.4.7]# cd /usr/local/nginx/conf/ [root@ying01 conf]# ls fastcgi.conf koi-utf nginx.conf uwsgi_params fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default fastcgi_params mime.types scgi_params win-utf fastcgi_params.default mime.types.default scgi_params.default [root@ying01 conf]# mv nginx.conf nginx.conf.1 //把原配置文件做为备份 [root@ying01 conf]# ls fastcgi.conf koi-utf nginx.conf.1 uwsgi_params fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default fastcgi_params mime.types scgi_params win-utf fastcgi_params.default mime.types.default scgi_params.default
新建nginx.conf配置文件,并按下面写入内容
[root@ying01 conf]# vim nginx.conf 如下为配置内容..... user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
相关名词,释义
- user nobody nobody; 运行服务的用户是谁
- worker_processes 2;定义子进程的数量
- worker_rlimit_nofile
- 51200;最多能够打开多少个文件
- worker_connections 6000;容许最大的链接数
- server; 下面对应的就是虚拟主机配置
- server_name localhost;定义网站的域名
- root /usr/local/nginx/html;定义网站的根目录
- location ~ .php$;配置解析PHP
- fastcgi_pass unix:/tmp/php-fcgi.sock;监听端口或者监听socket,经过此命令去执行
- fastcgi_pass 127.0.0.1:9000;(或者携程这种方式,服务器IP地址+端口)
[root@ying01 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@ying01 conf]# /etc/init.d/nginx start //开启服务 Starting nginx (via systemctl): [ 肯定 ] [root@ying01 conf]# ps aux |grep nginx //查看相关服务 root 63447 0.0 0.0 24844 784 ? Ss 17:20 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 63448 0.0 0.1 27148 3356 ? S 17:20 0:00 nginx: worker process nobody 63449 0.0 0.1 27148 3356 ? S 17:20 0:00 nginx: worker process root 63451 0.0 0.0 112720 980 pts/0 S+ 17:20 0:00 grep --color=auto nginx [root@ying01 conf]#
[root@ying01 conf]# curl localhost <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
[root@ying01 conf]# vim /usr/local/nginx/html/1.php //新建1.php页面,写入如下内容 如下为1.php内容... <?php echo "hello world";
测试解析PHP
[root@ying01 conf]# curl localhost/1.php //进行测试,成功解析 hello world[root@ying01 conf]#