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 status
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.4-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

通过官方APT仓库安装MySQL 8.4

这是首选方法,能确保你获取到最新的MySQL 8.4。

1. 更新系统包列表

首先确保你的WSL Ubuntu系统是最新的:

sudo apt update && sudo apt upgrade -y

2. 下载并安装MySQL APT仓库配置包

这会将MySQL的官方仓库添加到你的系统中:

wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb

sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb

在弹出的配置界面中,直接选择 Ok 确认即可,它会默认选择最新的MySQL系列。

此配置程序已确定您的系统上未配置 MySQL 服务器,并已高亮显示最合适的存储库软件包。如果您不确定要安装哪个版本,请勿更改自动选择的版本。高级用户可以随时根据需要更改版本。请注意,MySQL Cluster 也包含 MySQL 服务器。
您希望接收哪个服务器版本?

这里 我选择默认的。,操作之后,出下面界面

注意 在弹出的配置界面中,直接选择 Ok 确认即可,它会默认选择最新的MySQL系列。

yang@DESKTOP-C6K4RMT:/var/www/html$ sudo wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb
--2025-08-28 15:24:32--  https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb
Resolving dev.mysql.com (dev.mysql.com)... 23.57.138.234, 2600:140b:1a00:3ad::2e31, 2600:140b:1a00:39c::2e31
Connecting to dev.mysql.com (dev.mysql.com)|23.57.138.234|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://repo.mysql.com//mysql-apt-config_0.8.30-1_all.deb [following]
--2025-08-28 15:24:34--  https://repo.mysql.com//mysql-apt-config_0.8.30-1_all.deb
Resolving repo.mysql.com (repo.mysql.com)... 23.32.170.24, 2600:1406:5600:68f::1d68, 2600:1406:5600:680::1d68
Connecting to repo.mysql.com (repo.mysql.com)|23.32.170.24|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 18200 (18K) [application/x-debian-package]
Saving to: ‘mysql-apt-config_0.8.30-1_all.deb’

mysql-apt-config_0.8.30-1_all.deb                    100%[===================================================================================================================>]  17.77K  --.-KB/s    in 0.04s

2025-08-28 15:24:37 (461 KB/s) - ‘mysql-apt-config_0.8.30-1_all.deb’ saved [18200/18200]

yang@DESKTOP-C6K4RMT:/var/www/html$ sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb
Selecting previously unselected package mysql-apt-config.
(Reading database ... 44128 files and directories currently installed.)
Preparing to unpack mysql-apt-config_0.8.30-1_all.deb ...
Unpacking mysql-apt-config (0.8.30-1) ...
Setting up mysql-apt-config (0.8.30-1) ...

3. 刷新软件源并安装MySQL Server

sudo apt update

sudo apt install mysql-server -y

安装过程中,可能会提示你设置 root 用户的密码,请务必设置一个强密码并牢记

root用户密码: yang@123$%

4. 启动MySQL服务并设置开机自启

sudo service mysql start   # 立即启动MySQL
sudo service mysql status  # 检查运行状态,确认状态为 active (running)
yang@DESKTOP-C6K4RMT:/var/www/html$ sudo service mysql start
yang@DESKTOP-C6K4RMT:/var/www/html$ sudo service mysql status
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-08-28 15:51:35 CST; 1min 0s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
   Main PID: 20430 (mysqld)
     Status: "Server is operational"
      Tasks: 43 (limit: 18990)
     Memory: 422.4M (peak: 437.7M)
        CPU: 1.097s
     CGroup: /system.slice/mysql.service
             └─20430 /usr/sbin/mysqld

Aug 28 15:51:34 DESKTOP-C6K4RMT systemd[1]: Starting mysql.service - MySQL Community Server...
Aug 28 15:51:35 DESKTOP-C6K4RMT systemd[1]: Started mysql.service - MySQL Community Server.

5. 运行安全配置脚本(强烈建议)
这会引导你进行一些安全设置,如移除匿名用户、禁止root远程登录等:

sudo mysql_secure_installation

按照提示操作即可。在这里我是本地安装,所以不操作。如下有需要再操作

