LNMP LNMP环境搭建 搭建三个小项目 ab工具测试

LNMP

什么是LNMP?

LNMP是一套技术的组合,L=Linux、N=Nginx、M~=MySQL、P~=PHP、(ES、redis、kafka、zookeeper....

LNMP工作方式

首先Nginx服务是不能处理动态请求,那么当用户发起动态请求时, Nginx又是如何进行处理的。
	静态请求:请求静态文件或者html页面,服务器上存在的html文件
		静态文件:上传时什么样子,访问时还是什么样子;
	动态请求:请求的是动态内容,带参数的请求
			动态页面不存在于服务器上,他可能是取数据库或者redis等地方取值拼凑成的页面
	
当用户发起http请求,请求会被Nginx处理,如果是静态资源请求Nginx则直接返回,如果是动态请求Nginx则通过fastcgi协议转交给后端的PHP程序处理

访问流程

1.浏览器发起请求,请求到达nginx;
2.nginx先判断请求是动态还是静态;
	#静态请求
	location / {
		root /code;
		index index.html;
	}
	
	#动态请求
	location ~* \.php$ {
		fastcgi_pass 127.0.0.1:9000;
	}
3.如果是静态请求,nginx直接返回
4.如果是动态请求,nginx会通过fastcgi协议去找php-fpm管理进程;
5.php-fpm管理进程会去调用或者下发工作给wrapper工作进程;
6.wrapper工作进程判断php内容是否可以直接返回内容;
7.如果只是php内容,wrapper工作进程直接解析,并返回结果;
8.如果还需要访问数据库,则wrapper会去请求数据库获取数据,再返回。
9.最后数据由, 数据库mysql->wrapper->php-fpm->nginx->http->浏览器。

在这里插入图片描述

LNMP环境搭建

1)安装nginx

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true


[root@web1 ~]# yum clean all
[root@web1 ~]# yum makecache
[root@web1 ~]# yum install nginx -y

2)配置nginx

[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;

3)创建用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M

4)启动nginx,并加入开机自启

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

5)验证启动

[root@web01 ~]# ps -ef | grep nginx
root      23769      1  0 11:26 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www       23770  23769  0 11:26 ?        00:00:00 nginx: worker process
root      23816   8453  0 11:27 pts/1    00:00:00 grep --color=auto nginx

2.安装php

1)配置php第三方源

[root@web01 ~]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

2)卸载已安装php

[root@web01 ~]# yum remove php-mysql-5.4 php php-fpm php-common

3)安装php 7.1版本

[root@web01 ~]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd  php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

4)配置php

[root@web01 ~]# vim /etc/php-fpm.d/www.conf
user = www
group = www

5)启动php

[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

6)验证启动

[root@web01 ~]# ps -ef | grep php
root      24062      1  0 11:45 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
www       24063  24062  0 11:45 ?        00:00:00 php-fpm: pool www
www       24064  24062  0 11:45 ?        00:00:00 php-fpm: pool www
www       24065  24062  0 11:45 ?        00:00:00 php-fpm: pool www
www       24066  24062  0 11:45 ?        00:00:00 php-fpm: pool www
www       24067  24062  0 11:45 ?        00:00:00 php-fpm: pool www
root      24089   8453  0 11:46 pts/1    00:00:00 grep --color=auto php
[root@web01 ~]#

3.安装mariadb

1)安装

[root@web01 ~]# yum install -y mariadb-server

2)启动

[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

3)验证

[root@web01 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]>

5.搭建wordpress

1) 创建站点目录上传包
[root@web01 ~]# mkdir /code
[root@web01 ~]# chown -R www.www /code/
[root@web01 ~]# cd /code/
[root@web01 code]# rz wordpress-5.0.3-zh_CN.tar.gz

 wget https://wordpress.org/latest.tar.gz
2)解压代码包
[root@web01 code]# tar xf wordpress-5.0.3-zh_CN.tar.gz
3)授权目录
[root@web01 code]# chown -R www.www /code/
4)配置nginx
[root@web01 conf.d]# vim wordpress.conf
server {
    listen 80;
    server_name blog.linux.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
        root /code/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

[root@web01 conf.d]# systemctl restart nginx
5)创建数据库
[root@web01 conf.d]# mysql -uroot -pLin123.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.01 sec)

MariaDB [(none)]>

6.搭建知乎 wecenter

1)上传代码包
[root@web01 code]# rz WeCenter_3-2-1.zip
[root@web01 code]# unzip WeCenter_3-2-1.zip
[root@web01 code]# mv WeCenter_3-2-1 wecenter
[root@web01 code]# chown -R www.www ./*
2)配置nginx
[root@web01 conf.d]# vim zh.conf 

