Windows wsl2 lnmp环境 搭建Nextcloud笔记


一、lnmp环境搭建

系统选用wsl2 ubuntu(wsl1 php7.4加载慢搭载完成后首页加载不出来可能是linxu内核与兼容性问题)

微软商店下载,不带版本号版本,终端查看版本:Ubuntu20.04.3 LTS (Focal Fossa),

 

 

 

1.配置ssh

SSH配置过程:

(1)修改root密码

passwd 

(2)删除原ssh

apt remove openssh-server -y

(3)重新安装ssh服务

apt install openssh-server ssh -y

(4)编辑sshd_config文件

vi /etc/ssh/sshd_config

 

#sshd_config需修改内容

#以下几行需要删除#号
Port 22              #端口可自定
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

#以下内容no改为yes
PermitRootLogin yes # 需要用 root 直接登录系统此处no改为 yes
PasswordAuthentication yes # 将 no 改为 yes 表示使用帐号密码方式登录

(5)重新启动ssh

service ssh restart

 

WSL常用命令

debian config --default-user root #设置默认账户root

debian -c "service ssh start"     #ssh开机自启,需新建bat放到启动目录

wsl --shutdown    关闭

 

wsl镜像文件位置

C:\users\用户名\appdata\local\packages\canonicalgrouplimited.ubuntuonwindows_79rhkp1fndgsc\localstate\rootfs\

 

2.安装lnmp

apt install nginx -y
apt install mysql-server mysql-client -y
apt install php -y
apt install php-fpm  -y

(1)版本情况:

nginx version: nginx/1.18.0 (Ubuntu)

PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )

mysql  Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

(2)安装php扩展,这是 Nextcloud 核心系统安装软件包,如果计划运行其他应用程序,需要安装其他扩展:https://docs.nextcloud.com/server/19/admin_manual/installation/source_installation.html#prerequisites-label

apt install php7.4-gd php7.4-mysql php7.4-curl php7.4-mbstring php7.4-intl
apt install php7.4-gmp php7.4-bcmath php-imagick php7.4-xml php7.4-zip

 (3)必备:安装数据库插件

apt install php7.4-sqlite3 -y      #>= 3,出于性能原因官网不推荐,但个人用完全足够
apt install php7.4-mysql         #安装mysql驱动插件
也可以用下边命令
apt install php-pdo-mysql -y #安装mysql驱动插件

不想折腾mysql可以直接安装sqlite也够用

未安装php模块提示:

 

 

 

二、安装nextcloud

1.下载zip包

下载完成后winscp上传至/var/www目录

nextcloud下载地址:https://nextcloud.com/install/#instructions-server

或者直接cd /var/www, wget https://download.nextcloud.com/server/releases/nextcloud-23.0.0.zip

 

2.解压

xshell或者putty登录服务器安装unzip,解压至当前目录,并设置文件夹的访问权限可以被nginx访问

apt install unzip -y
cd /var/www 
unzip nextcloud
-23.0.0.zip
chown -R www-data:www-data /var/www/nextcloud

如果chown赋权限出错出错需新增www-data用户和组

useradd www-data
groupadd -g  www-data www-data

 

3.修改nginx配置文件/etc/nginx/sites-available/default,局域网访问可不开启ssl,配置参考官网。

upstream php-handler {
    #server 127.0.0.1:9000;
    server unix:/var/run/php/php7.4-fpm.sock;#php7.4默认监听方式
}


