WSL2从零开始搭建网站开发环境系列02: 安装lnmp搭建网站开发环境

参考:https://lnmp.org/
代码:https://gitee.com/licess/lnmp
参考:https://github.com/linuxeye/lnmp

🐓提前准备

一:标题镜像换为国内

华为云参考:https://mirrors.huaweicloud.com/mirrorDetail/5ea14ecab05943f36fb75ee7

标题1、备份配置文件:

sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak

2、修改sources.list文件

http://archive.ubuntu.comhttp://security.ubuntu.com 替换成 http://mirrors.huaweicloud.com ,可以参考如下命令:

sudo sed -i "s@http://.*archive.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@http://mirrors.huaweicloud.com@g" /etc/apt/sources.list

3、更新索引

root@verygood:~# apt-get update
root@verygood:~# apt-get upgrade

4、路径映射(软链接)

root@verygood:~# sudo ln -s /mnt/e/各大网盘同步盘/阿里云盘19936926974/网站开发/1-clients /www/1-clients
root@verygood:~# sudo ln -s /mnt/e/各大网盘同步盘/阿里云盘19936926974/网站开发/2-in_progress /www/2-in_progress
root@verygood:~# sudo ln -s /mnt/e/各大网盘同步盘/阿里云盘19936926974/网站开发/3-study /www/3-study

二:保证github可访问

手工修改

参考:https://github.com/521xueweihan/GitHub520

下载最新host:从 https://raw.hellogithub.com/hostshttps://hosts.gitcdn.top/hosts.txt 下载最新hosts后修改本机host,刷新dns, ipconfig /flushdns

也可以从 https://tool.chinaz.com/dns/github.com 中挑一个ttl小的地址,一个一个地试,看哪个合适

使用SwitchHosts 工具

参考:https://zhuanlan.zhihu.com/p/629079933

使用 dev-sidecar

https://github.com/docmirror/dev-sidecar/releases

三:配置ssh

生成密钥

# 邮箱使用自己的啰
root@verygood:~# ssh-keygen -t rsa -C "xxxx@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:5LUEjAJHsOmcztPWcNmSu84QU2PIXr9EdVODBBfOXZ8 xxxx@qq.com
The key's randomart image is:
+---[RSA 3072]----+
|  o+o  o. oo*+o .|
|   =... .o =.o oo|
|  o o.= o o o .E.|
| o o + X o .     |
|  + = = S .      |
| o . * + .       |
|  + + o .        |
|   o o .         |
|     .+          |
+----[SHA256]-----+

在github配置SSH密钥,打开 https://github.com/settings/keys , 将 ~/.ssh/id_rsa.pub 中的内容拷贝到上面的配置中,名称为morefine-wsl2-ubuntu-2023-11-04

root@verygood:~/.ssh# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDMghnR3f2qpQAB5A63i2+bi+1rJOQqpBCYGhxSPpscv7jutGNASOMAnk4vFPtbyZWxxxxxxxxxxxxxxxxxxxxxxxxxKEfLR3s915LNRQqD2MgxCUtSFY8aRqGC0lqiNevxgWyvJPc1AiB9Vfi++O06YzLSCRtAFtK4ye8MlcM0b2WxSu5FwrNioNEpI1gyf5nm78zkQ5hPceH4MH1H2SHxHCHrPciFrOTC4yIbYA5FD15BmTrCxjb4gXkQ0YFFZYlZXRmM3NnR1xthR2DB+Yb5OTofte8ykeCXzbnNCDTLJxKuM8yktvaNWDdbdteq88UBIpuvvK9l7jDpLhhxfuADOAJHU8rCNWxl+XJMyURe+V7g2KHnXxn+WDM1WAsE9w7WYDss1l4vvC9gBGWT+ZyKoV0zgNQSQul1PSUDcS1cSIRt1HWjoEfFfF7uAV4DZwDk= xxxx@qq.com

检查

root@verygood:~/.ssh# ssh -T git@github.com
The authenticity of host 'github.com (140.82.112.3)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,140.82.112.3' (ECDSA) to the list of known hosts.
Hi youshengyouse! You've successfully authenticated, but GitHub does not provide shell access.

🐯在线安装

一:安装基础软件

apt install net-tools

二:安装lnmp

root@verygood:~# screen -S lnmp
root@verygood:~# wget https://soft.lnmp.com/lnmp/lnmp2.0.tar.gz -O lnmp2.0.tar.gz && tar zxf lnmp2.0.tar.gz && cd lnmp2.0 && ./install.sh lnmp
# 或改分单独输入命令
root@verygood:~# screen -S lnmp
root@verygood:~# wget https://soft.lnmp.com/lnmp/lnmp2.0.tar.gz -O lnmp2.0.tar.gz 
root@verygood:~# tar zxf lnmp2.0.tar.gz 
root@verygood:~# cd lnmp2.0 
# 下面这行要加上,否则安装要很久时间
# 参考:https://lnmp.org/faq/lnmp-download-source.html
root@verygood:~# sed -i 's/soft.lnmp.com/soft1.vpser.net/g' lnmp.conf 
root@verygood:~# ./install.sh lnmp

在这里插入图片描述

如果安装一直卡着,很慢,原因与 /etc/resolv.conf 配置有关,改为宿主机的ip就ok。
https://soft.lnmp.com/ 下载软件慢,就修改lnmp.conf
共花费10分钟
在浏览器中输入 127.0.0.1 ,显示如下
在这里插入图片描述

开启php执行函数

root@verygood:~/lnmp2.0/tools# ./remove_disable_function.sh

安装php扩展

参考 https://www.vpser.net/manage/lnmp-php-install-ext.html

root@verygood:~/lnmp2.0# ./addons.sh

