Empower your server: Install and configure Apache, MySQL, and PHP on Ubuntu.
Estimated Time: Approximately 45 - 60 minutes
This comprehensive guide will walk you through the installation and setup of a robust LAMP stack (Linux, Apache, MySQL, PHP) on your Ubuntu server. The LAMP stack is a popular open-source solution renowned for its reliability, security, and widespread use in hosting dynamic websites and web applications.
Here's a quick breakdown of each component's role:
This guide is perfect for beginners looking to set up a foundational web server environment, ideal for hosting various PHP-based web applications like WordPress, Drupal, or custom-built frameworks.
45 - 60 minutes
Beginner
Basic command-line knowledge is helpful. No prior server administration experience is strictly required, but familiarity with a terminal is beneficial.
sudo privileges. If you're on a cloud provider (e.g., AWS, DigitalOcean, Linode), ensure your security groups or firewalls allow SSH (port 22) and HTTP/HTTPS (ports 80/443) traffic.It's crucial to always update your local package index before installing any new software. This ensures your system's package list is current and that you install the latest stable versions and security patches.
sudo apt update && sudo apt upgrade -ysudo apt update refreshes the list of available packages from the Ubuntu repositories.sudo apt upgrade -y then upgrades all installed packages to their latest versions, with -y automatically confirming prompts. If a kernel update occurs, a server reboot might be required for changes to take effect.
Before installing any services, it's a best practice to set up a firewall to secure your server. UFW is a user-friendly frontend for `iptables` that makes firewall management easy.
Install UFW (if not already present):
sudo apt install ufw -yAllow SSH, HTTP, and HTTPS traffic:
sudo ufw allow OpenSSHsudo ufw allow 'Apache'sudo ufw allow 'Apache Full' (allows both HTTP and HTTPS)
OpenSSH (port 22) is essential for remote access.
Apache (port 80 for HTTP) is needed for your web server.
Apache Full (ports 80 and 443 for HTTPS) is recommended as you'll likely want HTTPS in the future. You can choose to enable only `Apache` for now if you prefer.
Enable UFW:
sudo ufw enableCheck UFW status:
sudo ufw statusStatus: active and list the allowed rules for OpenSSH, Apache, and Apache Full.
Apache HTTP Server is the workhorse that serves your website content to visitors. Install it with a single command:
sudo apt install apache2 -yOnce installed, enable and start the Apache service to ensure it runs automatically on boot and is active now:
sudo systemctl enable apache2sudo systemctl start apache2
/var/www/html.
To check Apache's status, use sudo systemctl status apache2.
Verify Apache Installation:
First, find your server's public IP address:
curl -4 icanhazip.comThen, open your web browser and navigate to http://your_server_ip (replace your_server_ip with the IP address you found). You should see the default Apache "Ubuntu Default Page".
MySQL is a powerful relational database management system used to store and manage your application's data. Install it:
sudo apt install mysql-server -yAfter installation, it's highly recommended to secure your MySQL instance. This script guides you through setting a root password, removing anonymous users, disallowing remote root logins, and more:
sudo mysql_secure_installationmysql_secure_installation, set a strong root password. Select the highest password validation level recommended (Strong) and follow all prompts to enhance your database security significantly. Remember this password!
Create a Dedicated MySQL Database and User (Best Practice):
It's unsafe to use the MySQL root user for your web applications. Instead, create a dedicated database and a user with specific privileges for your application.
sudo mysql -u root -pOnce in the MySQL prompt (`mysql>`), execute the following commands, replacing `your_database_name`, `your_username`, and `your_password` with secure choices:
CREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';FLUSH PRIVILEGES;EXIT;
PHP is the scripting language that processes dynamic content, allowing your web applications to interact with the database and generate HTML. We'll install the core PHP package along with modules for Apache integration and MySQL connectivity:
sudo apt install php libapache2-mod-php php-mysql -ylibapache2-mod-php integrates PHP with Apache.php-mysql provides MySQL database connectivity for PHP.
Install Common PHP Modules (Optional but Recommended):
Many PHP applications require additional modules. It's a good idea to install some commonly used ones:
sudo apt install php-cli php-gd php-curl php-mbstring php-xml php-zip php-intl -yRestart Apache after PHP installations:
sudo systemctl restart apache2Confirm the PHP installation by checking its version:
php -vApache processes files in a specific order. Ensure `.php` files are prioritized:
Edit Apache's `dir.conf` file:
sudo nano /etc/apache2/mods-enabled/dir.confMove `index.php` to be the first entry in the `DirectoryIndex` line:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Change to:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save and exit (`Ctrl+O`, `Enter`, `Ctrl+X`). Then restart Apache:
sudo systemctl restart apache2To confirm that PHP is correctly integrated with Apache, we'll create a simple PHP info file in your web server's document root.
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.phpNow, open your browser and navigate to http://your_server_ip/info.php (using your server's public IP). You should see a detailed PHP information page, confirming PHP is working correctly with Apache.
info.php file exposes sensitive server information (PHP configuration, modules, environment variables). It's crucial to remove this file immediately after testing for security reasons, especially on public-facing servers.
Remove the test file:
sudo rm /var/www/html/info.phpProper file permissions are essential for security and for your web applications to function correctly. The Apache user (`www-data`) needs read access to files and often write access to specific directories (e.g., for uploads, caches).
Change ownership of your web root:
sudo chown -R $USER:www-data /var/www/htmlSet directory permissions (read, write, execute for owner, read & execute for group):
sudo find /var/www/html -type d -exec chmod 755 {} \;Set file permissions (read & write for owner, read for group):
sudo find /var/www/html -type f -exec chmod 644 {} \;Confirm your LAMP stack is fully operational and secure:
sudo ufw status. It should show Status: active with rules for OpenSSH, Apache, and Apache Full.systemctl status apache2. Look for active (running) in the output.mysql -u your_username -p your_database_name. It should work.info.php file).Congratulations! You have successfully installed and configured a robust and secure LAMP stack on your Ubuntu server. This strong foundation is now ready for your web development projects and deployment of dynamic web applications.
From here, you can dive into exciting possibilities and further harden your server: