第2步-安装Caddy

安装Caddy有几种方法,其中包括docker安装,使用官方存储库或从源代码构建。
如果我们需要只能通过第三方模块/插件添加的功能,则从源代码构建方法非常有用。

在本教程中,我们将坚持在基于Ubuntu/Debian的系统中使用官方的Caddy存储库。

将存储库添加到列表中

$ echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list
$ sudo apt update

这将在/etc/apt/sources.list.d目录中创建一个caddy存储库文件。

安装Caddy

$ sudo apt install caddy

这将自动安装并运行Caddy web服务器。在浏览器中打开http://域名/。

步骤5-配置MariaDB

运行以下命令以执行默认配置,例如提供根密码、删除匿名用户、禁止远程根用户登录以及删除测试表。

$ sudo mysql_secure_installation

在MariaDB 10.4中,现在将询问我们是使用根密码还是使用“unix_socket”插件。“unix_socket”插件允许我们使用Linux用户凭据登录MariaDB。它被认为是更安全的,但你将需要一个传统的用户名/密码使用第三方应用程序,如phpMyAdmin。在本教程中,我们将坚持使用“unix_socket”插件。我们仍然可以通过为数据库创建的特定用户使用phpMyAdmin。

按Enter键选择默认选项(大写的选项,在本例中为Y)。

<code class="language-ssh" lang="ssh">NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): [PRESS ENTER]
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] [PRESS ENTER]
Enabled successfully!
Reloading privilege tables..
 ... Success!

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] [ANSWER n]
... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] [PRESS ENTER]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] [PRESS ENTER]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] [PRESS ENTER]
 \- Dropping test database...
 ... Success!
 \- Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] [PRESS ENTER]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

登录到MariaDB的sql shell。

$ sudo mysql

提示时输入root密码。

创建具有访问权限的测试数据库和用户。将“password”替换为一个强壮复杂的密码。

Mysql> CREATE DATABASE testdb;
Mysql> CREATE USER 'user' IDENTIFIED BY 'password';
Mysql> GRANT ALL PRIVILEGES ON testdb.* TO 'user';

退出MySQL Shell。

Mysql> exit

步骤4-安装MariaDB

MariaDB是MySQL的一个替代品,这意味着运行和操作MariaDB的命令与MySQL的命令相同。

添加Mariadb的官方存储库。我们可以从中选择一个更靠近服务器位置的不同镜像

$ sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
$ sudo add-apt-repository 'deb [arch=amd64] http://mirror.lstn.net/mariadb/repo/10.4/ubuntu f
ocal main'
`

要安装MariaDB,执行以下命令。

$ sudo apt install mariadb-server

检查MariaDB是否正确安装。

$ mysql --version

我们应该看到以下输出。

mysql  Ver 15.1 Distrib 10.4.13-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

启用MariaDB服务。

$ sudo systemctl enable mariadb

第8步-启动演示站点

现在我们已经创建了一个Caddyfile并配置了PHP来与服务器一起运行,现在是时候创建和启动一个演示网站了。

确保你的域名指向服务器的IP地址。

重新启动Caddy服务器以应用我们在上面创建的Caddyfile中所做的更改。

$ sudo systemctl restart caddy

检查Caddy服务器的状态,以确保它正常工作。

$ sudo systemctl status caddy
? caddy.service - Caddy
     Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-05-20 07:09:25 UTC; 2s ago
       Docs: https://caddyserver.com/docs/
   Main PID: 25410 (caddy)
      Tasks: 7 (limit: 1074)
     Memory: 17.8M
     CGroup: /system.slice/caddy.service
             ??25410 /usr/bin/caddy run --environ --config /etc/caddy/Caddyfile

May 20 07:09:25 example.com caddy[25410]: 2020/05/20 07:09:25 [INFO][example.com] Obtain: Lock acquired; proceeding...
May 20 07:09:25 example.com caddy[25410]: 2020/05/20 07:09:25 [INFO][cache:0xc0006f8cd0] Started certificate maintenance routine
May 20 07:09:25 example.com caddy[25410]: 2020/05/20 07:09:25 [INFO] acme: Registering account for jack@oir
May 20 07:09:26 example.com caddy[25410]: 2020/05/20 07:09:26 [INFO][example.com] Waiting on rate limiter...
May 20 07:09:26 example.com caddy[25410]: 2020/05/20 07:09:26 [INFO][example.com] Done waiting
May 20 07:09:26 example.com caddy[25410]: 2020/05/20 07:09:26 [INFO] [example.com] acme: Obtaining bundled SAN certificate given a CSR
May 20 07:09:27 example.com caddy[25410]: 2020/05/20 07:09:27 [INFO] [example.com] AuthURL: https://acme-v02.api.letsencrypt.org/acme/authz-v3/4696123289
May 20 07:09:27 example.com caddy[25410]: 2020/05/20 07:09:27 [INFO] [example.com] acme: use tls-alpn-01 solver
May 20 07:09:27 example.com caddy[25410]: 2020/05/20 07:09:27 [INFO] [example.com] acme: Trying to solve TLS-ALPN-01
May 20 07:09:27 example.com caddy[25410]: 2020/05/20 07:09:27 http: TLS handshake error from 127.0.0.1:39466: EOF

从上面可以看到,当我们在创建Caddyfile之后第一次重新启动服务器时,Caddy自动生成了证书。

让我们创建一个测试页面来验证Caddy是否可以呈现PHP并连接到MariaDB数据库。

$ sudo nano /var/www/example.com/html/test.php

在编辑器中粘贴以下代码。将“user”和“password”字段替换为之前创建的MariaDB凭据。

<html>
<head>
    <title>Caddy Demo Site</title>

</head>
<body id="wrap">

## Caddy Demo Site

<?php echo 'Hello, Caddy -Onitroad';

    // Define PHP variables for the MySQL connection.
    $servername = "localhost";
    $username = "user";
    $password = "password";

    // Create a MySQL connection.
    $conn = mysqli_connect($servername, $username, $password);

    // Report if the connection fails or is successful.
    if (!$conn) {
        exit('连接失败!' .  mysqli_connect_error());
    }
    echo '连接成功!';
?>
</body>
</html>

保存文件。

在web浏览器中,打开站点,https://example.com/test.php

如果我们看到错误消息或页面根本没有加载,请重新检查配置。

如果一切ok, 删除测试文件。

$ sudo rm /var/www/example.com/html/test.php

准备工作

一个ubuntu20.04服务器,具有sudo特权的非root帐户。

指向服务器IP地址的完全注册域名。

步骤6-配置Caddy

可以通过多种方式配置Caddy—API调用、JSON文件或Caddyfile。Caddyfile是配置Caddy的最简单方法,我们将在本教程中使用它。

在配置Caddy之前,我们应该为我们的站点创建根目录。

$ sudo mkdir -p /var/www/example.com/html

我们还需要创建一个目录来存储Caddy的日志文件。

$ sudo mkdir /var/log/caddy

安装时的Caddy服务器创建一个用户“Caddy”,该用户为其处理任务。我们需要授予日志目录的权限,以便Caddy可以访问和写入它。

$ sudo chown -R caddy:caddy /var/log/caddy

在安装过程中,Caddy将在“/etc/Caddy/Caddyfile”生成了一个默认的Caddyfile。打开它。

$ sudo nano /etc/caddy/Caddyfile

它应该看起来像下面这样。

# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace the line below with your
# domain name.
:80

# Set this path to your site's directory.
root * /usr/share/caddy

# Enable the static file server.
file_server

# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080

# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000

# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile

:80告诉Caddy通过80号端口提供所有服务。
root设置站点主目录的路径。
file_server使Caddy可以作为静态文件服务器运行。

将上述代码替换为以下代码。

{
	experimental_http3
}
example.com {
    root * /var/www/example.com/html
    log {
        output file /var/log/caddy/example.com.access.log {
        	roll_size 3MiB
	        roll_keep 5
	        roll_keep_for 48h
        }
        format console
    }
    encode gzip zstd
    php_fastcgi unix//run/php/php7.4-fpm.sock

    tls jack@oir {
    	protocols tls1.2 tls1.3    	
    }
}
`

