
The LAMP stack combines Linux, Apache, MySQL, and PHP to serve dynamic websites and applications. It provides a reliable foundation for hosting PHP-based applications with Apache's flexible web server, MySQL's robust database management, and PHP's server-side scripting capabilities. The Vultr Marketplace provides a pre-configured LAMP instance, enabling quick deployment and setup on a Vultr server.
This guide explains deploying and using Vultr's LAMP Marketplace Application. You will deploy an instance, verify the installation, configure DNS and SSL, set up a virtual host, secure MySQL, and implement best practices for production deployments.
Deploy Vultr's LAMP Marketplace Application
Log in to your Vultr Customer Portal and click the Deploy Server button.
Select your preferred server type.
Choose a server location.
Select a server plan with at least 1GB RAM and 1 CPU core for basic workloads, or 2GB RAM and 2 CPU cores for production applications.
Click the Configure button to proceed.
Under Marketplace Apps, search for
LAMPand select it as the Marketplace Application.Select the Limited Login option from the Additional Features section to create a limited user with sudo access.
Review your configurations and click the Deploy Now button to start deployment.
It may take up to 10 minutes for your server to finish installing the LAMP stack.NoteAfter the instance shows the status of Running, navigate to the Server Overview page and copy the SSH connection details.
Initial Setup and Configuration
After deployment, verify the installation, configure DNS, and secure your LAMP stack before hosting applications.
Create a DNS A record pointing to your server's IP address, such as
lamp.example.com.Connect to your Vultr server instance over SSH using the connection details from the Server Overview page.
Verify LAMP Installation
Check the Apache service status.
console$ sudo systemctl status apache2
The service should show as
active (running).Verify the PHP installation.
console$ php -v
Output:
PHP 8.3.12 (cli) (built: Sep 27 2024 03:53:05) (NTS)Check the MySQL service status.
console$ sudo systemctl status mysql
The service should show as
active (running).Verify Apache is serving the default page by visiting
http://SERVER_IPin a web browser.
Configure Firewall Security
Secure your server by configuring the firewall to allow only necessary traffic before enabling SSL.
Allow SSH connections.
console$ sudo ufw allow OpenSSH
Allow HTTP and HTTPS traffic for Apache and Certbot.
console$ sudo ufw allow 'Apache Full'
Enable the firewall.
console$ sudo ufw enable
Verify firewall status.
console$ sudo ufw status
Secure LAMP with SSL/TLS
Protect your web server with HTTPS using Let's Encrypt certificates via Certbot.
Install Certbot and the Apache plugin.
console$ sudo apt update $ sudo apt install certbot python3-certbot-apache -y
Request an SSL certificate for your domain.
console$ sudo certbot --apache -d lamp.example.com
Follow the prompts and select the option to redirect HTTP traffic to HTTPS when asked.
Verify SSL certificate auto-renewal.
console$ sudo certbot renew --dry-run
Access your site securely at
https://lamp.example.com.
Configure Apache Virtual Host
Set up a virtual host to serve your website with proper configurations for PHP applications.
Create a directory for your website.
console$ sudo mkdir -p /var/www/lamp.example.com/html
Set proper ownership.
console$ sudo chown -R www-data:www-data /var/www/lamp.example.com
Set safe directory permissions.
console$ sudo chmod -R 755 /var/www/lamp.example.com
Create a test index file.
console$ sudo nano /var/www/lamp.example.com/html/index.php
php<!DOCTYPE html> <html> <head> <title>Welcome to LAMP Server</title> </head> <body> <h1>Success! The LAMP virtual host is working.</h1> <p>Server time: <?php echo date('Y-m-d H:i:s'); ?></p> </body> </html>
Save and close the file.
Create an Apache virtual host configuration.
console$ sudo nano /etc/apache2/sites-available/lamp.example.com.conf
apache<VirtualHost *:80> ServerName lamp.example.com ServerAdmin admin@lamp.example.com DocumentRoot /var/www/lamp.example.com/html <Directory /var/www/lamp.example.com/html> Options -Indexes +FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/lamp.example.com-error.log CustomLog ${APACHE_LOG_DIR}/lamp.example.com-access.log combined </VirtualHost>
Replace
lamp.example.comwith your domain name.Save and close the file.
Enable the virtual host.
console$ sudo a2ensite lamp.example.com.conf
Disable the default Apache site.
console$ sudo a2dissite 000-default.conf
Enable required Apache modules.
console$ sudo a2enmod rewrite ssl
Test the Apache configuration.
console$ sudo apache2ctl configtest
Output:
Syntax OKReload Apache to apply changes.
console$ sudo systemctl reload apache2
Run Certbot again to configure SSL for the new virtual host.
console$ sudo certbot --apache -d lamp.example.com
Access your site at
https://lamp.example.comto verify the test page loads with SSL.
Configure MySQL Database
Set up MySQL with secure authentication and create a database for your application.
Secure MySQL Installation
Run the MySQL secure installation script.
console$ sudo mysql_secure_installation
Follow the prompts:
- Set root password: Enter a strong password
- Remove anonymous users: Yes
- Disallow root login remotely: Yes
- Remove test database: Yes
- Reload privilege tables: Yes
Create Database and User
Log in to MySQL as root.
console$ sudo mysql -u root -p
Enter the root password when prompted.
Create a database for your application.
sql> CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Create a dedicated database user.
sql> CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'secure_password_here';
Replace
secure_password_herewith a strong password.Grant privileges to the user.
sql> GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
Flush privileges and exit.
sql> FLUSH PRIVILEGES; > EXIT;
Test the new user connection.
console$ mysql -u myapp_user -p myapp_db
Enter the password when prompted. If you can access the database, the setup is correct.
Configure PHP
Optimize PHP settings for production use and security.
Update PHP Configuration
Edit the PHP configuration file for Apache.
console$ sudo nano /etc/php/8.3/apache2/php.ini
Update the following settings for production use.
iniupload_max_filesize = 64M post_max_size = 64M memory_limit = 256M max_execution_time = 300 max_input_time = 300 display_errors = Off log_errors = On error_log = /var/log/php/error.log
Adjust values based on your application requirements.
Save and close the file.
Create the PHP log directory.
console$ sudo mkdir -p /var/log/php $ sudo chown www-data:www-data /var/log/php
Restart Apache to apply changes.
console$ sudo systemctl restart apache2
Install Common PHP Extensions
Install frequently used PHP extensions.
console$ sudo apt install php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip php8.3-intl -y
Restart Apache after installing extensions.
console$ sudo systemctl restart apache2
Verify installed extensions.
console$ php -m
Best Practices and Configuration
Implement these recommendations to ensure your LAMP stack runs securely and efficiently.
Security Hardening
Configure Apache security headers in your virtual host.
console$ sudo nano /etc/apache2/sites-available/lamp.example.com.conf
Add the following inside the
<VirtualHost>block:apacheHeader always set X-Frame-Options "SAMEORIGIN" Header always set X-Content-Type-Options "nosniff" Header always set X-XSS-Protection "1; mode=block" Header always set Referrer-Policy "no-referrer-when-downgrade"
Enable the headers module and reload Apache:
console$ sudo a2enmod headers $ sudo systemctl reload apache2
Disable PHP version exposure.
console$ sudo nano /etc/php/8.3/apache2/php.ini
Set:
iniexpose_php = Off
Restart Apache:
sudo systemctl restart apache2Hide Apache version information.
console$ sudo nano /etc/apache2/conf-available/security.conf
apacheServerTokens Prod ServerSignature Off
Reload Apache:
sudo systemctl reload apache2Keep the system and packages updated.
console$ sudo apt update $ sudo apt upgrade -y
Performance Optimization
Enable PHP OPcache for better performance.
console$ sudo nano /etc/php/8.3/apache2/php.ini
iniopcache.enable=1 opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=10000 opcache.revalidate_freq=60
Restart Apache.
Enable Apache compression module.
console$ sudo a2enmod deflate $ sudo systemctl reload apache2
Enable Apache caching module for static assets.
console$ sudo a2enmod expires $ sudo systemctl reload apache2
Backup Configuration
Create regular backups of important directories.
console$ sudo tar -czf /root/backup-$(date +%F).tar.gz /var/www /etc/apache2 /etc/php
Back up MySQL databases.
console$ sudo mysqldump -u root -p --all-databases > /root/mysql-backup-$(date +%F).sql
Troubleshooting
This section covers common issues and diagnostic commands to help resolve problems with your LAMP stack.
Check Service Status
Verify all services are running.
console$ sudo systemctl status apache2 $ sudo systemctl status mysql
View service logs.
console$ sudo journalctl -u apache2 -e $ sudo journalctl -u mysql -e $ sudo tail -f /var/log/apache2/error.log
Common Issues
500 Internal Server Error
Check Apache error logs for details.
console$ sudo tail -50 /var/log/apache2/error.log $ sudo tail -50 /var/log/apache2/lamp.example.com-error.log
Verify PHP is properly configured.
console$ php -v $ sudo systemctl status apache2
Test Apache configuration.
console$ sudo apache2ctl configtest
PHP Files Downloading Instead of Executing
Verify PHP module is enabled.
console$ sudo a2enmod php8.3 $ sudo systemctl restart apache2
Check that
.phpfiles are configured correctly in Apache.console$ apachectl -M | grep php
Permission Denied Errors
Set proper ownership for web directories.
console$ sudo chown -R www-data:www-data /var/www/lamp.example.com $ sudo chmod -R 755 /var/www/lamp.example.com
Verify Apache is running as the correct user.
console$ ps aux | grep apache2
Check SELinux or AppArmor restrictions if applicable.
MySQL Connection Errors
Verify MySQL is running and accessible.
console$ sudo systemctl status mysql $ mysql -u root -p -e "SELECT 1;"
Check user privileges.
console$ sudo mysql -u root -p -e "SELECT user, host FROM mysql.user;"
Verify MySQL socket permissions.
console$ ls -l /var/run/mysqld/mysqld.sock
.htaccess Not Working
Ensure
mod_rewriteis enabled.console$ sudo a2enmod rewrite $ sudo systemctl restart apache2
Verify
AllowOverride Allis set in the virtual host configuration.
Use Cases
The LAMP stack provides a widely-supported environment suitable for various PHP-based web workloads:
- Content Management Systems: Deploy WordPress, Drupal, Joomla, or other CMS platforms with Apache's robust virtual host configuration.
- E-commerce Platforms: Run Magento, PrestaShop, WooCommerce, or OpenCart stores with secure SSL and reliable database management.
- PHP Frameworks: Host Laravel, Symfony, CodeIgniter, or custom PHP applications with Apache's flexible
.htaccesssupport. - Legacy Applications: Maintain compatibility with applications requiring Apache-specific features and modules.
- Web Applications: Deploy business applications, CRM systems, project management tools, or SaaS platforms.
- Development Environments: Create staging and testing environments that mirror production Apache configurations.
Conclusion
In this guide, you deployed Vultr's LAMP Marketplace Application and configured it for production use. You secured the server with firewall rules and SSL/TLS certificates, set up Apache virtual hosts with proper permissions, configured MySQL with secure authentication and dedicated database users, and optimized PHP settings for performance and security. With this production-ready LAMP stack, you can host PHP applications, manage databases, and serve dynamic content reliably using Apache's proven web server technology.