DevOps Programming Tools

Setting Up a LAMP Server Using Vagrant on a macOS Local Machine

Install and Configure Vagrant

To begin, ensure Vagrant is installed and configured on your macOS. You can download Vagrant from the official website. After installation, verify it by running:

vagrant --version

Install and Configure VirtualBox

Next, download and install VirtualBox from the official website. Verify the installation by running:

VBoxManage --version

Vagrant Configuration

Create a Vagrantfile to define the virtual machines and their configurations. Below is the complete code for setting up a LAMP server with two VMs: one for the web server and one for the database.

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

Vagrant.configure("2") do |config|

    config.vm.define "master" do |master|
        master.vm.hostname = "master"
        master.vm.box = "bento/ubuntu-18.04"    
        master.vm.network "private_network", ip: "192.168.100.100"

        master.vm.provider "virtualbox" do |vb|
            vb.memory = "2048"
            vb.cpus = 2
        end

        master.vm.provision "shell", inline: <<-SHELL
            sudo apt-get update -y
            sudo apt-get upgrade -y

            sudo apt-get install php7.2-cli php7.2-common php7.2-fpm php7.2-mysql php7.2-curl -y
            sudo apt-get install php7.2-json php7.2-cgi php7.2-gd php-pclzip php7.2-zip php7.2-xml -y
            sudo apt-get install php7.2 php-common php7.2-readline php7.2-mbstring php7.2-intl -y            
            sudo apt-get install gcc make autoconf libc-dev pkg-config -y
            sudo apt-get install nginx git curl vim libmcrypt-dev zip unzip -y                    
            sudo apt-get install software-properties-common -y

            sudo service nginx start
            sudo service php7.2-fpm start
            cd /vagrant
            curl -sS https://getcomposer.org/installer | php        
            sudo mv composer.phar /usr/local/bin/composer
            sudo chmod 755 /usr/local/bin/composer
            sudo cp ./default /etc/nginx/sites-enabled/default
            sudo service nginx restart
            sudo service php7.2-fpm restart
            sudo systemctl enable nginx
            sudo systemctl enable php7.2-fpm

            composer install --prefer-source
            composer update --prefer-source
            composer dump-autoload
            composer cache:clear
            composer cache-clear
            composer clear:cache
            composer clear-cache
            php artisan cache:clear
            php artisan config:cache
            php artisan route:cache
        SHELL

    end

    config.vm.define "db" do |db|
        db.vm.hostname = "db"
        db.vm.box = "bento/ubuntu-18.04"   

        db.vm.network "private_network", ip: "192.168.200.200"        
        db.vm.network "forwarded_port", guest: 3306, host: 3306

        db.vm.provider "virtualbox" do |vb|
            vb.memory = "2048"
            vb.cpus = 2
        end

        db.vm.provision "shell", inline: <<-SHELL
            sudo apt-get update -y
            sudo apt-get upgrade -y            

            sudo apt-get install software-properties-common -y          
            sudo ufw enable
            sudo ufw allow mysql 
            sudo ufw allow 3306/tcp
            sudo iptables -A INPUT -p tcp --destination-port 3306 -j ACCEPT
            debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
            debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'
            sudo apt-get install mysql-server mysql-client -y
            sudo sed -i -e 's/bind-address/#bind-address/g' /etc/mysql/mysql.conf.d/mysqld.cnf
            sudo sed -i -e 's/skip-external-locking/#skip-external-locking/g' /etc/mysql/mysql.conf.d/mysqld.cnf
            mysql -u root -proot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root'; FLUSH privileges;"
            sudo service mysql restart
        SHELL

    end

end

Learn how to set up a LAMP server using Vagrant on a macOS local machine. Follow detailed instructions to install and configure Vagrant and VirtualBox, and create a Vagrantfile to define virtual machines for a web server and a database server, including the necessary provisioning scripts to install and configure PHP, Nginx, MySQL, and other essential tools.

Ali Imran
Over the past 20+ years, I have been working as a software engineer, architect, and programmer, creating, designing, and programming various applications. My main focus has always been to achieve business goals and transform business ideas into digital reality. I have successfully solved numerous business problems and increased productivity for small businesses as well as enterprise corporations through the solutions that I created. My strong technical background and ability to work effectively in team environments make me a valuable asset to any organization.
https://ITsAli.com

Leave a Reply