我需要协助来整理Vagrantfile。

我正在尝试在Windows桌面上设置虚拟机,以在现有的PHP / MySQL应用程序上工作。我被指示使用Vagrant和VirtualBox。我浏览了Vagrant的文档,发现其中的许多信息已经过时,这让我感到头疼。我对一般Linux的使用有一定的了解,但是对于在它们上设置LAMP服务器没有任何了解。

是)我有的:

  • 流浪汉2.1.2
  • VirtualBox 5.2.18

  • 我在VM中需要的东西:
  • Centos 6
  • Apache
  • MySQL 5.5
  • PHP 5.6
  • MySQLi / Mysqlnd(PDO可选)
  • 卷毛
  • DOM / SimpleXML
  • 典型的PHP应用程序
  • 需要的所有PHP扩展

    最佳答案

    花了几周的时间,过时了很多教程,但是我做到了。我正在分享,以便LAMP服务堆栈上的其他人有一个开始制作其vagrantfile的地方。请注意,这样做不是为了优雅,而是。这是一个快速入门,易于理解和适应自己的需求。

    与上面的原始帖子相比,有一些小的注释差异:

  • 我使用Ubuntu / bionic64而不是Centos 6,主要是因为我能够找到大量的教程 Material 。为CentOS 6修改此文件应该不难。 CentOS使用Yum而不是Apt-get进行软件包管理。我不完全确定还有什么不同。
  • 我使用PHP 7.2而不是5.6。
  • 我发现PHP 7.2随MySQLi一起提供,并且本机驱动程序已经开箱即用。

  • 包含工作中的vagrantfile和shell bootstrapper,对注释的理解度很高:https://pastebin.com/Eqvhq8KZ
    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    VAGRANTFILE_API_VERSION = '2'
    
    #########################################################################
    # VM Setup for LAMP stack application
    #
    # - Install PHP and packages
    # - Install Apache
    # - Configure /vagrant as document root
    # - Config PHP for development and logging
    # - Install MySQL
    # - Setup database and permissions (username and password are "vagrant")
    # - Install Composer
    #########################################################################
    
    @script = <<SCRIPT
    
    #################### PHP ####################
    apt-get install -y apt-utils php7.2 php7.2-bcmath php7.2-bz2 php7.2-cli php7.2-curl php7.2-intl php7.2-json php7.2-mbstring php7.2-opcache php7.2-soap php7.2-sqlite3 php7.2-xml php7.2-xsl php7.2-zip unzip
    
    #################### APACHE2 ####################
    apt-get install -y apache2 libapache2-mod-php7.2
    
    # Remove /var/www default
    rm -rf /var/www
    
    # Symlink /vagrant to /var/www
    ln -fs /vagrant /var/www
    
    # Add ServerName to httpd.conf
    echo "ServerName localhost" > /etc/apache2/httpd.conf
    
    # Setup hosts file
    VHOST=$(cat <<EOF
    <VirtualHost *:80>
        DocumentRoot "/vagrant"
        ServerName localhost
        <Directory "/vagrant">
            Options Indexes FollowSymLinks MultiViews Includes
            AllowOverride All
            Require all granted
            Order allow,deny
            Allow from all
            AddType text/html .shtm .shtml
            AddOutputFilter INCLUDES .htm .html .shtm .shtml
        </Directory>
    </VirtualHost>
    EOF
    )
    echo "${VHOST}" > /etc/apache2/sites-enabled/000-default.conf
    
    # Enable mod_rewrite
    a2enmod rewrite
    
    # Put PHP into development configuration
    mv /etc/php/7.2/apache2/php.ini /etc/php/7.2/apache2/php.ini.back
    cp /usr/lib/php/7.2/php.ini-development /etc/php/7.2/apache2/php.ini
    
    # Enable PHP extensions in php.ini
    #sed -i 's/;extension=mysqli/extension=mysqli/' /etc/php/7.2/apache2/php.ini
    
    # PHP will log its errors in a /log/error_log file
    sed -i 's:;error_log = php_errors.log:error_log = /vagrant/log/error_log:' /etc/php/7.2/apache2/php.ini
    
    # Restart apache
    systemctl restart apache2.service
    
    #################### MYSQL ####################
    apt-get install -y mysql-server mysql-client-core-5.7 php7.2-mysql
    
    systemctl start mysql.service
    
    # Reset root password
    #/usr/bin/mysqladmin -u root password 'root'
    mysqladmin -u root password 'root'
    
    # Setup database from root user and setup the application user
    mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS app"
    mysql -uroot -proot app < /vagrant/db/schema.sql
    mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* to 'vagrant'@'localhost' IDENTIFIED BY 'vagrant'"
    mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* to 'vagrant'@'%' IDENTIFIED BY 'vagrant'"
    mysql -uroot -proot -e "FLUSH PRIVILEGES"
    
    # Allow remote connections for MySQL Workbench
    MYSQLCONF=$(cat <<EOF
    
    [mysqld]
    bind-address        = 0.0.0.0
    EOF
    )
    echo "${MYSQLCONF}" >> /etc/mysql/my.cnf
    
    # Restart mysql
    /etc/init.d/mysql restart
    
    #################### COMPOSER ####################
    curl -sS http://getcomposer.org/installer | php
    mv composer.phar /usr/local/bin/composer
    
    # Run composer install
    cd /vagrant && composer install
    
    #################### FINISHED! ####################
    echo "** [PHP] Visit http://localhost:8080 in your browser for to view the application **"
    SCRIPT
    
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
        config.vm.box = 'ubuntu/bionic64'
    
        config.ssh.insert_key = false
    
        config.vm.network "forwarded_port", guest: 80, host: 8080
        config.vm.network "forwarded_port", guest: 443, host: 8081
        config.vm.network "forwarded_port", guest: 3306, host: 3307
    
        #config.vm.synced_folder '.', '/var/www/html'
    
        # Provision runs only on the first "Vangrant up" command
        config.vm.provision 'shell', privileged: true, inline: @script
    
        config.vm.provider "virtualbox" do |vb|
            vb.customize ["modifyvm", :id, "--memory", "2048"]
            vb.customize ['modifyvm', :id, "--natdnshostresolver1", "on"]
        end
    end
    

    关于vagrantfile - Centos 6 LAMP堆栈的Vagrantfile,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52170246/

    10-11 08:41