如何在Ubuntu 20.04上使用PHP和MariaDB安装和配置Caddy Web服务器

如何在Ubuntu 20.04上使用PHP和MariaDB安装和配置Caddy Web服务器

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

在本教程中,您将安装并配置Caddy使其在基于Ubuntu 20.04的服务器上与PHP和MariaDB一起运行。

先决条件

  • 具有具有sudo特权的非root帐户的Ubuntu 20.04服务器。
  • 完全注册的域名指向您服务器的IP地址。

步骤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)

第2步-安装球童

有多种安装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的存储库的位置。

安装球童。

$sudo apt install caddy

这将自动安装并运行Caddy Web服务器。 在浏览器中打开http://,下面的页面应该会打招呼。

球童默认页面

第3步-安装PHP

安装Ondrej的PHP库。

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

安装PHP 7.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

步骤4-安装MariaDB

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

添加Mariadb的官方存储库。 您可以从MariaDB的存储库页面中选择更接近服务器位置的其他镜像。Advertisement.banner-1{text-align:center; padding-top:10pximportant; padding-bottom:10pximportant; padding-left:0pximportant; padding-right:0pximportant; width:100%important; box-sizing:border-boximportant; background -color:#eeeeeeimportant; border:1px实心#dfdfdf}

$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

步骤5-配置MariaDB

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

$sudo mysql_secure_installation

使用MariaDB 10.4,现在将询问您使用root密码还是 unix_socket 插入。 的 unix_socket 插件可让您使用Linux用户凭据登录到MariaDB。 尽管您将需要传统的用户名/密码来使用phpMyAdmin之类的第三方应用程序,但它被认为更安全。 我们将坚持使用 unix_socket 本教程的插件。 您仍然可以通过为数据库创建的特定用户来使用phpMyAdmin。

按Enter键选择默认选项(大写字母,在这种情况下为Y)。

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密码。

创建一个具有访问权限的测试数据库和用户。 更换 databaseuser 根据您的选择。 更换 password 具有强密码。

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

退出MySQL Shell。

exit

第6步-配置球童

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

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

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

-p 标志还会创建任何丢失的父目录。

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

$sudo mkdir /var/log/caddy

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

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

在安装过程中,Caddy在以下位置生成了默认的Caddyfile: /etc/caddy/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 [email protected] {
    	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套接字的请求。
  • tls 块配置与SSL证书和安全性相关的设置。 在这里,我们启用了对TLSv1.2和TLSv1.3协议的支持。 默认情况下,Caddy开箱即用支持TLS v1.2。 Caddy还会自动为所有站点生成SSL证书。 如果您不希望Caddy为您生成SSL,则可以使用IP地址而不是主机名或指定完整的URL(即http://example.com)来实现。 在这种情况下,Caddy不会生成SSL证书。

到目前为止,我们已经涵盖了编写Caddyfile的绝对基础,它可以帮助您入门。 您可以在官方文档中了解更多信息。

第7步-配置PHP

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

首先,我们必须更改PHP进程的用户名。 开启档案 /etc/php-fpm.d/www.conf

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

找出 user=www-datagroup=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-datalisten.group=www-data 在文件中并将其更改为 caddy

listen.owner = caddy
listen.group = caddy

通过按Ctrl + X并在出现提示时输入Y来保存文件。

重新启动PHP-fpm进程。

$sudo systemctl restart php7.4-fpm

第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 [email protected]
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凭据的字段。



    Caddy Demo Site
    


    

Caddy Demo Site

Hello,

'; // 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('

Your connection has failed.

' . mysqli_connect_error()); } echo '

You have connected successfully.

'; ?>

通过按Ctrl + X并在出现提示时输入Y来保存文件。

在网络浏览器中访问https://example.com/test.php。 您应该获得以下页面。

球童演示网站页面

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

如果满意,请删除测试文件。

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

结论

到此结束我们的教程,我们在其中安装了Caddy Web服务器以及PHP和MariaDB SQL。 如果您有任何疑问,请在下面的评测中提问。

资讯来源:由0x资讯编译自HOWTOFORGE,版权归作者所有,未经许可,不得转载
你可能还喜欢