+-----------------------------------------------------------------------+
|            Addons script for LNMP V2.0, Written by Licess             |
+-----------------------------------------------------------------------+
|    A tool to Install cache,optimizer,accelerator...addons for LNMP    |
+-----------------------------------------------------------------------+
|           For more information please visit https://lnmp.org          |
+-----------------------------------------------------------------------+
##### cache / optimizer / accelerator #####
  1: eAccelerator
  2: XCache
  3: Memcached
  4: opcache
  5: Redis
  6: apcu
##### Image Processing #####
  7: imageMagick
##### encryption/decryption utility for PHP #####
  8: ionCube Loader
  9: SourceGuardian Loader
##### PHP Modules/Extensions #####
 10: Exif
 11: Fileinfo
 12: Ldap
 13: Bz2
 14: Sodium
 15: Imap
 16: Swoole
#################################################
 exit: Exit current script
#################################################
Enter your choice (1, 2, 3, 4, 5, 6, 7, 8... or exit):

# 或指定
root@verygood:~/lnmp2.0# ./addons.sh install memcached
root@verygood:~/lnmp2.0# ./addons.sh install redis
root@verygood:~/lnmp2.0# ./addons.sh install imageMagick
root@verygood:~/lnmp2.0# ./addons.sh install exif 
# 注意上面不能写成 Exif

状态管理

参考:https://lnmp.org/faq/lnmp-status-manager.html

lnmp start
lnmp status

测试

  • http://127.0.0.1/p.php
  • http://127.0.0.1/phpinfo.php
  • http://127.0.0.1/phpmyadmin/

三:配置composer

提示:如果镜像源已改为国内,但还是很慢,请在dos下执行 ipconfig wsl config,看win10和ubuntu中的ip是否变动过,如果变动过,以管理员身份执行 wsl2_2023-10-23.bat

查看当前配置

composer config -l -g 查看所有全局配置
composer config -l 查看当前项目镜像配置

修改阿里淘宝镜像源

配置只在当前项目生效

composer config repo.packagist composer https://mirrors.aliyun.com/composer/

取消当前项目配置

composer config --unset repos.packagist

配置全局生效

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# 清理缓存

取消全局配置

composer config -g --unset repos.packagist

composer 的各类镜像源

composer Packagist
phpcomposer https://packagist.phpcomposer.com
aliyun https://mirrors.aliyun.com/composer
tencent https://mirrors.tencent.com/composer/
huawei https://mirrors.huaweicloud.com/repository/php
laravel-china https://packagist.laravel-china.org
cnpkg https://php.cnpkg.org

🦄安装nvm, node.js, npm和pnpm

安装nvm

参考:[微软官方文档 在 WSL 2 上设置 Node.js | Microsoft Learn](https://learn.microsoft.com/zh-cn/windows/dev-environment/javascript/nodejs-on-wsl

# 安装nvm之前确保github能正常访问
root@verygood:~# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
# 网速比较慢,费时

重启终端

root@verygood:~# command -v nvm
nvm
root@verygood:~# nvm --version
0.39.5
root@verygood:~# nvm list
            N/A
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)

安装node和npm

两个版本的nodejs都安装(也可只安装一个)

root@verygood:~# export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node/
root@verygood:~# nvm install --lts #安装lts版本
root@verygood:~# nvm install node  #再安装一个最新版本

指定node版本

root@verygood:~# nvm use --lts
Now using node v18.18.2 (npm v9.8.1)

查看所有nodejs版本

root@verygood:~# nvm ls
->      v20.9.0
        v21.1.0
default -> lts/* (-> v20.9.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v21.1.0) (default)
stable -> 21.1 (-> v21.1.0) (default)
lts/* -> lts/iron (-> v20.9.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.18.2 (-> N/A)
lts/iron -> v20.9.0

当前使用的nodejs和npm版本

root@verygood:~# node -v
v20.9.0
root@verygood:~# npm -v
10.1.0

npm镜像改为国内

root@verygood:~# npm config set registry https://registry.npm.taobao.org

npm国内其它镜像

# 华为 https://mirrors.huaweicloud.com/mirrorDetail/5ea0f1e03fdb11a72fd319b2
npm config set registry https://mirrors.huaweicloud.com/repository/npm/

# 阿里 https://npmmirror.com/
npm config set registry https://registry.npmmirror.com

# 腾讯 https://mirrors.cloud.tencent.com/help/npm.html
npm config set registry http://mirrors.cloud.tencent.com/npm/

# 清华
# 中科大

重新安装包

npm install -g rimraf
rimraf node_modules
rm package-lock.json
rm /root/.npmrc
npm cache clean --force

安装pnpm

参考:https://www.pnpm.cn/installation

root@verygood:~# npm install -g pnpm
配置pnpm
pnpm config get register
pnpm config set register http://registry.npm.taobao.org
pnpm config set store-dir "/www/pnpm-2024/.pnpm-store"
pnpm config set global-dir "/www/pnpm-2024/pnpm-global"
pnpm config set global-bin-dir "/www/pnpm-2024/nodejs/bin"
pnpm config set state-dir "/www/pnpm-2024/nodejs/pnpm"
pnpm config set cache-dir "/www/pnpm-2024/nodejs/pnpm/cache"

添加下面内容到~/.bashrc中

export PATH=/www/pnpm-2024/nodejs/bin:$PATH
root@verygood:~# vi .bashrc
root@verygood:~# source .bashrc

🦘补充

安装多个版本php

参考:http://www.wan-old.com/html/computer/linux/20230313/833.html

./install.sh mphp

php升级到指定版本

参考:https://lnmp.org/faq/lnmp1-2-upgrade.html

./upgrade.sh

安装最新php扩展

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php8.2-xdebug