server {
    listen 80;
    listen [::]:80;
    
    # Use Mozilla's guidelines for SSL/TLS settings
    # https://mozilla.github.io/server-side-tls/ssl-config-generator/
    # NOTE: some settings below might be redundant
    #ssl_certificate /etc/ssl/nginx/cloud.example.com.crt;
    #ssl_certificate_key /etc/ssl/nginx/cloud.example.com.key;

    # Add headers to serve security related headers
    # Before enabling Strict-Transport-Security headers please read into this
    # topic first.
    #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
    #
    # WARNING: Only add the preload option once you read about
    # the consequences in https://hstspreload.org/. This option
    # will add the domain to a hardcoded list that is shipped
    # in all major browsers and getting removed from this list
    # could take several months.
    add_header Referrer-Policy "no-referrer" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Download-Options "noopen" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Permitted-Cross-Domain-Policies "none" always;
    add_header X-Robots-Tag "none" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Remove X-Powered-By, which is an information leak
    fastcgi_hide_header X-Powered-By;

    # Path to the root of your installation
    root /var/www/nextcloud;#nginx的root路径

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # The following 2 rules are only needed for the user_webfinger app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

    # The following rule is only needed for the Social app.
    # Uncomment it if you're planning to use this app.
    #rewrite ^/.well-known/webfinger /public.php?service=webfinger last;

    location = /.well-known/carddav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }

    # set max upload size
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # Enable gzip but do not remove ETag headers
    gzip on;
    gzip_vary on;
    gzip_comp_level 4;
    gzip_min_length 256;
    gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
    gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;

    # Uncomment if your server is build with the ngx_pagespeed module
    # This module is currently not supported.
    #pagespeed off;

    location / {
        rewrite ^ /index.php;
    }

    location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
        deny all;
    }
    location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) {
        deny all;
    }

    location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) {
        fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
        set $path_info $fastcgi_path_info;
        try_files $fastcgi_script_name =404;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $path_info;
        #fastcgi_param HTTPS on;
        # Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        # Enable pretty urls
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) {
        try_files $uri/ =404;
        index index.php;
    }

    # Adding the cache control header for js, css and map files
    # Make sure it is BELOW the PHP block
    location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
        try_files $uri /index.php$request_uri;
        add_header Cache-Control "public, max-age=15778463";
        # Add headers to serve security related headers (It is intended to
        # have those duplicated to the ones above)
        # Before enabling Strict-Transport-Security headers please read into
        # this topic first.
        #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
        #
        # WARNING: Only add the preload option once you read about
        # the consequences in https://hstspreload.org/. This option
        # will add the domain to a hardcoded list that is shipped
        # in all major browsers and getting removed from this list
        # could take several months.
        add_header Referrer-Policy "no-referrer" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header X-Download-Options "noopen" always;
        add_header X-Frame-Options "SAMEORIGIN" always;
        add_header X-Permitted-Cross-Domain-Policies "none" always;
        add_header X-Robots-Tag "none" always;
        add_header X-XSS-Protection "1; mode=block" always;

        # Optional: Don't log access to assets
        access_log off;
    }

    location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
        try_files $uri /index.php$request_uri;
        # Optional: Don't log access to other assets
        access_log off;
    }
}
View Code

如需开启ssl参考:https://docs.nextcloud.com/server/19/admin_manual/installation/nginx.html

 

4.nginx(vi /etc/nginx/nginx.conf)默认user是www-data不需要修改,其他配置暂不修改。

user www-data;

 

5.php7.4(vi  /etc/php/7.4/fpm/pool.d/www.conf)默认user、group是www-data不需要修改,其他配置暂不修改

user = www-data
group = www-data

 

三、启动lnmp,配置nextcloud

1.lnmp启动

service  nginx start                  #启动nginx
service php7.4-fpm start              #启动php            
service mysql start                   #启动mysql

 

2.mysql配置

mysql -uroot -p

出现mysql[root]提示后输入以下行,将用户名'nextcloud'@'localhost'中nextcloud和密码123456替换为适当的值,并使用回车键确认,注意最后加上分号:

CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY '123456';
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost'; FLUSH PRIVILEGES;

通过输入以下命令退出mysql:

quit;

 

3.本地浏览器输入localhost

 

 

 

 

 

四、常见问题

1.内部服务器错误

以下三种问题主要是nginx无法访问www目录,chown -R www-data:www-data /var/www/nextcloud赋予权限后解决

 

 

 

 

2.mysql 启动报错

mysql 启动报错:su: warning: cannot change directory to /nonexistent: No such file or directory

这种错误一般是 mysql 服务器异常关机导致的,解决方案如下:

# Ubuntu
sudo service mysql stop
sudo usermod -d /var/lib/mysql/ mysql sudo service mysql start
# CentOS
sudo systemctl stop mysql.service
sudo usermod -d /var/lib/mysql/ mysql sudo systemctl start mysql.service

 

 3.navicat连接不上数据库问题

是因为mysql8.0登录验证的默认插件变更了,从mysql_native_password变为auth_socket,需改为密码方式

(1)登陆数据库

mysql -uroot -p

(2)再输入root的密码:

(3)更改加密方式:

 ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;

(4)更改密码:该例子中 123456为新密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

用update改密码会报错

update mysql.user set authentication_string=password('123456') where user='root';

(5)刷新:

FLUSH PRIVILEGES;

 

4.忘记密码重置密码方法

# 停止mysql服务

sudo service mysql stop

# 修改MySQL的登录设置,暂时不校验登陆密码

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

# 将[mysqld]下的“skip-external-locking”注释掉

# 添加 “skip-grant-tables” 然后保存退出

# 重启mysql数据库

sudo service mysql restart

# 免密登陆mysql数据库

mysql -uroot -p

#打开 skip-grant-tables 这个参数后,进去要刷新授权表:

FLUSH PRIVILEGES;       #否则无法更改密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';   #更改密码

