Windows11的WSL的子系统安装Magento2

1. 查看所有已安装的子系统列表

C:\Users\yangg>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-24.04    Stopped         2

C:\Users\yangg>

查看结果
命令会返回一个表格,包含以下几列信息:

  • NAME: 子系统的名称(例如 UbuntuDebian)。
  • STATE: 当前状态(Running 表示正在运行,Stopped 表示已停止)。
  • VERSION: 使用的 WSL 版本(2 或 1)。

2. 子系统更新

3. 启动进入子系统

在dows想使用命令:wsl 即可

C:\Users\yangg>wsl
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 6.6.87.2-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Wed Aug 27 19:03:55 CST 2025

  System load:  0.72               Processes:             133
  Usage of /:   3.0% of 250.92GB   Users logged in:       0
  Memory usage: 10%                IPv4 address for eth0: 172.23.123.99

删除旧的子系统

打开一个普通的 PowerShell 或 命令提示符窗口(不需要管理员权限)。首先,查看一下你要删除的系统的确切名称:

powershellwsl –list 这会列出所有已安装的发行版,例如:textUbuntu-24.04 Debian

使用 --unregister 命令来注销(即删除)该系统。将 <Distribution Name> 替换为你的系统名(比如 Ubuntu-24.04):powershell

C:\Users\yangg>wsl --unregister Ubuntu-24.04
正在注销。
操作成功完成。


C:\Users\yangg>

请注意: 系统名区分大小写,且必须和 wsl --list 列出的名称完全一致。如果你安装的是默认的Ubuntu,名字很可能就是 Ubuntu

执行命令后,你会看到类似 正在注销... 的提示。这个过程非常快,完成后该子系统及其所有数据就被彻底删除了。

验证是否删除成功:


再次运行 wsl --list --verbose,你应该看不到 Ubuntu-24.04 了。

C:\Users\yangg>wsl --list --verbose
适用于 Linux 的 Windows 子系统没有已安装的分发。
可通过安装包含以下说明的分发来解决此问题:

使用“wsl.exe --list --online' ”列出可用的分发
和 “wsl.exe --install <Distro>” 进行安装。

重新安装全新的子系统

过命令行安装

你也可以直接使用命令来安装默认的 Ubuntu 版本。

在 PowerShell 或命令提示符中运行:

user: yang

pwd:yang123456

C:\Users\yangg>wsl --install -d Ubuntu-24.04
正在下载: Ubuntu 24.04 LTS
正在安装: Ubuntu 24.04 LTS
已成功安装分发。可以通过 “wsl.exe -d Ubuntu-24.04” 启动它
正在启动 Ubuntu-24.04...
Provisioning the new WSL instance Ubuntu-24.04
This might take a while...
Create a default Unix user account: yang
New password:
Retype new password:
Sorry, passwords do not match.
passwd: Authentication token manipulation error
passwd: password unchanged
Try again? [y/N] Y
New password:
Retype new password:
passwd: password updated successfully
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

重置用户密码

wsl --distribution Ubuntu-24.04 --user root

在 root 命令行下,使用 passwd 命令后跟你忘记密码的用户名

