使用vagrant搭建LNMP 环境

准备工作:
  1. 安装 OracleVMVirtualBox https://www.virtualbox.org/
  2. 安装 vagrant http://www.vagrantup.com/
  3. 镜像文件下载https://app.vagrantup.com/boxes/search

新建非中文目录,把镜像Ubuntu2004放入刚刚新建的目录

打开终端然后cd进入刚刚新建的目录

输入vagrant box add server Ubuntu2004 回车

使用vagrant box list 查看是否有名为server的box

看到有之后,我们执行vagrant init server 初始化 然后我们的工作目录就生成了一个vagrantfile的配置文件

因为我们虚拟机默认只分配一个访问外网的 IP, 项目使用的IP得手动设置打开 Vagrantfile文件,找到config.vm.network 'private_network',ip:'127.0.0.1'这一行设置一个私有 IP(我自己的在29行)并去掉注释

然后我们就可以使用vagrant up 启动 成功后会在当前目录生成一个.vagrant的隐藏文件

打开Oracle VM VirtualBox就发现server的虚拟机已经运行了

使用vagrant ssh直接连接到虚拟机里面 ls查看vagrant是同步本地的文件夹

vagrant box add NAME URL #添加一个box
vagrant box list #查看本地已添加的box
vagrant box remove NAME virtualbox #删除本地已添加的box,如若是版本1.0.x,执行vagrant box remove NAME(NAME是添加的box名字)
vagrant init NAME #初始化,实质应是创建Vagrantfile文件(NAME是添加的box名字)
vagrant up #启动虚拟机
vagrant halt #关闭虚拟机
vagrant destroy #销毁虚拟机
vagrant reload #重启虚拟机
vagrant package #当前正在运行的VirtualBox虚拟环境打包成一个可重复使用的box
vagrant ssh #进入虚拟环境


安装PHP7.4 之前,要先进行:

sudo vim /etc/apt/source.list 将里面的内容换成下面内容(换清华源,国内速度较快)

# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse

执行sudo apt-get update 命令更新

sudo apt-get install language-pack-en-base -y 解决语言冲突

安装完成之后,执行:sudo locale-gen en_US.UTF-8

安装 git ,vim: sudo apt-get install git vim

执行: sudo apt-get install software-properties-common 安装依赖包

执行:sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

再次:sudo apt-get update

然后就可以安装php7.4了:运行 apt-cache search php7.4

安装php7.4:sudo apt-get install php7.4 -y

执行:php -V 查看是否安装成功

继续安装php与mysql的通信模块:sudo apt-get install php7.4-mysql -y

然后安装fpm,这是nginx用来解析php文件的:sudo apt-get install php7.4-fpm -y

其他必备模块:sudo apt-get install php7.4-curl php7.4-xml php7.4-json php7.4-gd php7.4-mbstring -y

进入:sudo vim /etc/php/7.4/fpm/php.ini 将cgi.fix_pathinfo=1改为cgi.fix_pathinfo=0

进入:sudo vim /etc/php/7.4/fpm/pool.d/www.conf 将listen = /run/php/php7.4-fpm.sock修改为listen = 127.0.0.1:9000 然后重启

关闭:service php7.4-fpm stop
开启:service php7.4-fpm start


安装MySQL 5.7

运行:wget http://dev.mysql.com/get/mysql-apt-config_0.5.3-1_all.deb

然后使用dpkg命令添加MySQL的源:sudo dpkg -i mysql-apt-config_0.5.3-1_all.deb
选择 Server MySQL 5.7 Apply

执行:sudo apt-get update 更新

安装: sudo apt-get install mysql-server -y

卸载MySQL

sudo apt-get autoremove --purge mysql-server-5.0

sudo apt-get remove mysql-server

sudo apt-get autoremove mysql-server

sudo apt-get remove mysql-common 这个很重要上面的其实有一些是多余的

dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P清理残留数据


安装NGINX

安装:sudo apt-get install nginx -y

安装:sudo apt-get install curl -y

执行:curl localhost查看是否成功

卸载NGINX

执行:sudo apt-get --purge remove nginx
执行:sudo apt-get autoremove
执行:dpkg --get-selections|grep nginx
执行:罗列出与nginx相关的软件, nginx-common deinstall 然后
执行:sudo apt-get --purge remove nginx-common
这样就可以完全卸载掉nginx包括配置文件

安装组件:
sudo apt-get install unzip

sudo apt-get install zip

sudo apt-get install composer -y

进入:sudo vim /etc/php/7.4/fpm/php.inicgi.fix_pathinfo=1改为cgi.fix_pathinfo=0

进入:sudo vim /etc/php/7.4/fpm/pool.d/www.conflisten = /run/php/php7.4-fpm.sock修改为listen = 127.0.0.1:9000 然后重启

关闭:service php7.4-fpm stop
开启:service php7.4-fpm start

进入:sudo vim /etc/nginx/sites-available/default

将server这部分替换:

server {
    #listen 80 default_server;
    listen 80;
    #listen [::]:80 default_server ipv6only=on;

    root /var/www/your-project-name/public;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name lufficc.com www.lufficc.com;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.php?$query_string;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php7.4-cgi alone:
            fastcgi_pass 127.0.0.1:9000;
    #       # With php7.4-fpm:
    #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
}

root:是你的项目的public目录,也就是网站的入口
index:添加了index.php,告诉Nginx先解析index.php文件
server_name:你的域名,没有的话填写localhost
location / try_files修改为了try_files uriuri/ /index.php?query_string; location ~ \.php部分告诉Nginx怎么解析Php,原封不动复制即可,但注意:fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;的目录要和fpm的配置文件中的listen一致。

sudo service nginx restart(重启nginx)
sudo service php7.4-fpm restart(重启fpm)

内容来源:吕倡个人博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容