让我们看看下面的Caddyfile的所有部分。

任何Caddyfile中的第一个块都是全局块,除非我们指定了一个主机名,然后它就变成了站点块。全局块包含应用于服务器下托管的所有站点的指令。在我们的示例中,我们启用了对实验性HTTP/3协议的支持。全局块是完全可选的,我们始终可以直接用站点块启动Caddyfile。

下一个区块是站点区块。如果服务器上只有一个站点,我们不需要将配置包含在一个块中,但是如果我们要托管多个站点,则应该将每个站点的配置托管在其自己的块中。站点块用大括号标记。每个站点块都以站点的主机名开始。

log启用和配置HTTP请求日志记录。没有 log 指令,Caddy将什么日志都不会记录。
output配置日志文件的写入位置。
format描述如何对日志进行编码或格式化。
console设置日志条目的格式,以便于用户阅读。
这里的encode指令为站点启用Gzip和Zstandard压缩。
php_fastcgi将请求代理到php fastcgi服务器,如“php fpm”。这里我们正在通过Unix套接字监听请求。

tlsblock配置与SSL证书和安全性相关的设置。这里,我们启用了对TLSv1.2和TLSv1.3协议的支持。默认情况下,Caddy支持tlsv1.2开箱即用。Caddy还为所有站点自动生成SSL证书。如果我们不希望Caddy为我们生成SSL,可以使用IP地址而不是主机名,或者指定完整的URL,即。在这种情况下,Caddy不会生成SSL证书。

到目前为止,我们已经介绍了编写Caddyfile的基础知识。

步骤3-安装PHP

安装Ondrej的PHP存储库。

$ sudo add-apt-repository ppa:ondrej/php

安装php7.4和一些额外的软件包。

$ sudo apt install php-cli php-fpm php-mysql

检查PHP是否正常工作。

$ php --version

我们应该会看到类似的输出。

PHP 7.4.6 (cli) (built: May 14 2020 10:03:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

步骤7-配置PHP

现在,我们的Caddyfile已经准备好了,是时候配置PHP了。

首先,我们必须更改PHP进程的用户名。打开文件'/etc/php-fpm.d/www.conf`.

$ sudo nano /etc/php/7.4/fpm/pool.d/www.conf

在文件中找到“user=www-data”和“group=www-data”行,并将它们更改为“caddy”。

...
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = caddy
; RPM: Keep a group allowed to write in log dir.
group = caddy
...

同时,找出listen.owner=www-data行和 listen.group=www-data行并将他们更改为“caddy”。

listen.owner = caddy
listen.group = caddy

Ctrl+X保存文件,提示时输入 Y

重新启动PHP-fpm进程。

$ sudo systemctl restart php7.4-fpm

步骤1-配置防火墙

如果使用的是UFW(简单防火墙),则需要将其配置为允许访问HTTP和HTTPS端口,以便服务器正常工作。

检查防火墙的状态。

$ sudo ufw status

你应该看到下面这样的输出。

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
`

允许HTTP和HTTPs端口。

$ sudo ufw allow http
$ sudo ufw allow https

再次确认状态。

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)

总结

我们在unbuntu上安装了caddy和PHP和mariadb 。

如何在ubuntu20.04上使用PHP和MariaDB安装和配置caddyweb服务器

CaddyWeb服务器是一个用GO语言编写的现代开源Web服务器。它没有任何依赖关系,运行在静态二进制文件之外,并自动生成和更新SSL证书。它可以作为静态文件服务器、可扩展的反向代理或强大的动态服务器,并且可以通过插件进行扩展。它还包括对HTTP/2和实验性HTTP/3协议的支持。

在本教程中,我们将安装并配置Caddy,使其在基于ubuntu20.04的服务器上与PHP和MariaDB一起运行。

日期:2019-04-29 03:17:13 来源:oir作者:oir