C:\Users\yangg>wsl --distribution Ubuntu-24.04 --user root
Welcome to Ubuntu 24.04.3 LTS (GNU/Linux 6.6.87.2-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Wed Aug 27 19:35:01 CST 2025

  System load:  0.0                 Processes:             41
  Usage of /:   0.1% of 1006.85GB   Users logged in:       1
  Memory usage: 2%                  IPv4 address for eth0: 172.23.123.99
  Swap usage:   0%


This message is shown once a day. To disable it please create the
/root/.hushlogin file.
root@DESKTOP-C6K4RMT:/mnt/c/Users/yangg# passwd  yang
New password:
Retype new password:
passwd: password updated successfully
root@DESKTOP-C6K4RMT:/mnt/c/Users/yangg# exit
logout

安装nginx

sudo apt update

sudo apt install nginx

安装nginx 默认已经启动了

yang@DESKTOP-C6K4RMT:/mnt/c/Users/yangg$ sudo apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  nginx-common
Suggested packages:
  fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  nginx nginx-common
0 upgraded, 2 newly installed, 0 to remove and 9 not upgraded.
Need to get 564 kB of archives.
After this operation, 1596 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 nginx-common all 1.24.0-2ubuntu7.5 [43.4 kB]
Get:2 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 nginx amd64 1.24.0-2ubuntu7.5 [520 kB]
Fetched 564 kB in 3s (198 kB/s)
Preconfiguring packages ...
Selecting previously unselected package nginx-common.
(Reading database ... 40754 files and directories currently installed.)
Preparing to unpack .../nginx-common_1.24.0-2ubuntu7.5_all.deb ...
Unpacking nginx-common (1.24.0-2ubuntu7.5) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.24.0-2ubuntu7.5_amd64.deb ...
Unpacking nginx (1.24.0-2ubuntu7.5) ...
Setting up nginx-common (1.24.0-2ubuntu7.5) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
Setting up nginx (1.24.0-2ubuntu7.5) ...
 * Upgrading binary nginx                                                                                            [ OK ]
Processing triggers for man-db (2.12.0-4build2) ...
yang@DESKTOP-C6K4RMT:/mnt/c/Users/yangg$ service nginx start
Failed to start nginx.service: Interactive authentication required.
See system logs and 'systemctl status nginx.service' for details.
yang@DESKTOP-C6K4RMT:/mnt/c/Users/yangg$ service nginx stus
Usage: nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}
yang@DESKTOP-C6K4RMT:/mnt/c/Users/yangg$ service nginx status
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Wed 2025-08-27 19:40:55 CST; 4min 50s ago
       Docs: man:nginx(8)
    Process: 1014 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 1015 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 1062 (nginx)
      Tasks: 21 (limit: 18990)
     Memory: 14.8M (peak: 32.3M)
        CPU: 164ms
     CGroup: /system.slice/nginx.service
             ├─1062 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─1064 "nginx: worker process"
             ├─1065 "nginx: worker process"
             ├─1066 "nginx: worker process"
             ├─1067 "nginx: worker process"
             ├─1068 "nginx: worker process"
             ├─1069 "nginx: worker process"
             ├─1070 "nginx: worker process"
             ├─1071 "nginx: worker process"
             ├─1072 "nginx: worker process"
             ├─1073 "nginx: worker process"
             ├─1074 "nginx: worker process"
             ├─1075 "nginx: worker process"
             ├─1076 "nginx: worker process"
             ├─1077 "nginx: worker process"
             ├─1078 "nginx: worker process"
             ├─1079 "nginx: worker process"
             ├─1080 "nginx: worker process"
             ├─1081 "nginx: worker process"
             ├─1082 "nginx: worker process"
             └─1083 "nginx: worker process"

Aug 27 19:40:55 DESKTOP-C6K4RMT systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
Aug 27 19:40:55 DESKTOP-C6K4RMT systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.
yang@DESKTOP-C6K4RMT:/mnt/c/Users/yangg$

在 WSL 中安装了 Nginx 后,从宿主机(Windows)访问它非常简单。整个过程的核心是:WSL 2 虚拟机有一个被宿主机访问的独立 IP 地址

请按照以下步骤检查和操作:


第 1 步:启动 Nginx 服务

首先,确保你的 Nginx 已经在 WSL 中正常运行。

打开你的 WSL 终端(如 Ubuntu)。

使用以下命令启动 Nginx:bashsudo service nginx start # 或者 sudo systemctl start nginx(某些 WSL 发行版可能默认没有 systemctl,使用 service 命令更通用)

检查 Nginx 是否成功启动:bashsudo service nginx status如果看到 active (running) 的提示,说明 Nginx 已经在 WSL 内部正常运行了。


第 2 步:获取 WSL 2 的 IP 地址

这是最关键的一步。WSL 2 每次启动时都会从 Hyper-V 管理器获取一个动态的私有 IP 地址。

在你的 WSL 终端中,输入以下命令来查看这个 IP 地址:

bash

yang@DESKTOP-C6K4RMT:/mnt/c/Users/yangg$ hostname -I
172.23.123.99

或者

bash

ip addr show eth0 | grep inet

你会得到一个类似 172.25.123.99 的 IP 地址。记下这个地址(我们称之为 <WSL_IP>)。

例如,你的命令输出可能是:

text

172.28.123.99

第 3 步:从宿主机 Windows 访问