FLUSH PRIVILEGES;           #再次刷新:

 

5.打开缓慢、卡顿优化

 安装PHP的APCu+Redis扩展

安装apcu,apcu_bc 扩展

sudo apt-get install php-apcu php-apcu-bc

安装redis

apt-get install redis-server        #安装redis

apt install php-redis                #安装php-redis驱动

安装完毕后,打开nextcloud目录/config/config.php,在其尾部添加以下代码

'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Redis',
'redis' => [
    'host' => '127.0.0.1',
    'port' => 6379,
],
'memcache.locking' => '\OC\Memcache\Redis',

 6.上传速度优化、上传文件大小设置

(1)上传速度优化

进入nextcloud安装目录关闭上传分块

sudo -u www-data php occ config:app:set files max_chunk_size --value 0

 

也可以打开  /nextcloud/apps/files/lib/App.php  将其中10*1024*1024改为0*1024*1024

$maxChunkSize = (int)\OC::$server->getConfig()->getAppValue('files', 'max_chunk_size', 0 * 1024 * 1024);

 

occ命令出现错误1(开启APCu+Redis优化后)

nextcloud不能执行occ命令,报错如下:

OCP\HintException: [0]: Memcache \OC\Memcache\APCu not available for local cache (Is the matching PHP module installed and enabled?)

解决方法是开启apcu命令行,在/etc/php/7.4/fpm/conf.d/20-apcu.ini  文件中末尾添加 

apc.enable_cli=1

重启php问题解决

 

occ命令出现错误2

PHP Fatal error: Allowed memory size of 2097152 bytes exhausted (tried to allocate 438272 bytes) in /var/www/nextcloud/3rdparty/composer/autoload_real.php on line 37

将  /etc/php/7.4/fpm  文件夹中  php.ini  复制到  /etc/php/7.4/cli  中覆盖原文件重启php

 

(1)上传文件大小设置参考官网:

https://docs.nextcloud.com/server/latest/admin_manual/configuration_files/big_file_upload_configuration.html?highlight=php%20timeout#adjust-chunk-size-on-nextcloud-side

五、常用命令

更新

sudo apt update      #更新源
sudo apt upgrade    #升级已安装的所有软件包

aptitude update //查看所有可用包列表

aptitude search nginx //查看此时所有可用的nginx包

 

lnmp命令

nginx -t    #检查配置文件

service nginx start      #nginx启动

service nginx stop      #nginx停止

service nginx restart      #nginx重新启动

service nginx status     #查看状态

 

service php7.4-fpm start     #php启动

service php7.4-fpm stop     #php停止

service php7.4-fpm restart    #php重新启动

php -v      #php版本查看

php -m    # php 加载的模块 


service mysql start                #mysql启动

service mysql stop                #mysql停止

service mysql restart            #mysql重新启动

service mysql status   #查看mysql状态

service --status-all     #显示所有服务的状态

可以将service命令替换为/etc/init.d/nginx stop (部分linux的版本不支持service)

service service_name --full-restart  表示的是先执行 service service_name stop,然后执行service service_name start

 

数据库操作

use mysql;

select user,plugin from user where user ='root';                      # -- 查询root账号加密方式

select host,user,plugin from user;                                            #-- 查询所有账号加密方式

select user,host from user;                                                      #远程访问权限查看,先选择数据库,查看

 

卸载mysql

# 卸载mysql:

sudo apt-get autoremove mysql* --purge

sudo apt-get remove mysql-server

sudo apt-get remove mysql-common

# 清理残留数据

sudo dpkg -l |grep mysql|awk '{print $2}' |sudo xargs dpkg -P

sudo rm -rf /etc/mysql/

sudo rm -rf /var/lib/mysql

# 检查是否删除完毕

whereis mysql

sudo find / -name mysql

 

 

参考:

https://dwurl.cn/rIpzJC

http://dreamphp.cn/blog/detail?blog_id=18713

https://www.cnblogs.com/qumogu/p/12734694.html

https://www.cnblogs.com/cnwcl/p/13805643.html

https://blog.csdn.net/LJFPHP/article/details/78670083

https://www.cnblogs.com/vivih-y/p/14597235.html

https://www.linuxidc.com/Linux/2019-06/158958.htm

https://blog.csdn.net/cruiserblog/article/details/106934570

https://www.cnblogs.com/lian95/archive/2020/12/17/14140838.html

http://dreamphp.cn/blog/detail?blog_id=18713

https://www.jb51.net/article/142025.htm

https://blog.csdn.net/weixin_39793576/article/details/111521254

https://www.cnblogs.com/thespace/p/12445252.html

https://blog.csdn.net/i12344/article/details/107712473

https://www.timeit.cn/post-534.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM