Debian12 使用 nginx 与 php8.2 使用 Nextcloud

最近将小服务器升级了下系统,使用了 debian12 的版本,正好试试 nginx 和 php-fpm 这种方式运行 Nextcloud 这个私有云的配置。

一、基本系统及应用安装

系统:debian12 x86_64 位版本最小安装,安装后可根据自己需求安装一些工具,比如 neofetch 等自己喜欢的工具;

nginx:使用系统源的包进行安装,主要是为了省事儿,要不然还得找服务启动脚本等各种配置,内容里会记录 nginx 的配置文件内容;另如需要使用源码安装的话,想使用系统的服务启动,服务脚本在最后的“附件”里查看;

php:使用系统源中的包进行安装,主要是使用 php-fpm 方式,主要也是为了省事儿,看缺什么组件就直接安装就可以,当前源中的版本是 8.2.7 版本,这个不太建议使用源码安装,主要是太麻烦,不容易记录也不必要弄太麻烦;

nextcloud:使用的是当前最新的版本 28.0.4,数据库使用 sqlite 主要也是为了省事儿,也没特别大的数据,所以就不再另安装数据库系统;

二、系统配置

1、配置 debian12:

最小安装完成后(要注意安装过程中要选择安装 ssh 服务,要不然系统安装完成还得自己另安装,太麻烦),修改源到自己习惯的国内源,并更新到最新

apt update && apt upgrade -y

安装一些自己习惯使用工具,比如 neofetch、btop、net-tools 什么的

并将系统的的 IP 设置为固定即可,方式很多,我使用的是直接设置 interfaces 文件:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug enp1s0
# iface enp1s0 inet dhcp
iface enp1s0 inet static
        address X.X.X.X/24
        gateway X.X.X.X
        dns-nameservers X.X.X.X

重新网络服务以使其生效

systemctl restart networking.service

系统已经设置完毕;

2、安装配置 Nginx

apt install nginx        # 安装nginx

编写 nginx 配置文件(注意:按配置文件内容中可以看到 nextcloud 程序的位置是在/var/www/nextcloud 下,后面直接将 nextcloud 解压到/var/www/下即可)

vim /etc/nginx/sites-available/nextcloud

# 注:这是访问方式为 http://IP:PORT/ 的配置
server {
    listen 80;
    server_name X.X.X.X;

    root /var/www/nextcloud;
    index index.html index.htm index.php;

    # 下面这行是设置上传文件大小限制,0 不限制大小,100M 限制文件大小不超过 100M
    client_max_body_size 0;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # 要注意下面这行的接口的版本,版本要与 php 的版本一样,可以去指定的地址查看一下
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    # 解决 nextcloud 中的一个安全检查的 .mjs 问题
    location ~* \.mjs$ {
        types { }
        default_type application/javascript;
    }

    location ^~ /.well-known {
        # The rules in this block are an adaptation of the rules
        # in `.htaccess` that concern `/.well-known`.

        location = /.well-known/carddav { return 301 /remote.php/dav/; }
        location = /.well-known/caldav  { return 301 /remote.php/dav/; }

        location /.well-known/acme-challenge    { try_files $uri $uri/ =404; }
        location /.well-known/pki-validation    { try_files $uri $uri/ =404; }

        # Let Nextcloud's API for `/.well-known` URIs handle all other
        # requests by passing them to the front-end controller.
        return 301 /index.php$request_uri;
    }

    # Optional: set long EXPIRES header on static assets
    location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
        expires 30d;
        access_log off;
    }
}

或是使用 url 路径的方式使用 http://IP:PORT/nextcloud/ 的配置内容(使用这种安装的会在nextcloud页面安装完成后自动转到没有 url 地址的网址,进而导致无法打开,此时手工加上 url 地址就可以)

根据自己需要二选一即可

mkdir -p /var/www/app

tar xjvf nextcloud-28.0.4.tar.bz2 -C /var/www/app/        # 解压 nextcloud 到 app 目录中

chown -R www-data:www-data /var/www/app

# 使用 url 路径的方式使用 http://IP:PORT/nextcloud/ 的配置
server {
    listen 80;
    server_name X.X.X.X;

    root /var/www/app;
    index index.html index.htm index.php;

    # 下面这行是设置上传文件大小限制,0 不限制大小,100M 限制文件大小不超过 100M
    client_max_body_size 0;

    location /nextcloud {
        try_files $uri $uri/ /nextcloud/index.php$request_uri;
    }

    location ~ \.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # 要注意下面这行的接口的版本,版本要与 php 的版本一样,可以去指定的地址查看一下
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    # 解决 nextcloud 中的一个安全检查的 .mjs 问题
    location ~* \.mjs$ {
        types { }
        default_type application/javascript;
    }

    location ^~ /.well-known {
        # The rules in this block are an adaptation of the rules
        # in `.htaccess` that concern `/.well-known`.

        location = /.well-known/carddav { return 301 /nextcloud/remote.php/dav/; }
        location = /.well-known/caldav  { return 301 /nextcloud/remote.php/dav/; }
        
        location = /.well-known/webfinger  { return 301 /nextcloud/index.php/.well-known/webfinger; }
        location = /.well-known/nodeinfo  { return 301 /nextcloud/index.php/.well-known/nodeinfo; }

        location /.well-known/acme-challenge    { try_files $uri $uri/ =404; }
        location /.well-known/pki-validation    { try_files $uri $uri/ =404; }

        # Let Nextcloud's API for `/.well-known` URIs handle all other
        # requests by passing them to the front-end controller.
        return 301 /nextcloud/index.php$request_uri;
    }

    # Optional: set long EXPIRES header on static assets
    location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
        expires 30d;
        access_log off;
    }
}

创建一个软链接

ln -s /etc/nginx/sites-available/nextcloud /etc/nginx/sites-enabled/

systemctl restart nginx.service        # 重启nginx服务

3、安装配置 php

直接使用系统源中的版本就可以,当前版本是 8.2.7

# 安装 php 及 php-fpm
apt install php8.2-fpm -y

# 安装 nextcloud 所需要的必要 php 组件,可以在运行 nextcloud 时看提示所缺少的组件安装也可以
apt install php8.2-sqlite3 php8.2-zip php8.2-xml php8.2-mbstring php8.2-gd php8.2-curl -y

# 安装 nextcloud 正常使用时建议安装的 php 组件
apt install php8.2-gmp php8.2-bcmath php8.2-intl php8.2-imagick -y


# 按 nextcloud 建议修改 php-fpm 的配置
# 解决:PHP 内存限制低于建议值 512 MB
vim /etc/php/8.2/fpm/php.ini
# 搜索 memory_limit,并将原值 128M 修改为 512M 后保存
# 搜索 interned_strings_buffer,启用并将原值 8 修改为 16,因为这个有可能会被提示需进行优化

vim /etc/php/8.2/fpm/pool.d/www.conf
# 搜索 PATH,找到 env[PATH],= 的内容为在系统中的 PATH 值(echo $PATH 的结果)
# 例如:env[PATH] = /usr/local/bin:/usr/bin:/bin

# 重启服务使组件及配置生效
systemctl restart php8.2-fpm.service

4、安装 Nextcloud

可以直接到官网上下载就可以(这个是官方各个版本的下载地址,选一个自己喜欢的就可以)Index of /server/releases (nextcloud.com)icon-default.png?t=N7T8https://download.nextcloud.com/server/releases/我选择下载最新的版本

# 下载指定的版本包
wget https://download.nextcloud.com/server/releases/nextcloud-28.0.4.tar.bz2
# 解压到指定位置,也是按 nginx 的配置文件中所指的位置运行解压
tar xjvf nextcloud-28.0.4.tar.bz2 -C /var/www/
# 修改目标目录的所属权限
chown -R www-data:www-data /var/www/nextcloud/
# 此时输入 ip:port 就可以正常访问 nextcloud 第一运行的安装向导页面了
# 如果 php 组件没有安装完全,也会提示,按提示安装完全后,重启 php 服务并刷新页面即可
# 需要另找一个位置专门给 nextcloud 存数据文件使用,所有上传的文件都会保存在该目录下
# 数据目录的权限也是 www-data 用户的,例如:
mkdir /nextcloud-data
chown -R www-data:www-data /nextcloud-data/

5、配置 nextcloud

在安装完成后可以修改配置文件中的内容,修改一些设置

vim /var/www/nextcloud/config/config.php

# 如果有域名的可以在这段里添加
'trusted_domains' =>
  array (
    0 => '127.0.0.1:80',
    1 => 'www.abc.com:80',
  ),

# 解决默认电话区域和默认地域的提示,在配置文件中添加
 'default_phone_region' => 'CN',
 'default_language' => 'zh_CN',
 'default_locale' => 'zh',

附件一:

nginx 源码安装并使用服务启动管理所需使用脚本(暂未测试,但看着应该是可以正常使用)

vim /etc/init.d/nginx        # 创建服务启动脚本

#!/bin/sh
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO
 
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx
 
test -x $DAEMON || exit 0
 
case "$1" in
    start)
        echo -n "Starting $DESC: "
        $DAEMON
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        $DAEMON -s stop
        echo "$NAME."
        ;;
    reload)
        echo -n "Reloading $DESC configuration: "
        $DAEMON -s reload
        echo "$NAME."
        ;;
    restart)
        echo -n "Restarting $DESC: "
        $DAEMON -s quit
        $DAEMON
        echo "$NAME."
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|reload}" >&2
        exit 1
        ;;
esac
 
exit 0

chmod a+x /etc/init.d/nginx        # 添加可执行权限

update-rc.d nginx defaults        # 添加并更新到启动服务,使其可以自动启动

# 之后的启动服务操作

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx reload


  • 28
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值