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