LAMP Ubuntu Server On VirtualBox
Last Updated:
This tutorial will help you set up a LAMP (Linux Apache MySQL PHP) Ubuntu virtual machine server running on VirtualBox. VirtualBox is a free virtualization tool that supports Linux guest additions, enabling better integration with desktop resolutions and mouse control. Shared folders between the host and guest machine allow you to save files and refresh your browser without needing to upload via FTP/SFTP, simplifying the testing process. You can also upgrade or test software like Drupal or WordPress and test new modules or customizations before adding them to your live website.
This tutorial assumes the host is running a version of Windows later than Windows 7, though it will likely work with other operating systems with slight variations.
Setting Up VirtualBox & Ubuntu
Install VirtualBox
Download VirtualBox: For this tutorial, we will use the latest version of VirtualBox for Windows hosts, which can be downloaded from the VirtualBox website.
Download Ubuntu: Download the latest version of Ubuntu Desktop from the Ubuntu website. The 64-bit version is recommended.
Install VirtualBox: Install VirtualBox by following the installer prompts. Once installed, open VirtualBox and click ‘New’ to create a new virtual machine. The virtual machine wizard will start.
Configure Virtual Machine:
- Name: Enter a name for your virtual machine, such as ‘LAMPServer’.
- Type: Select ‘Linux’.
- Version: Choose ‘Ubuntu (64-bit)’.
- Memory: Allocate at least 2048 MB (2 GB) of RAM.
- Hard Disk: Choose ‘Create a virtual hard disk now’, set it as ‘Dynamically allocated’, and allocate at least 20 GB of storage space.
Network Configuration:
- In Windows, go to Control Panel > Network and Sharing Center > Change Adapter Settings.
- Select both the Local Area Connection and VirtualBox Host-Only Network connection, right-click, and choose ‘Bridge Connections’.
- In VirtualBox, select your new virtual machine, click ‘Settings’, go to ‘Network’, and change ‘Attached to’ to ‘Bridged Adapter’. Select your bridged network connection from the ‘Name’ dropdown.
Installing Ubuntu
Start Virtual Machine: Double-click your virtual machine to start it. A ‘First Run Wizard’ will appear.
Select Installation Media: Select the Ubuntu ISO file you downloaded as the startup disk. Click ‘Start’.
Install Ubuntu: Choose ‘Install Ubuntu’ (not ‘Try Ubuntu’). Follow the on-screen instructions to complete the installation.
First Run & Update Ubuntu
Update Ubuntu: Once Ubuntu is installed, open the Terminal and run:
sudo apt update && sudo apt upgrade -y
- Unmount ISO: In the virtual machine menu, select ‘Devices’ > ‘Optical Drives’ and unmount the Ubuntu install ISO. Restart Ubuntu.
Install Guest Additions
- Install Guest Additions: From the virtual machine menu, select ‘Devices’ > ‘Insert Guest Additions CD Image’.
- Install Guest Additions: Open the terminal in Ubuntu and run:
sudo apt install build-essential dkms linux-headers-$(uname -r) sudo sh /media/$USER/VBox*/VBoxLinuxAdditions.run
- Restart Ubuntu: Reboot the virtual machine to apply the changes.
VirtualBox Snapshots
- Create a Snapshot: With the base operating system installed and updated, create a snapshot as a restore point. Select ‘Machine’ > ‘Take Snapshot’, provide a name and description, and save it. You can restore this point anytime via the ‘Snapshots’ tab in the VirtualBox window.
Install & Configure LAMP
Install all the necessary components for your LAMP server via the Terminal application.
Install Apache Server
- Open Terminal: Access the Terminal via ‘Applications’ > ‘Accessories’.
- Install Apache: Run the following command:
sudo apt install apache2
- Test Apache: Open Firefox and navigate to
http://localhost/
. You should see a page displaying ‘It Works!’.
Install PHP 8.3
- Install PHP: Run the following commands:
sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt update sudo apt install php8.3 libapache2-mod-php8.3
- Restart Apache: Restart the Apache server to apply changes:
sudo systemctl restart apache2
- Test PHP: Create a PHP test file:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test.php
Visit http://localhost/test.php
in Firefox to see PHP information.
Install MySQL
- Install MySQL: Run the following command to install MySQL server:
sudo apt install mysql-server
- Secure MySQL Installation: Run the MySQL security script:
sudo mysql_secure_installation
- Edit MySQL Configuration: Open the MySQL configuration file and set the bind address:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
- Find the line with ‘bind-address’ and change it to:
bind-address = 127.0.0.1
- Restart MySQL: Apply the changes:
sudo systemctl restart mysql
Install phpMyAdmin
- Install phpMyAdmin: Run the following command:
sudo apt install phpmyadmin
- Configure phpMyAdmin: During installation, select Apache2 and choose ‘Yes’ to configure the database automatically. Enter the root password you set during the MySQL installation.
- Include phpMyAdmin in Apache Configuration:
echo 'Include /etc/phpmyadmin/apache.conf' | sudo tee -a /etc/apache2/apache2.conf
- Restart Apache:
sudo systemctl restart apache2
- Access phpMyAdmin: Visit
http://localhost/phpmyadmin
and log in using your MySQL root username and password.
Accessing Your Server
Viewing Your Server on Host Machine
- Find IP Address: Run the following in the terminal to find your server’s IP address:
ip addr show
- Access Server: Open your web browser on your host machine and enter the IP address in the address bar to access the server and phpMyAdmin.
Setting the Server to Use a Static IP
- Edit Network Interfaces: Open the network interfaces file:
sudo nano /etc/netplan/01-netcfg.yaml
- Configure Static IP: Modify the file to set a static IP address (replace 192.168.1.85 with your static IP):
network: version: 2 renderer: networkd ethernets: enp0s3: dhcp4: no addresses: [192.168.1.85/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4]
Replace enp0s3
with your actual network interface name if it differs.
- Apply Changes: Apply the network changes:
sudo netplan apply
- Update MySQL Bind Address: Open the MySQL configuration file:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
- Set the bind address to your static IP (replace 192.168.1.85 with your static IP):
bind-address = 192.168.1.85
- Restart MySQL: Restart the MySQL service to apply the changes:
sudo systemctl restart mysql
Conclusion
You now have a fully functioning LAMP server running on Ubuntu within VirtualBox. This setup allows you to develop and test web applications efficiently. Refer to our next guide on sharing folders between the Windows host and Ubuntu guest machine for further integration tips.