6. 验证MySQL 8.4安装

登录MySQL并检查版本:

yang@DESKTOP-C6K4RMT:/var/www/html$ mysql -u root -p -e "SELECT VERSION();"
Enter password:
+-----------+
| VERSION() |
+-----------+
| 8.4.6     |
+-----------+

安装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

用官方安装脚本(推荐)

这是最直接和官方推荐的方法,通常能自动安装最新稳定版(包括2.8)。

1.确保已安装 PHP 和必要的扩展:Composer 需要 PHP 运行。Magento 2.4.8 要求 PHP 8.4

sudo apt update
sudo apt install php-cli php-zip php-mbstring php-xml php-curl unzip -y

如下:安装后可以通过 php -v 确认版本。

yang@DESKTOP-C6K4RMT:/var/www/html$ sudo apt update
[sudo] password for yang:
Hit:1 http://repo.mysql.com/apt/ubuntu noble InRelease
Hit:2 http://security.ubuntu.com/ubuntu noble-security InRelease
Hit:3 http://archive.ubuntu.com/ubuntu noble InRelease
Hit:4 http://archive.ubuntu.com/ubuntu noble-updates InRelease
Hit:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble InRelease
Hit:6 http://archive.ubuntu.com/ubuntu noble-backports InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
yang@DESKTOP-C6K4RMT:/var/www/html$ sudo apt install php-cli php-zip php-mbstring php-xml php-curl unzip -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  zip
The following NEW packages will be installed:
  php-cli php-curl php-mbstring php-xml php-zip unzip
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 195 kB of archives.
After this operation, 446 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu noble-updates/main amd64 unzip amd64 6.0-28ubuntu4.1 [174 kB]
Get:2 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble/main amd64 php-cli all 2:8.4+96+ubuntu24.04.1+deb.sury.org+1 [4454 B]
Get:3 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble/main amd64 php-curl all 2:8.4+96+ubuntu24.04.1+deb.sury.org+1 [3984 B]
Get:4 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble/main amd64 php-mbstring all 2:8.4+96+ubuntu24.04.1+deb.sury.org+1 [4000 B]
Get:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble/main amd64 php-xml all 2:8.4+96+ubuntu24.04.1+deb.sury.org+1 [4010 B]
Get:6 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble/main amd64 php-zip all 2:8.4+96+ubuntu24.04.1+deb.sury.org+1 [3994 B]
Fetched 195 kB in 3s (64.1 kB/s)
Selecting previously unselected package php-cli.
(Reading database ... 44568 files and directories currently installed.)
Preparing to unpack .../0-php-cli_2%3a8.4+96+ubuntu24.04.1+deb.sury.org+1_all.deb ...
Unpacking php-cli (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Selecting previously unselected package php-curl.
Preparing to unpack .../1-php-curl_2%3a8.4+96+ubuntu24.04.1+deb.sury.org+1_all.deb ...
Unpacking php-curl (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Selecting previously unselected package php-mbstring.
Preparing to unpack .../2-php-mbstring_2%3a8.4+96+ubuntu24.04.1+deb.sury.org+1_all.deb ...
Unpacking php-mbstring (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Selecting previously unselected package php-xml.
Preparing to unpack .../3-php-xml_2%3a8.4+96+ubuntu24.04.1+deb.sury.org+1_all.deb ...
Unpacking php-xml (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Selecting previously unselected package php-zip.
Preparing to unpack .../4-php-zip_2%3a8.4+96+ubuntu24.04.1+deb.sury.org+1_all.deb ...
Unpacking php-zip (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Selecting previously unselected package unzip.
Preparing to unpack .../5-unzip_6.0-28ubuntu4.1_amd64.deb ...
Unpacking unzip (6.0-28ubuntu4.1) ...
Setting up php-xml (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Setting up php-curl (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Setting up unzip (6.0-28ubuntu4.1) ...
Setting up php-mbstring (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Setting up php-cli (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
update-alternatives: using /usr/bin/php.default to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar.default to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar.default to provide /usr/bin/phar.phar (phar.phar) in auto mode
Setting up php-zip (2:8.4+96+ubuntu24.04.1+deb.sury.org+1) ...
Processing triggers for man-db (2.12.0-4build2) ...
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
yang@DESKTOP-C6K4RMT:/var/www/html$

2. 下载并运行 Composer 安装脚本

# 下载安装脚本并运行安装
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# 执行安装,并指定安装到全局目录 /usr/local/bin 并命名为 composer
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
 sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

yang@DESKTOP-C6K4RMT:/var/www/html$ sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
All settings correct for using Composer
Downloading...

Composer (version 2.8.11) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer

3.(可选)验证安装脚本的哈希值:为了安全起见,你可以验证安装脚本。

 获取最新的安装脚本签名
EXPECTED_SIGNATURE=$(wget -q -O - https://composer.github.io/installer.sig)
# 验证你下载的安装脚本
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$EXPECTED_SIGNATURE') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

yang@DESKTOP-C6K4RMT:/var/www/html$ EXPECTED_SIGNATURE=$(wget -q -O - https://composer.github.io/installer.sig)
yang@DESKTOP-C6K4RMT:/var/www/html$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '$EXPECTED_SIGNATURE') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified

如果输出 “Installer verified”,则脚本 verified。

4. 清理安装脚本

yang@DESKTOP-C6K4RMT:/var/www/html$ sudo php -r "unlink('composer-setup.php');"
yang@DESKTOP-C6K4RMT:/var/www/html$ ls -al
total 941652
drwxr-xr-x  3 root root      4096 Aug 28 16:14 .
drwxr-xr-x  3 root root      4096 Aug 27 19:40 ..
-rw-r--r--  1 root root     10671 Aug 27 20:33 index.html
-rw-r--r--  1 root root       615 Aug 27 19:40 index.nginx-debian.html
-rw-r--r--  1 root root     18200 Apr 23  2024 mysql-apt-config_0.8.30-1_all.deb
drwxr-xr-x 10 yang yang      4096 Feb  8  2025 opensearch-2.19.0
-rw-r--r--  1 root root 964198305 Feb 12  2025 opensearch-2.19.0-linux-x64.tar.gz
yang@DESKTOP-C6K4RMT:/var/www/html$

5. 查看Composer版本

yang@DESKTOP-C6K4RMT:/var/www/html$ composer -v


/ / _
/ / / _
\/ _ `_ \/ _ \/ _ \/
/ _ \/ / / // // / / / / / / // / // (_ ) / / __/____// // // ./_/_/_// //
Composer version 2.8.11 2025-08-21 11:29:39

Usage:
command [options] [arguments]

Options:
-h, –help Display help for the given command. When no command is given display help for the list command
-q, –quiet Do not output any message
-V, –version Display this application version
–ansi|–no-ansi Force (or disable –no-ansi) ANSI output
-n, –no-interaction Do not ask any interactive question
–profile Display timing and memory usage information
–no-plugins Whether to disable plugins.
–no-scripts Skips the execution of all scripts defined in composer.json file.
-d, –working-dir=WORKING-DIR If specified, use the given directory as working directory.
–no-cache Prevent use of the cache
-v|vv|vvv, –verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
about Shows a short information about Composer
archive Creates an archive of this composer package
audit Checks for security vulnerability advisories for installed packages
browse [home] Opens the package’s repository URL or homepage in your browser
bump Increases the lower limit of your composer.json requirements to the currently installed versions
check-platform-reqs Check that platform requirements are satisfied
clear-cache [clearcache|cc] Clears composer’s internal package cache
completion Dump the shell completion script
config Sets config options
create-project Creates new project from a package into given directory
depends [why] Shows which packages cause the given package to be installed
diagnose Diagnoses the system to identify common errors
dump-autoload [dumpautoload] Dumps the autoloader
exec Executes a vendored binary/script
fund Discover how to help fund the maintenance of your dependencies
global Allows running commands in the global composer dir ($COMPOSER_HOME)
help Display help for a command
init Creates a basic composer.json file in current directory
install [i] Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json
licenses Shows information about licenses of dependencies
list List commands
outdated Shows a list of installed packages that have updates available, including their latest version
prohibits [why-not] Shows which packages prevent the given package from being installed
reinstall Uninstalls and reinstalls the given package names
remove [rm|uninstall] Removes a package from the require or require-dev
require [r] Adds required packages to your composer.json and installs them
run-script [run] Runs the scripts defined in composer.json
search Searches for packages
self-update [selfupdate] Updates composer.phar to the latest version
show [info] Shows information about packages
status Shows a list of locally modified packages
suggests Shows package suggestions
update [u|upgrade] Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file
validate Validates a composer.json and composer.lock
yang@DESKTOP-C6K4RMT:/var/www/html$

安装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. 提取归档文件

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

yang@DESKTOP-C6K4RMT:/var/www/html$ sudo tar -xzf opensearch-2.19.0-linux-x64.tar.gz
tar -xzf opensearch-2.19.0-linux-x64.tar.gzyang@DESKTOP-C6K4RMT:/var/www/html$ ls -al
total 941632
drwxr-xr-x  3 root root      4096 Aug 27 20:58 .
drwxr-xr-x  3 root root      4096 Aug 27 19:40 ..
-rw-r--r--  1 root root     10671 Aug 27 20:33 index.html
-rw-r--r--  1 root root       615 Aug 27 19:40 index.nginx-debian.html
drwxr-xr-x 10 yang yang      4096 Feb  8  2025 opensearch-2.19.0
-rw-r--r--  1 root root 964198305 Feb 12  2025 opensearch-2.19.0-linux-x64.tar.gz
yang@DESKTOP-C6K4RMT:/var/www/html$ cd opensearch-2.19.0/

5.配置系统限制 (重要!)

为了防止运行时错误,需要增加系统的内存映射限制。

yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ sudo sysctl -w vm.max_map_count=262144
vm.max_map_count = 262144

为了使此更改在重启后永久生效:

yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.conf
vm.max_map_count=262144

6.创建专用用户 (推荐)

出于安全考虑,不建议以 root 用户运行 OpenSearch。

yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ sudo adduser --system --group opensearch
[sudo] password for yang:
info: Selecting UID from range 100 to 999 ...

info: Selecting GID from range 100 to 999 ...
info: Adding system user `opensearch' (UID 106) ...
info: Adding new group `opensearch' (GID 110) ...
info: Adding new user `opensearch' (UID 106) with group `opensearch' ...
info: Not creating `/nonexistent'.
yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ id opensearch
uid=106(opensearch) gid=110(opensearch) groups=110(opensearch)
yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ pwd
/var/www/html/opensearch-2.19.0
yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ sudo chown -R opensearch:opensearch /var/www/html/opensearch-2.19.0
yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$

7. 进行基本配置

编辑 config/opensearch.yml 配置文件以适应单节点开发环境。

vim  config/opensearch.yml

找到并修改以下关键设置(取消注释并更改值):

cluster.name: my-opensearch-cluster # 给你的集群起个名字
node.name: node-1                   # 节点名
path.data: /path/to/data            # 数据目录路径 (可选,默认在解压目录内)
path.logs: /path/to/logs            # 日志目录路径 (可选,默认在解压目录内)
network.host: 0.0.0.0               # 允许所有IP访问,以便从Windows主机连接
http.port: 9200                     # HTTP端口
discovery.type: single-node         # 单节点模式,简化开发设置
# 由于是开发环境,可以禁用安全插件(生产环境绝不要这样做!)
plugins.security.disabled: true

修改如下

cluster.name: my-application-magento248   # 给你的集群起个名字
node.name:  node-1-magento248                  # 节点名
path.data: /var/www/html/opensearch-2.19.0/data            # 数据目录路径 (可选,默认在解压目录内)
path.logs:/var/www/html/opensearch-2.19.0/logs          # 日志目录路径 (可选,默认在解压目录内)
network.host: 0.0.0.0               # 允许所有IP访问,以便从Windows主机连接
http.port: 9200                     # HTTP端口
下面配置没有找到,手动添加。
discovery.type: single-# 单节点模式,简化开发设置
# 由于是开发环境,可以禁用安全插件(生产环境绝不要这样做!)  手动添加代码
plugins.security.disabled: true
/var/www/html/opensearch-2.19.0/logs

path.data: /path/to/data # 数据目录路径 (可选,默认在解压目录内)

创建在opensearch里面创建data目录

yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ sudo mkdir data
yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ ls -al
total 980
drwxr-xr-x 11 opensearch opensearch   4096 Aug 28 16:48 .
drwxr-xr-x  3 root       root         4096 Aug 28 16:14 ..
-rw-r--r--  1 opensearch opensearch  11358 Feb  5  2025 LICENSE.txt
-rw-r--r--  1 opensearch opensearch 924056 Feb  5  2025 NOTICE.txt
-rw-r--r--  1 opensearch opensearch   3600 Feb  5  2025 README.md
drwxr-xr-x  3 opensearch opensearch   4096 Feb  8  2025 bin
drwxr-xr-x  9 opensearch opensearch   4096 Aug 28 16:32 config
drwxr-xr-x  2 root       root         4096 Aug 28 16:48 data
drwxr-xr-x  9 opensearch opensearch   4096 Feb  5  2025 jdk
drwxr-xr-x  3 opensearch opensearch   4096 Feb  5  2025 lib
drwxr-xr-x  2 opensearch opensearch   4096 Feb  5  2025 logs
-rw-r--r--  1 opensearch opensearch  10041 Feb  8  2025 manifest.yml
drwxr-xr-x 22 opensearch opensearch   4096 Feb  5  2025 modules
-rwxr-xr-x  1 opensearch opensearch   2855 Feb  8  2025 opensearch-tar-install.sh
drwxr-xr-x  5 opensearch opensearch   4096 Feb  8  2025 performance-analyzer-rca
drwxr-xr-x 26 opensearch opensearch   4096 Feb  8  2025 plugins
yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ sudo chown -R opensearch:opensearch /var/www/html/opensearch-2.19.0
yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$ ls -al
total 980
drwxr-xr-x 11 opensearch opensearch   4096 Aug 28 16:48 .
drwxr-xr-x  3 root       root         4096 Aug 28 16:14 ..
-rw-r--r--  1 opensearch opensearch  11358 Feb  5  2025 LICENSE.txt
-rw-r--r--  1 opensearch opensearch 924056 Feb  5  2025 NOTICE.txt
-rw-r--r--  1 opensearch opensearch   3600 Feb  5  2025 README.md
drwxr-xr-x  3 opensearch opensearch   4096 Feb  8  2025 bin
drwxr-xr-x  9 opensearch opensearch   4096 Aug 28 16:32 config
drwxr-xr-x  2 opensearch opensearch   4096 Aug 28 16:48 data
drwxr-xr-x  9 opensearch opensearch   4096 Feb  5  2025 jdk
drwxr-xr-x  3 opensearch opensearch   4096 Feb  5  2025 lib
drwxr-xr-x  2 opensearch opensearch   4096 Feb  5  2025 logs
-rw-r--r--  1 opensearch opensearch  10041 Feb  8  2025 manifest.yml
drwxr-xr-x 22 opensearch opensearch   4096 Feb  5  2025 modules
-rwxr-xr-x  1 opensearch opensearch   2855 Feb  8  2025 opensearch-tar-install.sh
drwxr-xr-x  5 opensearch opensearch   4096 Feb  8  2025 performance-analyzer-rca
drwxr-xr-x 26 opensearch opensearch   4096 Feb  8  2025 plugins
yang@DESKTOP-C6K4RMT:/var/www/html/opensearch-2.19.0$

设置数据目录

path.data: /var/www/html/opensearch-2.19.0/data # 数据目录路径 (可选,默认在解压目录内)

如果禁用安全插件,则无需用户名密码即可访问。如果保持启用(默认),初始用户为 admin,密码需要在首次启动时从终端输出或日志中获取。

发表评论