LNMP==Linux+Nginx+Mysql+PHP
nginx:省资源,省cpu,因此在高并发时可以处理更多的请求,高端能达到3万到5万的并发量。
nginx和LAMP不一样的是,提供web服务的是Nginx
而且php是做为一个独立服务存在的,这个服务叫作php-fpm
Nginx直接处理静态请求,动态请求会转发给php-fpm
在当前互联网环境下,通常高端的服务前端都采用nginx做为web前端,而更多的都是采用lnmp架构,真正的后端服务器才会采用apache.
为何这么作,要取决于nginx和apache二者之间的优缺性.:
nginx与apache相比有如下优点:在性能上,nginx占用不多的系统资源,能支持更多的并发连接,达到更高的访问率;在功能上,Nginx是优秀的代理服务器和负载均衡器;在安装配置上,简单灵活。
nginx模块基本都是静态编译,同时对Fast-CGI支持比较好.在处理连接上,nginx支持epoll,并且体积小通常只有几百K。
Nginx的优势有如下几点:
1.做为Web服务器,nginx处理静态文件、索引文件以及自动索引效率很是高。
2.做为代理服务器,Nginx能够实现无缓存的反向代理加速,提升网站运行速度。
3.做为负载均衡服务器,Nginx既能够在内部直接支持Rails和PHP,也能够支持HTTP代理服务器,对外进行服务。同时支持简单的容错和利用算法进行负载均衡。
4.在性能方面,Nginx是专门为性能优化而开发的,在实现上很是注重效率。它采用内核Poll模型,能够支持更多的并发链接,最大能够支持对50 000个并发链接数的响应,并且占用很低的内存资源。
5.在稳定性方面,Nginx采起了分阶段资源分配技术,使得对CPU与内存的占用率很是低。Nginx官方表示Nginx保持10 000个没有活动的链接,这些链接只占2.5M内存,所以,相似DOS这样的***对Nginx来讲基本上是没有任何做用的。
6.在高可用性方面,Nginx支持热部署,启动速度特别迅速,所以能够在不间断服务的状况下,对软件版本或者配置进行升级,即便运行数月也无需从新启动,几乎能够作到7×24小时的不间断运行。javascript
安装mysql的配置,其实在作LAMP的时候已经作过了,如出一辙的编译配置。http://www.javashuo.com/article/p-pxxlmzti-e.html (有须要的能够看看这篇)php
和LAMP安装PHP方法有差异,须要开启php-fpm服务css
[root@chy php-5.6.30]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz [root@chy php-5.6.30]# tar -zxvf php-5.6.30.tar.gz [root@chy01 php-5.6.30]# useradd -s /sbin/nologin php-fpm (增长php-fpm用户) [root@chy01 php-5.6.30]# ./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 (编译参数如上,以下是详解参数 ./configure --prefix=/usr/local/php-fpm(php路径) --with-config-file-path=/usr/local/php-fpm/etc(php配置文件路径) --enable-fpm(启动服务必须的参数) --with-fpm-user=php-fpm(指定php的用户) --with-fpm-group=php-fpm(指定phpd 的组) --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 报错信息1:configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/ 解决方法:[root@chy01 php-5.6.30]# yum install -y libcurl-devel 而后再次编译时:编译成功 [root@chy01 php-5.6.30]# echo $? 0 [root@chy01 php-5.6.30]# make && make install (而后make 与make install) [root@chy01 php-5.6.30]# echo $? 0 (make 成功)
php-fpm简单介绍html
[root@chy01 php-5.6.30]# ls /usr/local/php-fpm/sbin/ php-fpm 其中(php-fpm是php-fpm的启动文件) [root@chy01 php-5.6.30]# ls /usr/local/php-fpm/var/log/ (查看日志的启动文件) [root@chy01 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -i [root@chy01 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -m (查看php的模块) [root@chy01 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t [10-Aug-2017 01:41:12] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2) [10-Aug-2017 01:41:12] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf' [10-Aug-2017 01:41:12] ERROR: FPM initialization failed (-t测试php-fpm的配置的语法是否正确) [root@chy01 php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini (cp php的线上的配置文件到/usr/local/php-fpm/etc/php.ini下去) [root@chy01 php-5.6.30]# cd /usr/local/php-fpm/etc/ [root@chy01 etc]# ls pear.conf php-fpm.conf.default php.ini [root@chy01 etc]# vim php-fpm.conf (建立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.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](定义全局参数) pid = /usr/local/php-fpm/var/run/php-fpm.pid (定义pid) error_log = /usr/local/php-fpm/var/log/php-fpm.log [www] (服务的名称) listen = /tmp/php-fcgi.sock(监听的sock) #listen =127.0.0.1:9000 (这个是监听的ip地址) listen.mode = 666(这个是若是监听的是sock此项才会生效,则不会生效) 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 [root@chy01 etc]# cd /usr/local/src/php-5.6.30 (进入到php的源码包里) [root@chy01 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm (cp配置文件到/etc/init.d/php-fpm 下) [root@chy01 php-5.6.30]# chmod 755 /etc/init.d/php-fpm (更改配置权限为755) [root@chy01 php-5.6.30]# chkconfig --add php-fpm [root@chy01 php-5.6.30]# chkconfig --list 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 若是您想列出 systemd 服务,请执行 'systemctl list-unit-files'。 欲查看对特定 target 启用的服务请执行 'systemctl list-dependencies [target]'。 apache2 0:关 1:关 2:关 3:开 4:关 5:开 6:关 mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关 network 0:关 1:关 2:开 3:开 4:开 5:开 6:关 php-fpm 0:关 1:关 2:开 3:开 4:开 5:开 6:关 (增长到开机启动) [root@chy01 php-5.6.30]# /etc/init.d/php-fpm start Starting php-fpm done (启动服务) [root@chy01 php-5.6.30]# ps aux |grep php-fpm root 125159 0.0 0.3 226640 4948 ? Ss 02:13 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf) php-fpm 125160 0.0 0.3 226640 4712 ? S 02:13 0:00 php-fpm: pool www php-fpm 125161 0.0 0.3 226640 4712 ? S 02:13 0:00 php-fpm: pool www php-fpm 125162 0.0 0.3 226640 4712 ? S 02:13 0:00 php-fpm: pool www php-fpm 125163 0.0 0.3 226640 4712 ? S 02:13 0:00 php-fpm: pool www php-fpm 125164 0.0 0.3 226640 4716 ? S 02:13 0:00 php-fpm: pool www php-fpm 125165 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125166 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125167 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125168 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125169 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125170 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125171 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125172 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125173 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125174 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125175 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125176 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125177 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125178 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www php-fpm 125179 0.0 0.3 226640 4720 ? S 02:13 0:00 php-fpm: pool www root 125220 0.0 0.0 112664 976 pts/0 R+ 02:15 0:00 grep --color=auto php-fpm [root@chy01 php-5.6.30]# ls -l /tmp/php-fcgi.sock srw-rw-rw- 1 root root 0 8月 10 02:13 /tmp/php-fcgi.sock
nginx官网http://nginx.org
Nginx应用场景:web服务、反向代理、负载均衡
Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都同样,和Nginx的最大区别在于Tenging增长了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并前端
[root@chy01 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz (下载nginx安装包) [root@chy01 src]# tar zxvf nginx-1.12.1.tar.gz (解压) [root@chy01 src]./configure --prefix=/usr/local/nginx [root@chy01 src]# cd nginx-1.12.1 (进入到此目录下开始编译) [root@chy01 nginx-1.12.1]# echo $? 0 [root@chy01 nginx-1.12.1]# make && make install [root@chy01 nginx-1.12.1]# echo $? 0 (编译成功) [root@chy01 nginx-1.12.1]# /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 (-t检查配置文件语法是否错误) [root@chy01 nginx-1.12.1]# 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@chy01 nginx-1.12.1]# chmod 755 /etc/init.d/nginx (增长nginx权限) [root@chy01 nginx-1.12.1]# chkconfig --add nginx (将nginx设置为开机自启) [root@chy01 nginx-1.12.1]# cd /usr/local/nginx/conf/ (配置nginx的配置文件,须要先下一个模板) [root@chy01 conf]# mv nginx.conf nginx.conf.1 (因nginx已经有一个配置文件,须要将此配置文件cp而且从新更改一个本身的配置文件) [root@chy01 conf]# vim nginx.conf (配置nginx的配置文件) 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; (定义的nginx的用户) worker_processes 2;(定义子进程有几个) error_log /usr/local/nginx/logs/nginx_error.log crit; (定义错误日志) pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200;(定义nginx最多能打开多少个文件) 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$ (配置解析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; } } } [root@chy01 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 (查看nginx配置文件是否有错) [root@chy01 conf]# /etc/init.d/nginx start(这里报了一个错误) Starting nginx (via systemctl): Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. [失败] (根据提示查错误,[root@chy01 conf]# systemctl status nginx.service ● nginx.service - SYSV: http service. Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled) Active: failed (Result: exit-code) since 五 2017-08-11 00:23:23 CST; 36s ago Docs: man:systemd-sysv-generator(8) Process: 5613 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE) 8月 11 00:23:21 chy01 nginx[5613]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 8月 11 00:23:21 chy01 nginx[5613]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 8月 11 00:23:22 chy01 nginx[5613]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 8月 11 00:23:22 chy01 nginx[5613]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 8月 11 00:23:23 chy01 nginx[5613]: nginx: [emerg] still could not bind() 8月 11 00:23:23 chy01 nginx[5613]: [失败] 8月 11 00:23:23 chy01 systemd[1]: nginx.service: control process exited, code=exited status=1 8月 11 00:23:23 chy01 systemd[1]: Failed to start SYSV: http service.. 8月 11 00:23:23 chy01 systemd[1]: Unit nginx.service entered failed state. 8月 11 00:23:23 chy01 systemd[1]: nginx.service failed. (能够看到提示错误nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 提示的意思为80端口被使用) [root@chy01 conf]# /usr/local/apache2.4/bin/apachectl stop (将http关闭) [root@chy01 conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 肯定 ] (在进行启动,启动成功) [root@chy01 conf]# ps aux |grep nginx root 5655 0.0 0.0 20484 628 ? Ss 00:24 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 5656 0.0 0.2 22928 3216 ? S 00:24 0:00 nginx: worker process nobody 5657 0.0 0.2 22928 3216 ? S 00:24 0:00 nginx: worker process root 5672 0.0 0.0 112664 976 pts/0 S+ 00:26 0:00 grep --color=auto nginx (能够看到子进程为root,主进程为nobody) [root@chy01 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> (nginx 进行测试) [root@chy01 conf]# vim /usr/local/nginx/html/1.php (建立一个php的测试文件 <?php echo "this is nginx test page"; [root@chy01 conf]# curl localhost/1.php this is nginx test page[root@chy01 conf]# (如上解析成功)