搭建php+宝塔面板+vagrant+laravel+Mac+centos7.2+Composer开发环境,vagrantlaravel


今天周末,在家闲着无事,琢磨良久,想着要做一个随时随地都可以用的开发环境,经过多方资料查询,整理出如下资料:

初始步骤:

1、下载virtualbox     Downloads – Oracle VM VirtualBox 

2、下载vagrant Download - Vagrant by HashiCorp

3、下载box 我下载的是centos7.2   可以根据自己的喜好去下载,推荐使用迅雷下载。A list of base boxes for Vagrant - Vagrantbox.es

4、建一个文件夹,用来专门存放此次工作需要的文件   我的目录是 ~/vagrant

5、这些都弄齐全后,安装好1,2步下载的,接下来都是命令行输入了。


安装步骤:

安装vagrantbox

1、打开mac终端:

输入:

vagrant -v
会输出:

Vagrant 2.0.1
如果报错,请重新安装第二步下载的文件,这里我是一步通过的

2、接下来进入我们建的相应文件夹中:

$ cd ~/vagrant
$ vagrant box add centos72 ~/Downloads/vagrant-centos-7.2.box
正常输出如下:

==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos72' (v0) for provider: 
    box: Unpacking necessary files from: file:///Users/***/Downloads/vagrant-centos-7.2.box
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!

3、之后输入:

$ vagrant init centos72

输出如下:

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

这时,文件夹‘~/vagrant’中应该存在‘Vagrantfile’文件

4、我们可以打开看看这个文件,并修改文件如下

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  # 这是端口映射模式      将主机的8080端口映射到虚拟机(也就是我们的box)的80端口

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
   config.vm.network "private_network", ip: "192.168.1.199"
  # 这是私有网络模式      将虚拟机做为你局域网中的一台机器看待   可以指定其ip   我用的是这种方式

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"
  # 这是公有网络模式      虚拟机享受实体机器一样的待遇,一样的网络配置

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end
上面有三处加粗的地方,建议详细看看,此处参考网址:Vagrant 三种网络配置详解 - CSDN博客

5、接下来只需要执行下面这行命令

$ vagrant up
输出如下:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos72'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1510451276448_1323
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.3.30
    default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
    default: /vagrant => /Users/***/vagrant

6、最后执行:

$ vagrant ssh
显示:
[vagrant@localhost ~]$ 

至此,主机上的操作全部完成,box环境已经搭好,如果是搭建php环境的,可以继续往下看,其他的环境,请自己斟酌。

安装宝塔面板

接下来我们先安装宝塔面板,详细教程 宝塔面板 - 简单好用的Linux/Windows服务器管理面板 此处不多说。

1、运行命令:

$ sudo su
$ yum install -y wget && wget -O install.sh http://download.bt.cn/install/install.sh && sh install.sh
这里根据个人网速与机器,需等待数分钟乃至数小时

运行停止后,会显示登录账号与密码,一般账号为admin ,密码为随机字符串,需要记录,下面需要使用账号密码。

2、输入:

$ cat /www/server/panel/data/port.pl
查看面板端口,一般默认为:8888

3、接下来输入

$ service bt start 
Starting Bt-Panel... Bt-Panel (pid 1244) already running
Starting Bt-Tasks... Bt-Tasks (pid 1567) already running
好了,面板安装好了,

4、在主机中打开浏览器,输入:http://192.168.1.199:8888

可以看到


5、输入账号密码


6、点击登录,进入后可以看到环境选择,选择其中一个进行安装即可:


7、接下来就等他自动安装,安装完成后再继续下面的操作。


安装composer

依次执行命令:

$ php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
$ sudo mv composer.phar /usr/local/bin/composer			#将composer设置为全局命令
$ composer selfupdate						#更新composer
此处参考:Packagist / Composer 中国全量镜像

安装laravel框架

$ composer create-project --prefer-dist laravel/laravel My***

进入laravel框架根目录,更改缓存文件目录权限

$ chmod -R 777 storage/

环境搭建过程中如果出现问题,请参考各资料博客:

感谢下面帖子的作者:

php错误提示 open_basedir restriction in effect 解决 - CSDN博客

使用国内镜像composer安装laravel - CSDN博客

Windows下Composer&Laravel的安装及配置 - 简书

mac os 禁止apache httpd自动启动 - 初级程序员的资料 - ITeye博客   apache自启动端口占用问题解决办法

Vagrant打造Laravel开发环境(LNMP) - 简书

vagrant系列一:vagrant的安装与初识 - CSDN博客

nginx+php使用open_basedir限制站点目录防止跨站



如遇到端口占用的情况:请先查看mac os 禁止apache httpd自动启动 - 初级程序员的资料 - ITeye博客 教程仍然被占用时,再使用下面的命令

$ sudo lsof -i :80
mac查询80端口占用情况,显示之后使用 

$ kill -9 [pid]

将进程杀死 (注意⚠️:[pid] 需要更改为具体数字)

最后,分享下我自制的box

链接:http://pan.baidu.com/s/1kV47g03  密码:0sd5

如果出现 open_basedir() 的错误,请参考:nginx+php使用open_basedir限制站点目录防止跨站

相关内容

    暂无相关文章