安装 Ubuntu 24.04 LTS, Webmin, Nginx, MariaDB, PHP8.1-FPM,Perl-Fastcgi 到 DigitalOcean 的 VPS上。
DavidYin 介绍了如何在 DigitalOcean 创建新 VPS。并且完成基本的 Ubuntu 24.04 LTS的系统。然后介绍如何安装 Webmin 主机控制面板,时区设置和 SSH 的安全设置。再之后说明一下如何用之前的新添加的用户来安装 Nginx Web 服务器和 MairaDB 数据库服务器。
现在就是很重要的语言支持部分了。因为我用的最多的就是 php 以及 perl 语言。所以这两部分就是重点了。
Ubuntu 24.04 LTS 仓库所包含的是 php8.3.6,目前 php 官方支持的版本是 8.1,8.2,8.3 这三个系列。所以直接使用 Ubuntu 的就已经是很新的版本了。
sudo apt install php8.3 php8.3-fpm php8.3-cli php8.3-common php8.3-mbstring php8.3-gd php8.3-intl php8.3-xml php8.3-mysql php8.3-zip php8.3-curl
安装完成后,执行 php -v 命令,可以看到版本信息。
davidyin@walnut:~$ php -v
PHP 8.3.6 (cli) (built: Sep 30 2024 15:17:17) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
davidyin@walnut:~$
再看一下 php8.3-fpm 是否已经运行。
说明:下面这些都是用来举例说明所用的,在实际使用中请用真实的数据。
IP: 143.110.227.68
Domain: u24.webexample.win
username: davidyin
接下来我要设置一个 vhost,就是一个虚拟主机,我用的域名是 u24.webexample.win,此为举例而已。 到域名服务商的网站,专门设置域名记录的地方,把 u24.webexample.win 的 A 记录指向此 VPS 的 IP 地址,生效可能需要十分钟或更久,为快捷,可以在所操作的Windows hosts 文件添加纪录使之在本地立即可用。 新建一个主机配置文件,u24.conf,如下。
sudo nano /etc/nginx/conf.d/u24.conf
这里我会定义 log 文件的格式,以及它的储存位置。
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name u24.webexample.win;
access_log /var/log/nginx/host.access.log main;
root /home/davidyin/u24.webexample.win;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ = 404;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
保存退出,然后执行 sudo nginx -t
命令看看,是否配置文件正确。若正确,就重启 Nginx 服务,使配置生效。
sudo service nginx restart
在/home/davidyin/u24.webexample.win/下新建一个文件 info.php
输入如下内容:
<?php
phpinfo();
回到桌面浏览器中,输入网址 http://u24.webexample.win/info.php,如果看到下面这些内容,就表示 php 安装正确, nginx 也运行正确。
我一般会把 www-data 用户添加到 当前用户的组内,比如我这里用的 davidyin 用户就在同名的 davidyin 组内。
有时候会出现奇怪的问题,找不到文件啊,没有权限啊。这是最好的解决方法,就是重启服务器。有时候是配置未生效。
如果出现问题,或者是页面错误,可以查看这两个日志文件。/var/log/nginx/host.access.log
/var/log/nginx/error.log
至此,php8.3 已经安装完毕,并且虚拟主机也可以使用 php 的语言了。
如果是商用,或者愿意购买一年期的证书,DavidYin建议到 Gogetssl 购买,这里价格最优,无限重签,不限服务器。目前的证书可以购买5年的,但是实际签发的证书都是一年的,每年重新签发一次,直到购买的年份用完为止。买多年的会便宜一点。
Gogetssl 证书的好处是你买了一个证书给域名 A 用,如果这个域名不用了,还可以签发给域名 B 使用剩下的时间。
免费证书这里我采用 Zerossl 的 SSL 证书。使用了 Neilpang 的 ACME.SH 来安装。每月自动续签。
先安装工具。
curl https://get.acme.sh | sh -s email=seo@g2soft.net
接下来,重新登入SSH,相当于重新载入 BASH 环境,因为上面的安装已经把路径配置到 Bash 中了,并且自动创建了一个 bash 的别名,方便使用,直接输入 acme.sh 命令就可以了。另外还自动创建了一个 cronjob,每天零点自动检测所有的证书,如果快过期,就会自动更新。
验证域名的方式有两种,DNS 和 http,这次我用了 http 方式来验证。
davidyin@walnut:~$ acme.sh --issue -d u24.webexample.win --webroot /home/davidyin/u24.webexample.win
[Mon Oct 7 16:37:46 PDT 2024] Using CA: https://acme.zerossl.com/v2/DV90
[Mon Oct 7 16:37:46 PDT 2024] Single domain='u24.webexample.win'
[Mon Oct 7 16:37:48 PDT 2024] Getting webroot for domain='u24.webexample.win'
[Mon Oct 7 16:37:48 PDT 2024] Verifying: u24.webexample.win
[Mon Oct 7 16:37:49 PDT 2024] Processing. The CA is processing your order, please wait. (1/30)
[Mon Oct 7 16:37:52 PDT 2024] Success
[Mon Oct 7 16:37:52 PDT 2024] Verification finished, beginning signing.
[Mon Oct 7 16:37:52 PDT 2024] Let's finalize the order.
[Mon Oct 7 16:37:52 PDT 2024] Le_OrderFinalize='https://acme.zerossl.com/v2/DV90/order/-0HtI52SzVp9B1iWfXvHrw/finalize'
[Mon Oct 7 16:37:53 PDT 2024] Order status is 'processing', let's sleep and retry.
[Mon Oct 7 16:37:53 PDT 2024] Sleeping for 15 seconds then retrying
[Mon Oct 7 16:38:09 PDT 2024] Polling order status: https://acme.zerossl.com/v2/DV90/order/-0HtI52SzVp9B1iWfXvHrw
[Mon Oct 7 16:38:09 PDT 2024] Downloading cert.
[Mon Oct 7 16:38:09 PDT 2024] Le_LinkCert='https://acme.zerossl.com/v2/DV90/cert/65Q_RSKwu-urE1DZVXE7FA'
[Mon Oct 7 16:38:10 PDT 2024] Cert success.
-----BEGIN CERTIFICATE-----
MIIEBzCCA4ygAwIBAgIRANWHTHkkfhcpadmh96AqH5IwCgYIKoZIzj0EAwMwSzEL
MAkGA1UEBhMCQVQxEDAOBgNVBAoTB1plcm9TU0wxKjAoBgNVBAMTIVplcm9TU0wg
中间省略
2u8271N/ejTHa2yuKuF4KiMP+BywmEifAjEAm/U9GoOqf7u/4yiVAAp6Neo5Nt5Q
Xm/X1Y3+KB0c636aAkftFce8fXep9o5RXpB2
-----END CERTIFICATE-----
[Mon Oct 7 16:38:10 PDT 2024] Your cert is in: /home/davidyin/.acme.sh/u24.webexample.win_ecc/u24.webexample.win.cer
[Mon Oct 7 16:38:10 PDT 2024] Your cert key is in: /home/davidyin/.acme.sh/u24.webexample.win_ecc/u24.webexample.win.key
[Mon Oct 7 16:38:10 PDT 2024] The intermediate CA cert is in: /home/davidyin/.acme.sh/u24.webexample.win_ecc/ca.cer
[Mon Oct 7 16:38:10 PDT 2024] And the full-chain cert is in: /home/davidyin/.acme.sh/u24.webexample.win_ecc/fullchain.cer
验证正确,就会自动签发证书,证书会临时先存放在一个工作目录,现在我要指定一个目录存放: /home/davidyin/ssl/。 之后就是安装证书到该目录。
acme.sh --install-cert -d u24.webexample.win \
--key-file /home/davidyin/ssl/key \
--fullchain-file /home/davidyin/ssl/cert \
--reloadcmd "service nginx force-reload"
就这样,证书也签发好了,也安装到指定位置,接下来会介绍如何在 nginx 的配置文件中,设置证书路径,设置 https,设置重定向,还有 perl-fastcgi等等。