web服务apache

配置

CentOS7

使用清华大学的remi源自动生成php和mysql的yum源


测试

测试工具ab命令

-n 请求次数 执行一次测试会话时候所发出的请求数目,默认状态下是一次
-c 并发次数 同时向服务器端发送的请求数目,默认状态下是一次

[root@centos8 ~]#ab -c100 -n1000 http://10.0.0.6/blog/

测试结果

[root@centos8 ~]#ab -c 10 -n 100 http://10.0.0.6/blog
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 10.0.0.6 (be patient).....done


Server Software:        Apache
Server Hostname:        10.0.0.6
Server Port:            80

Document Path:          /blog
Document Length:        229 bytes

Concurrency Level:      10
Time taken for tests:   0.017 seconds
Complete requests:      100
Failed requests:        0
Non-2xx responses:      100
Total transferred:      43400 bytes
HTML transferred:       22900 bytes
Requests per second:    5986.59 [#/sec] (mean)
Time per request:       1.670 [ms] (mean)
Time per request:       0.167 [ms] (mean, across all concurrent requests)
Transfer rate:          2537.29 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.3      0       2
Processing:     0    1   0.5      1       3
Waiting:        0    1   0.5      1       2
Total:          0    1   0.7      1       3
ERROR: The median and mean for the initial connection time are more than twice the standard
       deviation apart. These results are NOT reliable.

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      2
  75%      2
  80%      2
  90%      2
  95%      3
  98%      3
  99%      3
 100%      3 (longest request)


[root@centos8 ~]#ab -c100 -n1000 http://10.0.0.6/blog/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 10.0.0.6 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache
Server Hostname:        10.0.0.6
Server Port:            80

Document Path:          /blog/
Document Length:        33525 bytes

Concurrency Level:      100
Time taken for tests:   17.607 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      33757000 bytes
HTML transferred:       33525000 bytes
Requests per second:    56.80 [#/sec] (mean)
Time per request:       1760.671 [ms] (mean)
Time per request:       17.607 [ms] (mean, across all concurrent requests)
Transfer rate:          1872.34 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.9      1      19
Processing:   292 1688 322.2   1646    3716
Waiting:      267 1614 326.8   1552    3583
Total:        292 1689 322.3   1647    3718

Percentage of the requests served within a certain time (ms)
  50%   1647
  66%   1768
  75%   1860
  80%   1917
  90%   2054
  95%   2226
  98%   2489
  99%   2649
 100%   3718 (longest request)

同时请求的次数越多,服务器处理的速度会越慢。

测试持久连接

vim /etc/httpd/conf.d/test.conf
KeepAlive Off

测试

[root@centos8 conf.d]#telnet 10.0.0.5 80
Trying 10.0.0.5...
Connected to 10.0.0.5.
Escape character is '^]'.
GET /test2.html HTTP/1.1
host: 1.1.1.2

HTTP/1.1 200 OK
Date: Wed, 19 Jan 2022 03:17:02 GMT
Server: Apache
Last-Modified: Wed, 19 Jan 2022 02:41:52 GMT
ETag: "9-5d5e65408364b"
Accept-Ranges: bytes
Content-Length: 9
Content-Type: text/html; charset=UTF-8

webpage2
Connection closed by foreign host.

提示:输入完host之后要按两次回车,才执行
否则就会出现请求超时的提示

[root@localhost ~]#telnet 10.0.0.5 80
Trying 10.0.0.5...
Connected to 10.0.0.5.
Escape character is '^]'.
GET /index.html HTTP/1.1
host: 2.2.2.2
HTTP/1.1 408 Request Timeout
Date: Tue, 18 Jan 2022 08:49:46 GMT
Server: Apache
Content-Length: 221
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>408 Request Timeout</title>
</head><body>
<h1>Request Timeout</h1>
<p>Server timeout waiting for the HTTP request from the client.</p>
</body></html>
Connection closed by foreign host.

开启持久连接

KeepAlive on
KeepAliveTimeout 30

测试
第一次请求完毕连接不断开

[root@centos8 conf.d]#telnet 10.0.0.5 80
Trying 10.0.0.5...
Connected to 10.0.0.5.
Escape character is '^]'.
GET /test2.html HTTP/1.1
host: 1.1.2.1

HTTP/1.1 200 OK
Date: Wed, 19 Jan 2022 03:18:51 GMT
Server: Apache
Last-Modified: Wed, 19 Jan 2022 02:41:52 GMT
ETag: "9-5d5e65408364b"
Accept-Ranges: bytes
Content-Length: 9
Content-Type: text/html; charset=UTF-8

webpage2
GET /test1.html HTTP/1.1
host: 1.1.2.3

HTTP/1.1 200 OK
Date: Wed, 19 Jan 2022 03:19:13 GMT
Server: Apache
Last-Modified: Wed, 19 Jan 2022 02:41:46 GMT
ETag: "9-5d5e653a44073"
Accept-Ranges: bytes
Content-Length: 9
Content-Type: text/html; charset=UTF-8

webpage1
Connection closed by foreign host.

练手小项目

环境准备

安装必要的包

如果服务器中装的有mariadb-community版本,yum会提示和当前安装的mysql-server冲突,卸载掉mysql-community就行。

yum install php php-mysqlnd mysql-server httpd -y
启动服务

启动服务并设置为开机启动

systemctl enable --now httpd 
systemctl enable --now mysqld
systemctl enable --now php-fpm
测试服务是否正常
测试apache

换一个虚拟机用curl命令,或者直接在宿主机访问httpd服务器地址即可

[root@centos8 ~]#curl 10.0.0.6
<h1> http website test <h1\>
测试php

编写php测试界面

[root@centos8 ~]#cat > /var/www/html/phpinfo.php << EOF
> <?php
> phpinfo();
> ?>
> EOF

重启服务

systemctl restart php-fpm

这个测试用浏览器更容易看出来,地址栏输入10.0.0.6/phpinfo.php 可以看到php的详细信息即测试成功

测试php连接数据库

编写测试界面

[root@centos8 ~]#vim /var/www/html/lamp.php
<?php
try {
$user='root';
$pass='';
$dbh= new PDO('mysql:host=localhost;port=3306;dbname=mysql', $user, $pass);
foreach($dbh->query('SELECT user,host from user') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>

简单说明:$user就是连接数据库使用的账户,$pass连接数据库账户登录需要的密码,默认yum安装好mysql-server 密码就是空
登录上数据库之后执行一条查询select user,host from user

在数据库中创建一个用户在浏览器中使用php进行查询

[root@centos8 html]#mysql -uroot -p123456 -e"create user zhao1@'localhost' identified by '123456';"
mysql: [Warning] Using a password on the command line interface can be insecure.

网页显示了在数据库中创建的用户
在这里插入图片描述

PHPadmin

Discuz

在这里插入图片描述
点击下一步 进入安装
在这里插入图片描述
设置服务器数据库账户密码,论坛的管理员账户密码
在这里插入图片描述
进入正常安装步骤
在这里插入图片描述
访问首页,登录管理员账户测试
在这里插入图片描述

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| ultrax             |
| wordpress          |
+--------------------+
6 rows in set (0.00 sec)
mysql> use ultrax ;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------------------------+
| Tables_in_ultrax                  |
+-----------------------------------+
| pre_common_admincp_cmenu          |
| pre_common_admincp_group          |
| pre_common_admincp_member         |
| pre_common_admincp_perm           |
| pre_common_admincp_session        |
| pre_common_admingroup             |
| pre_common_adminnote              |
| pre_common_advertisement          |
| pre_common_advertisement_custom   |
| pre_common_banned                 |

WrodPress

Centos8部署
#1.解压下载好的wordpress包
[root@centos8 ~]# tar -xvf wordpress-5.4.1-zh_CN.tar.gz
#2.将解压好的包移动到apache默认的发布目录下
[root@centos8 ~]# mv wordpress /var/www/html/blog
#3. 更改文件夹的所有者所属组
[root@centos8 ~]# chown -R apache.apache /var/www/html/
Centos7部署

配置php mysql yum源

[root@localhost yum.repos.d]# yum install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm -y 
[root@localhost yum.repos.d]# yum install -y php74-php php74-php-mysqlnd

部署mysql

[root@centos8 ~]#mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1012
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| ultrax             |
| wordpress          |
+--------------------+
6 rows in set (0.01 sec)

mysql> create database blog;
Query OK, 1 row affected (0.01 sec)

mysql> create user blog@'10.0.0.%' identified by 123456;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '123456' at line 1
mysql> create user blog@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all on blog.* to blog@'10.0.0.%' ;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

php-opcache加速

[root@centos8 ~]#dnf install php-opcache -y

使用ab命令测试
速度提升了大概5倍

[root@centos8 ~]#ab -c100 -n1000 http://10.0.0.6/blog/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 10.0.0.6 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache
Server Hostname:        10.0.0.6
Server Port:            80

Document Path:          /blog/
Document Length:        33525 bytes

Concurrency Level:      100
Time taken for tests:   3.907 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      33757000 bytes
HTML transferred:       33525000 bytes
Requests per second:    255.98 [#/sec] (mean)
Time per request:       390.659 [ms] (mean)
Time per request:       3.907 [ms] (mean, across all concurrent requests)
Transfer rate:          8438.52 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.1      0      13
Processing:    31  345  99.1    311     580
Waiting:       20  335  99.5    302     571
Total:         34  346  99.0    312     581

Percentage of the requests served within a certain time (ms)
  50%    312
  66%    373
  75%    435
  80%    457
  90%    493
  95%    527
  98%    540
  99%    544
 100%    581 (longest request)

LAMP多机部署

规划:

  • 10.0.0.5 httpd
  • 10.0.0.6 php
  • 10.0.0.18 mysql
10.0.0.5

部署httpd

yum install httpd -y
systemctl enable --now httpd 

配置fcgi

[root@centos8 ~]#cat /etc/httpd/conf.d/cgi.conf
DirectoryIndex index.php
ProxyRequests Off

ProxyPassMatch ^/(.*\.php)$ fcgi://10.0.0.6:9000/data/html/$1
ProxyPassMatch ^/(fpm_status|ping) fcgi://10.0.0.6:9000

重启服务

systemctl restart httpd

查看端口是否正常

[root@centos8 ~]#ss -ntl
State            Recv-Q           Send-Q                       Local Address:Port                        Peer Address:Port           Process
LISTEN           0                128                                      *:80                                     *:*
LISTEN           0                128                                   [::]:22                                  [::]:*

编写测试httpd界面

[root@centos8 ~]#echo "test lamp" > /var/www/html/index.html

centos7进行测试

[root@localhost ~]#curl 10.0.0.5
test lamp
10.0.0.6

配置php

#安装
yum install php-fpm php-mysqlnd -y

更新配置文件

vim /etc/php-fpm.d/www.conf
[www]
user = apache
group = apache
listen = /run/php-fpm/www.sock
listen = 0.0.0.0:9000
listen.acl_users = apache,nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.status_path = /fpm_status
ping.path = /ping
ping.response = pong
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache

配置httpd服务解析的fcgi文件夹

mkdir /data/html
echo "<?php phpinfo() ?>" > /data/html/info.php

启动服务

systemctl enable --now php-fpm

测试

[root@localhost ~]#curl -I 10.0.0.5/info.php
HTTP/1.1 200 OK
Date: Wed, 26 Jan 2022 02:46:52 GMT
Server: Apache/2.4.37 (centos)
X-Powered-By: PHP/7.2.24
Content-Type: text/html; charset=UTF-8

配置php连接mysql

[root@centos8 html]#vim /data/html/lamp.php
<?php
try {
$user='lamp';
$pass='123456';
$dbh= new PDO('mysql:host=10.0.0.18;port=3306;dbname=mysql', $user, $pass);
foreach($dbh->query('SELECT user,host from user') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>

重启服务

systemctl restart php-fpm

查看端口

[root@centos8 html]#ss -ntl
State            Recv-Q           Send-Q                       Local Address:Port                        Peer Address:Port           Process
LISTEN           0                128                                0.0.0.0:9000                             0.0.0.0:*
10.0.0.18

安装&配置

yum install mysql-server -y
#启动服务
systemctl enable --now mysqld
#连接数据库
[root@centos8 ~]#mysql -uroot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.26 Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user lamp@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to lamp@'10.0.0.%' ;
Query OK, 0 rows affected (0.01 sec)

centos7 测试

[root@localhost ~]#curl 10.0.0.5/lamp.php
Array
(
    [user] => lamp
    [0] => lamp
    [host] => 10.0.0.%
    [1] => 10.0.0.%
)
Array
(
    [user] => mysql.infoschema
    [0] => mysql.infoschema
    [host] => localhost
    [1] => localhost
)
Array
(
    [user] => mysql.session
    [0] => mysql.session
    [host] => localhost
    [1] => localhost
)
Array
(
    [user] => mysql.sys
    [0] => mysql.sys
    [host] => localhost
    [1] => localhost
)
Array
(
    [user] => root
    [0] => root
    [host] => localhost
    [1] => localhost
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值