如何在windows10的子系统(wsl)中安装php开发环境
记录一下今天的安装过程
1.安装WSL
WSL(Windows Subsystem for Linux) 是Win10下面的linux子系统,与Win10共享端口,网络,文件和进程列表。现在已经到第二版,wsl2版本基于虚拟机的。目前win10系统默认的还是wsl1。
- 打开 控制面板->程序与功能->启用或关闭Windows功能。
- 选择 适用于Linux的Windows子系统
- 打开win10应用商店 搜索linux
- 选择 Ubuntu 18.04LTS 进行安装
- 安装完成后启动程序,输入账号密码,密码输入两遍。
- 等待wsl安装完毕
2. 安装nmp(nginx、php、mysql)
1 .原文件备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
2. 编辑源列表文件
sudo vim /etc/apt/sources.list
3. 替换原来的内容为下面的
Ubuntu 20.04 国内源(清华)
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
22.04.2阿里源
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
保存,执行命令
cd ~
sudo apt-get update && sudo apt-get upgrade
等待升级完成后,开始安装nginx
sudo apt install nginx
安装完成后测试运行
sudo service nginx start
在浏览器中输入http://127.0.0.1
没有问题的话,会出现
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to [nginx.org](http://nginx.org/).
Commercial support is available at [nginx.com](http://nginx.com/).
Thank you for using nginx.
安装mysql8
sudo apt install mysql-server
sudo usermod -d /var/lib/mysql/ mysql
sudo service mysql start
sudo mysql_secure_installation
设置mysql root密码
前面几步随意输入,修改一下mysql root的密码
先在直接输入mysql
进入mysql中(最好切换到root环境变量中)
输入命令:SHOW VARIABLES LIKE 'validate_password%';
查看validate_password.length
相关的项
执行命令:set global validate_password.length=6;
再执行:set global validate_password.policy=LOW;
再执行命令:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '111111';
修改root密码为111111
配置文件:/etc/mysql/mysql.conf.d/mysqld.cnf
安装mysql5.7
下载apt仓库的安装包,Ubuntu的安装包是.deb文件
wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
#安装
dpkg -i mysql-apt-config_0.8.12-1_all.deb
弹出框中选择:ubuntu bionic (Ubuntu18.04系统的代号是bionic,选择18.04的版本库用来安装)
弹出框中选择:MySQL Server & Cluster
弹出框中选择:mysql-5.7
最后选择:ok
更新apt仓库的信息
首先导入仓库的密钥信息
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
更新仓库信息
apt update
检查是否成功配置MySQL5.7的仓库
apt-cache policy mysql-server
看到如图所示字样,即成功
安装MySQL5.7
使用apt安装mysql客户端和mysql服务端
apt install -f -y mysql-client=5.7* mysql-community-server=5.7*
弹出框中输入root密码并选择ok,密码任意,这里以123456代替
启动MySQL
/etc/init.d/mysql start # 启动
/etc/init.d/mysql stop # 停止
/etc/init.d/mysql status # 查看状态
对MySQL进行初始化
执行如下命令,此命令是MySQL安装后自带的配置程序
mysql_secure_installation
可以通过which命令查看到这个自带程序所在的位置
which mysql_secure_installation
/usr/bin/mysql_secure_installation
输入密码:
是否开启密码验证插件,如果需要增强密码安全性,输入y并回车,不需要直接回车(这里选择直接回车)
是否更改root密码,需要输入y回车,不需要直接回车(这里不更改)
是否移除匿名用户,移除输入y回车,不移除直接回车(这里选择移除)
是否进制root用户远程登录,禁止输入y回车,不禁止直接回车(这里选择不禁止)
是否移除自带的测试数据库,移除输入y回车,不移除直接回车(这里选择不移除)
完事儿
安装php7.2
-
删除php的相关包及配置
sudo apt-get autoremove php*
-
删除关联
sudo find /etc -name "*php*" |xargs rm -rf
-
清除dept列表
sudo apt purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.2 php7.2-fpm php7.2-mysql php7.2-curl php7.2-json php7.2-mbstring php7.2-xml php7.2-bcmath
sudo service php7.2-fpm start
安装composer
sudo apt install zip unzip php7.2-zip
sudo apt install composer
OK,nmp环境安装完成
下面开始设置开机启动
vim ~/.bashrc
找个合适的地方加入
# Start Nginx, PHP 7.2 and MySQL on boot
sudo /etc/init.d/nginx start
sudo /etc/init.d/php7.2-fpm start
sudo /etc/init.d/mysql start
sudo visudo
在最下面加入
# Programs that will start automatically
%sudo ALL=NOPASSWD: /etc/init.d/nginx
%sudo ALL=NOPASSWD: /etc/init.d/php7.2-fpm
%sudo ALL=NOPASSWD: /etc/init.d/mysql
ctrl+o保存
执行完“Ctrl+O”后,会输出”File Name to Write sudoers.tmp”,在tmp后执行 敲回车
执行“Ctrl+X”退出
好,下面开始网站配置
- 设置权限
sudo chown -R <your_username>:www-data /home/<your_username>/example
sudo chmod -R 777 /home/<your_username>/example
配置一下windows的hosts(C:\Windows\System32\drivers\etc\hosts)
127.0.0.1 example.local
为nginx配置虚拟主机
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.local
sudo nano /etc/nginx/sites-available/example.local
example.local的内容为
server {
listen 80;
server_name example.local;
root /home/<your_username>/example/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
保存
建立软连,这是必须的,不然会报403
sudo ln -s /etc/nginx/sites-available/example.local /etc/nginx/sites-enabled/example.local
重启nginx
注意:WSL下使用sock文件的方式访问会一直超时,需要在 /etc/nginx/nginx.conf 的 http block 中加入 fastcgi_buffering off;,原因不明,也是WSL的问题,在WSL的github中有issue提到过这个解决方案
sudo nginx -t
sudo service nginx restart
在浏览器中打开
http://example.local
大功告成?
安装imageMagick
1. sudo apt-get install imagemagick
2. sudo apt-get install php7.2-imagick
好,到这里,安装的步骤基本完成了,但是我们为了方便管理,我设置了几个命令来重启nginx\php-fpm\mysql
sudo vim ~/.bashrc
# 找到合适的地方写入
function nmp_start(){
echo "==========================="
sudo /etc/init.d/nginx start
echo "==========================="
echo -e "\033[32m nginx started \033[0m"
sudo /etc/init.d/php7.2-fpm start
echo "==========================="
echo -e "\033[32m php-fpm7.2 started \033[0m"
echo "==========================="
sudo /etc/init.d/mysql start
echo -e "\033[32m mysql started \033[0m"
echo "==========================="
sudo service redis-server start
echo -e "\033[32m redis started \033[0m"
echo "==========================="
}
function nmp_stop(){
echo "==========================="
sudo /etc/init.d/nginx stop
echo "==========================="
echo -e "\033[32m nginx stopped \033[0m"
sudo /etc/init.d/php7.2-fpm stop
echo "==========================="
echo -e "\033[32m php7.2-fpm stopped \033[0m"
echo "==========================="
sudo /etc/init.d/mysql stop
echo -e "\033[32m mysql stopped \033[0m"
echo "==========================="
sudo service redis-server stop
echo -e "\033[32m redis stopped \033[0m"
echo "==========================="
}
function nmp_restart(){
echo "==========================="
sudo /etc/init.d/nginx restart
echo "==========================="
echo -e "\033[32m nginx restarted \033[0m"
sudo /etc/init.d/php7.2-fpm restart
echo "==========================="
echo -e "\033[32m php-fpm7.2 restarted \033[0m"
echo "==========================="
sudo /etc/init.d/mysql restart
echo "==========================="
echo -e "\033[32m mysql restarted \033[0m"
echo "==========================="
sudo service redis-server restart
echo -e "\033[32m redis restarted \033[0m"
echo "==========================="
}
保存后,执行命令
source ~/.bashrc
npm_start # 开启nginx\php-fpm\mysql
npm_stop # 停止
npm_restart # 重启
安装nodejs
首先uname -a
看一下自己系统的版本
比如我的是x86_64
那么我就会下node官网下载:https://nodejs.org/download/release/v16.13.0/node-v16.13.0-linux-x64.tar.xz
下载完成后执行以下几个命令
tar -xvf node-v16.13.0-linux-x64.tar.xz
mv node-v16.13.0-linux-x64 nodejs
sudo ln -s /data/soft/nodejs/bin/npm /usr/local/bin/
sudo ln -s /data/soft/nodejs/bin/node /usr/local/bin/
安装cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
sudo ln -s /data/sof/nodejs/bin/cnpm /usr/local/bin
cnpm -v
安装redis-server
sudo apt-get install -y redis-server
配置文件
sudo vim /etc/redis/redis.conf
重启
service redis-server restart
安装php-redis扩展
sudo apt-get install php7.2-redis -y
完事儿