Vagrant LEMP stack: A Comprehensive Guide from Basic to Advanced

Introduction

In this tutorial, I wrote deploy Vagrant LEMP stack. I will install and configure the LEMP stack for this web application.

Vagrant is a powerful tool for managing virtual development environments, and the LEMP stack (Linux, Nginx, MySQL, PHP) is a popular choice for web developers due to its performance and flexibility. Combining Vagrant with a LEMP stack allows developers to create a consistent and reproducible environment, making development and collaboration more efficient.

What does lemp stand for?

Linux operating system, and Nginx (pronounced engine-x, hence the E in the acronym) webserver. MySQL database and dynamic content processed by PHP.

My Environment for Vagrant LEMP stack

  • Host OS: Window 11
  • Vagrant version: 2.2.18
  • Vagrant provider: VirtualBox
  • Boxes Vagrant: rockylinux/8
  • Terminal/PowerShell

Vagrant directory and files will look like as below:

C:\MYDATA\VAGRANT_VMS\PROJECTS\VAGRANT\ROCKY-LEMP
│   Vagrantfile
│
├───Files

│       index.html
│       info.php
│
└───shell

        web-lemp-rocky.sh

Files Folder contains test Nginx and PHP with index.html and info.php files.

The content index.html and info.php files

# index.html file

<html>
    <h2>DevopsRoles.com</h2>
</html>

# info.php file

<?php
phpinfo();
?>

I created a web-lemp-rocky.sh script to install Nginx, MySQL, and PHP on Rocky Linux. I use the provisioning shell of Vagrant to deploy LEMP on Rocky Linux.

The content script is as below:

#!/bin/bash


#Tools
sudo dnf install -y git unzip net-tools

#Apache
sudo dnf install -y nginx

#chkconfig --add nginx
sudo systemctl enable nginx
sudo systemctl stop nginx

sudo systemctl start nginx

#PHP
sudo dnf install php php-fpm php-gd php-mysqlnd php-cli php-opcache
sudo systemctl enable php-fpm
sudo systemctl start php-fpm

#MySQL
sudo dnf install -y mysql mysql-server mysql-devel
sudo systemctl enable mysqld.service
sudo systemctl start mysqld

mysql -u root -e "SHOW DATABASES";

# content
# sudo rm -rf /var/www/html/*
sudo cp -rf /vagrant/Files/{index.html,info.php} /usr/share/nginx/html/
# cd /vagrant
#sudo -u vagrant wget -q https://raw.git.....

sudo systemctl restart nginx

Create a Virtual Machine

Navigate to my working directory

cd Rocky-LEMP
vagrant init rockylinux/8

Configure the Virtual Machine

Edit the Vagrantfile and paste the content below

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

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

  config.vm.box = "rockylinux/8"
  config.ssh.insert_key = false  

  config.vbguest.auto_update = false
  
  config.vm.define "webserver" do |webserver|
    webserver.vm.hostname = "devopsroles.com"
    webserver.vm.network "private_network", ip: "192.168.4.4"
    webserver.vm.network "forwarded_port", guest: 80, host: 8888
    webserver.vm.provision "shell", 
     path: "C:\\MyData\\Vagrant_VMS\\Projects\\Vagrant\\Rocky-LEMP\\shell\\web-lemp-rocky.sh"

  end

end

Deploy LEMP on Rocky Linux

vagrant up

To connect to Web Server.

vagrant ssh webserver

The output terminal is below

Vagrant LEMP stack

Opens a browser that can access your Server’s IP address.

Vagrant LEMP Stack Nginx 1

Testing PHP

Vagrant LEMP stack

FAQs

What is Vagrant used for?

Vagrant is used for creating and managing virtual development environments, allowing developers to work in a consistent and isolated environment.

Why choose the LEMP stack over LAMP?

The LEMP stack uses Nginx instead of Apache, offering better performance and scalability for handling high-traffic websites.

How do I access the Vagrant VM’s web server?

You can access it using the IP address assigned to the VM or using a private network setup.

Can I use a different operating system for the Vagrant box?

Yes, Vagrant supports various operating systems, and you can specify a different box in the Vagrantfile.

Conclusion

Setting up a Vagrant LEMP stack is a powerful way to streamline your development process. By following this guide, you can create a consistent, isolated, and easily reproducible environment for your web development projects. Whether you’re a beginner or looking to implement advanced configurations, Vagrant provides the flexibility and control needed to optimize your workflow.

You have to use the Vagrant LEMP stack on Rocky Linux. I hope will this your helpful. Thank you for reading the DevopsRoles page!

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.