如何在windows10的子系统(wsl)中安装php开发环境

aries 发表于 2021-04-06 523 次浏览 标签 : windows10wsllnmp

记录一下今天的安装过程

1.安装WSL

WSL(Windows Subsystem for Linux) 是Win10下面的linux子系统,与Win10共享端口,网络,文件和进程列表。现在已经到第二版,wsl2版本基于虚拟机的。目前win10系统默认的还是wsl1。

  1. 打开 控制面板->程序与功能->启用或关闭Windows功能。
  2. 选择 适用于Linux的Windows子系统
  3. 打开win10应用商店 搜索linux
  4. 选择 Ubuntu 18.04LTS 进行安装
  5. 安装完成后启动程序,输入账号密码,密码输入两遍。
  6. 等待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. 替换原来的内容为下面的

阿里云Ubuntu18.04

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

保存,执行命令

cd ~
sudo apt-get update && sudo apt-get upgrade

等待升级完成后,开始安装nginx

sudo apt install nginx
安装完成后测试运行
sudo service nginx start
没有问题的话,会出现

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.

安装mysql

sudo apt install mysql-server
sudo usermod -d /var/lib/mysql/ mysql
sudo service mysql start
sudo mysql_secure_installation

随意输入,安装完成后再修改一下mysql root的密码

sudo apt install mysql-server
sudo usermod -d /var/lib/mysql/ mysql
sudo service mysql start
sudo mysql_secure_installation

安装php7.2

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

好,下面开始网站配置

  1. 设置权限
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"
    echo "==========================="
    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 "==========================="
}
function nmp_stop(){
    echo "==========================="
    sudo /etc/init.d/nginx stop
    echo "==========================="
    echo -e "\033[32m nginx stopped \033[0m"
    echo "==========================="
    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 "==========================="
}
function nmp_restart(){
    echo "==========================="
    sudo /etc/init.d/nginx restart
    echo "==========================="
    echo -e "\033[32m nginx restarted \033[0m"
    echo "==========================="
    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 "==========================="
}

保存后,执行命令

source ~/.bashrc
npm_start # 开启nginx\php-fpm\mysql
npm_stop # 停止
npm_restart # 重启

0条评论

如需评论,请填写表单。
换一个

记住我的信息