server {
    listen 80;
    server_name zh.linux.com;

    location / {
        root /code/wecenter;
        index index.php;
    }

    location ~* \.php$ {
        root /code/wecenter;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

[root@web01 conf.d]# systemctl restart nginx
3)创建数据库
[root@web01 conf.d]# mysql -uroot -pLin123.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 144
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database zh;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
| zh                 |
+--------------------+
6 rows in set (0.01 sec)

MariaDB [(none)]>

7.搭建edusoho

1)上传代码包
[root@web01 code]# rz edusoho-8.3.36.tar.gz
2)解压代码包
[root@web01 code]# tar xf edusoho-8.3.36.tar.gz
[root@web01 code]# chown -R www.www edusoho
3)配置nginx
[root@web03 ~]# cat /etc/nginx/conf.d/edu.conf
server {
        listen 80;
        server_name edu.linux.com;
        root /code/edusoho/web;
        client_max_body_size 200m;

        location / {
                index app.php;
                try_files $uri @rewriteapp;
        }
        location @rewriteapp {
                rewrite ^(.*)$ /app.php/$1 last;
        }

        location ~ ^/udisk {
                internal;
                root /code/edusoho/app/data/;
        }

        location ~ ^/(app|app_dev)\.php(/|$) {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param  HTTPS              off;
                fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
                fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/app/data/udisk;
                fastcgi_buffer_size 128k;
                fastcgi_buffers 8 128k;
        }

        # 配置设置图片格式文件
        location ~* \.(jpg|jpeg|gif|png|ico|swf)$ {
                # 过期时间为3年
                expires 3y;
                # 关闭日志记录
                access_log off;
                # 关闭gzip压缩,减少CPU消耗,因为图片的压缩率不高。
                gzip off;
        }
        # 配置css/js文件
        location ~* \.(css|js)$ {
                access_log off;
                expires 3y;
        }
        # 禁止用户上传目录下所有.php文件的访问,提高安全性
        location ~ ^/files/.*\.(php|php5)$ {
                deny all;
        }

        # 以下配置允许运行.php的程序,方便于其他第三方系统的集成。
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param  HTTPS              off;
        }
}

[root@web01 code]# systemctl restart nginx

优化

#报错是413,原因是文件过大
#解决:
[root@web01 code]# vim /etc/nginx/nginx.conf
http{
	... ...
	client_max_body_size 100m;
	... ...
}

#报错为405,因为nginx解析不了php文件,只能报错

ab工具测试


#安装ab测试工具

[root@web02 conf.d]# yum install  httpd-tools   -y   #ab测试工具参数详解

#检查ab测试工具是否安装成功


[root@web02 conf.d]#   ab -V       #安装成功后状态

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/



#ab测试工具参数详解

-n     ----------requests,用于指定压力测试总共的执行次数

-c     ----------concurrency,用于指定的并发数

-t      ----------timelimit,等待响应的最大时间(单位:秒)

-b      ----------windowsize,TCP发送/接收的缓冲大小(单位:字节)

-p      ---------postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数

-u     -----------putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数

-T     ------------content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。

-v     -----------verbosity,指定打印帮助信息的冗余级别。

-w     -----------以HTML表格形式打印结果。

-i     -----------使用HEAD请求代替GET请求。

-x     -----------插入字符串作为table标签的属性。

-y    ----------- 插入字符串作为tr标签的属性。

-z     -----------插入字符串作为td标签的属性。

-C     -----------添加cookie信息,例如:“Apache=1234”(可以重复该参数选项以添加多个)。

-H     -----------添加任意的请求头,例如:“Accept-Encoding: gzip”,请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。

-A     -----------添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。

-P     -----------添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。

-X     -----------指定使用的和端口号,例如:“126.10.10.3:88”。

-V     -----------打印版本号并退出。

-k     -----------使用HTTP的KeepAlive特性。

-d     -----------不显示百分比。

-S     -----------不显示预估和警告信息。

-g     -----------输出结果信息到gnuplot格式的文件中。

-e     -----------输出结果信息到CSV格式的文件中。

-r     -----------指定接收到错误信息时不退出程序。

-h     -----------显示用法信息,其实就是ab -help。




#使用ab工具测试

[root@web02 conf.d]# ab -n 20 -c 2 http://www.host1.com/

Server Software:        nginx/1.16.1
Server Hostname:        www.host1.com
Server Port:            80


Document Path:          /
Document Length:        581 bytes


Concurrency Level:      2
Time taken for tests:   0.007 seconds
Complete requests:      20
Failed requests:        19
   (Connect: 0, Receive: 0, Length: 19, Exceptions: 0)
Write errors:           0
Non-2xx responses:      19
Total transferred:      8125 bytes
HTML transferred:       4324 bytes
Requests per second:    3056.70 [#/sec] (mean)
Time per request:       0.654 [ms] (mean)
Time per request:       0.327 [ms] (mean, across all concurrent requests)
Transfer rate:          1212.68 [Kbytes/sec] received


nginx关联php语法
1> fastcgi_pass
#语法
Syntax:	fastcgi_pass address;
Default:	—
Context:	location, if in location

fastcgi_pass 127.0.0.1:9000;
2> fastcgi_index
#语法
Syntax:	fastcgi_index name;
Default:	—
Context:	http, server, location

fastcgi_index index.php
3>fastcgi_param
#语法
Syntax:	fastcgi_param parameter value [if_not_empty];
Default:	—
Context:	http, server, location

#语法模块	  开始定义(标准格式)站点目录  php文件名字
fastcgi_param SCRIPT_FILENAME /code/$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
配置nginx关联php
[root@web01 conf.d]# vim php.conf 
server {
    listen 80;
    server_name www.php.com;

    location / {
        root /code;
        index index.html;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /code/$fastcgi_script_name;
        include fastcgi_params;
    }
}

php上传文件限制

[root@web01 ~]# vim /etc/php.ini
#默认post请求字符串内容不超过8m
post_max_size = 20M
#默认上传文件大小不超过2m
upload_max_filesize = 20M

[root@web01 ~]# systemctl restart php-fpm
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琴声浮或沉__听懂只一人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值