PHP 8 is generally available since 26th November and now it’s the time to start considering the migration to the latest php version. I’m here just recoding the steps for building a vagrant based simple dev environment for php 8.0.
Final code can be fond in this repo: https://github.com/thilinah/php8-vagrant
Add a Vagrantfile under your project to start with debian linux.
Vagrant.configure("2") do |config| config.vm.box = "debian/buster64" end
Assuming we already have Vagrant installed, should run vagrant up first
vagrant up
This creates a running vagrant machine, which you can ssh into. Ssh into the machine and update all required certificates.
vagrant ssh sudo apt-get update
Download the Sury PPA for PHP 8 package
sudo apt -y install lsb-release \ apt-transport-https \ ca-certificates wget sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
Update source list
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list sudo apt update
Install php fpm and cli
sudo apt-get install php8.0-cli php8.0-fpm php -v
Install xdebug
Xdebug is essential for most of the php developers. So lets first install it and configure it
sudo apt-get install php8.0-xdebug
Update xdebug configuration
sudo nano /etc/php/8.0/mods-available/xdebug.ini
Here we need to pay attention to the xdebug.remote_host, as we have selected 192.168.10.1, we should use an IP like 192.168.10.100 for the vagrant machine
zend_extension=xdebug.so xdebug.mode=debug xdebug.client_host=192.168.10.1 xdebug.client_port=9003
Install other required php extensions
sudo apt install php8.0-{common,mysql,xml,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl,bcmath} -y
Update default Php runtime parameters (optional)
sudo nano /etc/php/8.0/fpm/php.ini upload_max_filesize = 50M post_max_size = 50M memory_limit = 256M max_execution_time = 600
Install Nginx
sudo apt-get install nginx
Assign an IP to the Vagrant Machine
Updates to Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "debian/buster64"
config.vm.network "private_network", ip: "192.168.10.100"
config.vm.synced_folder ".", "/vagrant", type: "nfs"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = "2"
end
end
exit
vagrant reload
vagrant ssh
Visit http://192.168.10.100 and you should see
Change Nginx default site root to /vagrant
Add nginx default site to your workspace
<work-space>/deployment/nginx/sites-available/default
server { listen 80; listen [::]:80; root /vagrant; # Add index.php to the list if you are using PHP index index.html index.php; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # With php-fpm (or other unix sockets): fastcgi_pass unix:/run/php/php8.0-fpm.sock; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } }
Update Vagrantfile to replace default nginx file during provision
Vagrant.configure("2") do |config|
config.vm.box = "debian/buster64"
config.vm.network "private_network", ip: "192.168.10.100"
config.vm.synced_folder ".", "/vagrant", type: "nfs"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.cpus = "2"
end
config.vm.provision "shell", inline: <<-SHELL
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /vagrant/deployment/nginx/sites-available/default /etc/nginx/sites-enabled/default
sudo service nginx restart
sudo chmod 755 -R /var/log
SHELL
end
Add Php files to Workspace
Create index.php and info.php in your workspace root to test if everything is working fine.
index.php
<?php
echo 'Welcome to PHP 8.0';
info.php
<?php
phpinfo();
Reload Vagrant
exit
vagrant reload
vagrant ssh
sudo service nginx restart
Visit http://192.168.10.100/info.php and you should see php 8.0 info from your vagrant machine
Install MySQL 5.7 or 8.0
Going one step further you can install your preferred DB also and the PHP framework you want to start with
wget http://repo.mysql.com/mysql-apt-config_0.8.13-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.13-1_all.deb
In order to install MySQL 8.0, ignore above images and proceed with default settings.