Once you have up and running Vagrant and VirtualBox in your machine you need to follow these steps to create a VM and install all necessary dependencies to run PHP Projects.
UPDATE: I have updated the vagrant file to use CentOS 9 Stream so we can install newer versions of PHP and NodeJs
Prerequisites:
Step 1. Go to the Directory where you have the Vagrantfile and then execute
$ vagrant suspend
this will stop the VM if it is running.
then with a text editor, replace the content of the Vagrantfile with this:
Vagrant.configure("2") do |config|
# VM with CentOS 9
config.vm.box = "martyV/centos9-stream"
# Use private network
config.vm.network "private_network", ip: "192.168.56.10"
# sync a project folder between host computer and VM
config.vm.synced_folder "./projects", "/var/www/projects"
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
sudo dnf -y update
sudo dnf -y install perl wget nano cronie zip unzip grubby gcc gcc-c++ kernel-devel make
sudo dnf module install nginx:1.24 -y
sudo dnf module install mariadb:10.11 -y
sudo dnf module install php:8.2 -y
sudo dnf module install nodejs:20 -y
sudo dnf -y install php-gd php-mysqlnd php-mbstring php-xml php-intl php-pdo php-soap php-opcache php-json php-cli php-devel php-odbc php-pecl-apcu php-pear php-zip
sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo rpm --import https://dl.yarnpkg.com/rpm/pubkey.gpg
sudo dnf -y install yarn
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
sudo mv composer.phar /usr/local/bin/composer
sudo chown -R root:nginx /var/lib/php
SHELL
end
Save the file.
Step 2. Execute
$ mkdir projects
$ vagrant up --provision
First you will create the projects folder where you will be sync'ing all your code between host machine and VM, then you wil lturn on and provision the VM. This process may take a few minutes in get done.
Step 2. Open a SSH Console to configure new provisioned Virtual Machine.
$ vagrant ssh
Step 3. Open up ports 80 and 443 to allow requests from the outside world into the VM.
$ sudo firewall-cmd --permanent --add-port=80/tcp
$ sudo firewall-cmd --permanent --add-port=443/tcp
$ sudo firewall-cmd --reload
$ # make sure the ports were added and loaded
$ sudo firewall-cmd --list-ports
you will see something like this:
Step 4. Tune VM to work with Enterprise Class PHP Projects.
$ sudo grubby --update-kernel=ALL --args="elevator=noop"
then
$ sudo nano /etc/sysctl.conf
remove all lines if any and replace with:
##### Performance kernel conf
## Ruben Elizondo
##
## restrict file swapping as much as possible. not recommended to set it to 0
vm.swappiness = 1
fs.file-max = 5000000
net.core.netdev_max_backlog = 400000
net.core.optmem_max = 10000000
net.core.rmem_default = 10000000
net.core.rmem_max = 10000000
net.core.somaxconn = 100000
net.core.wmem_default = 10000000
net.core.wmem_max = 10000000
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_congestion_control = bic
net.ipv4.tcp_ecn = 0
net.ipv4.tcp_max_syn_backlog = 12000
net.ipv4.tcp_max_tw_buckets = 2000000
net.ipv4.tcp_mem = 30000000 30000000 30000000
net.ipv4.tcp_rmem = 30000000 30000000 30000000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_wmem = 30000000 30000000 30000000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
kernel.shmmax=1073741824
kernel.msgmni=1024
kernel.sem=250 32000 32 1024
Save the file and then
$ sudo nano /etc/security/limits.conf
add this just right below #@student
* - nofile 16384
Save the file.
Then restart your VM to enable all these changes.
$ exit
$ vagrant suspend
$ vagrant up
Next Up:
- Configure MariaDB in a Vagrant Virtual Machine
- Configure Nginx Web Server with SSL in a Vagrant Virtual Machine