适用于 Centos 6 LAMP 堆栈的 Vagrantfile

Vagrantfile for Centos 6 LAMP stack

我需要帮助来整理 Vagrantfile。

我正在尝试在我的 Windows 桌面上设置一个虚拟机来处理现有的 PHP/MySQL 应用程序。我被指示使用 Vagrant 和 VirtualBox。我一直在浏览 Vagrant 的文档,发现很多信息都过时了,这让我难以理解。我有一些一般 Linux 用法的背景,但是 none 在其上设置 LAMP 服务器。

我有:

我在 VM 中需要的东西:

花了几个星期的时间和大量的过时教程,但我完成了。我分享是为了让 LAMP 服务堆栈上的其他人有一个地方开始制作他们的 vagrantfile。请注意,这 不是 为优雅而做的。这是一个快速入门,易于理解并根据需要进行调整。

与上面的原文有一些小的差异post:

  • 我选择了 Ubuntu/bionic64 而不是 Centos 6,主要是因为我能够找到大量教程 material。为 CentOS 6 修改这个文件应该不会太难。 CentOS 使用 Yum 而不是 Apt-get 进行包管理。我不完全确定还有什么不同。
  • 我选择了 PHP 7.2 而不是 5.6。
  • 我发现 PHP 7.2 附带了 MySQLi 并且本机驱动程序已经开箱即用。

包括工作 vagrantfile 和 shell 引导程序,为了理解而大量评论: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