现在,你可以在宿主机 Windows 上做任何一件事来访问 Nginx:

  1. 打开浏览器(Chrome, Edge, Firefox 等)。
  2. 在地址栏中,输入你在上一步获取的 WSL IP 地址。texthttp://<WSL_IP>根据上面的例子,你应该输入:http://172.28.123.456
  3. 按回车键。

如果一切顺利,你应该能看到 Nginx 的默认欢迎页面,上面显示 “Welcome to nginx!”。

配置nginx站点 cd /etc/ngnix/sites-available nano magento.conf

cd /etc/ngnix/sites-available
nano magento.conf
----------

upstream fastcgi_backend {
server unix:/run/php/php8.1-fpm.sock;
}
server {
server_name magento.local.com;
listen 80;
set $MAGE_ROOT /var/www/html/magento2;
set $MAGE_MODE developer; # or production

access_log /var/log/nginx/magento2-access.log;
error_log /var/log/nginx/magento2-error.log;

include /var/www/html/magento2/nginx.conf.sample;
}
---------------

yang@DESKTOP-C6K4RMT:/etc/nginx$ cd sites-enabled/
yang@DESKTOP-C6K4RMT:/etc/nginx/sites-enabled$ ls -al
total 8
drwxr-xr-x 2 root root 4096 Aug 27 19:40 .
drwxr-xr-x 8 root root 4096 Aug 27 19:40 ..
lrwxrwxrwx 1 root root   34 Aug 27 19:40 default -> /etc/nginx/sites-available/default
yang@DESKTOP-C6K4RMT:/etc/nginx/sites-enabled$ sudo ln -s ../sites-available/magento.conf
yang@DESKTOP-C6K4RMT:/etc/nginx/sites-enabled$ ls -al
total 8
drwxr-xr-x 2 root root 4096 Aug 27 20:22 .
drwxr-xr-x 8 root root 4096 Aug 27 19:40 ..
lrwxrwxrwx 1 root root   34 Aug 27 19:40 default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root   31 Aug 27 20:22 magento.conf -> ../sites-available/magento.conf
yang@DESKTOP-C6K4RMT:/etc/nginx/sites-enabled$

安装mysql8.4

安装php8.4

首先,安装所有必要的依赖项:

sudo apt install curl gpg gnupg2 software-properties-common ca-certificates apt-transport-https lsb-release

将 PHP 存储库添加到您的系统:

sudo add-apt-repository ppa:ondrej/php

更新您的 APT 存储库。

sudo apt update

使用命令安装 PHP 8.4

	
sudo apt install php8.4

和包裹:

sudo apt install php8.4-{exif,bz2,intl,soap,cli,pdo,mysql,zip,gd,mbstring,curl,xml,bcmath,common,xmlrpc,gmp}

接下来,验证您的 PHP 版本:

php -v

您应该看到如下输出:

yang@DESKTOP-C6K4RMT:/var/www/html$ php -v
PHP 8.4.11 (cli) (built: Aug  3 2025 08:42:27) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.4.11, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.11, Copyright (c), by Zend Technologies

安装Composer2.8

安装OpenSearch 2.19.

在 WLS 中安装 OpenSearch 2.19.0

1. 更新系统并安装依赖

首先,确保您的系统是最新的并安装必要的工具(如 wget)和 Java。

sudo apt update && sudo apt upgrade -y
sudo apt install wget -y

2. 安装 Java (OpenJDK 11)

OpenSearch 2.19.0 需要 Java 11。

sudo apt install openjdk-11-jdk -y

安装后验证 Java 版本:

yang@DESKTOP-C6K4RMT:/var/www/html$ java -version
openjdk version "11.0.28" 2025-07-15
OpenJDK Runtime Environment (build 11.0.28+6-post-Ubuntu-1ubuntu124.04.1)
OpenJDK 64-Bit Server VM (build 11.0.28+6-post-Ubuntu-1ubuntu124.04.1, mixed mode, sharing)

3. 下载 OpenSearch 2.19.0

使用 wget 从官方仓库下载特定版本 2.19.0 的压缩包。

sudo wget https://artifacts.opensearch.org/releases/bundle/opensearch/2.19.0/opensearch-2.19.0-linux-x64.tar.gz

4. 提取归档文件

提取下载的文件并进入解压后的目录。

发表评论