Here's a step-by-step guide to installing the LAMP stack (Linux, Apache, MySQL, PHP) on an AWS EC2 Ubuntu 22.04 server.
Prerequisites
- AWS EC2 instance running Ubuntu 22.04
- Connected via SSH (e.g.,
ssh -i key.pem ubuntu@your-ec2-public-ip
) - Updated system
sudo apt update && sudo apt upgrade -y
Step 1: Install Apache
sudo apt install apache2 -y
Enable and start Apache:
sudo systemctl enable apache2
sudo systemctl start apache2
Check in browser:
Go to http://your-ec2-public-ip/
— You should see the Apache2 Ubuntu default page.
Step 2: Install MySQL
Install MySQL server by running the command:
sudo apt-get install mysql-server
During the installation, you’ll be prompted to set the root password for MySQL. Choose a strong password and remember it.
Once MySQL is installed, start the service and enable it to start at boot time by running the following commands:
sudo systemctl start mysql
sudo systemctl enable mysql
This is an optional step and will allow the root MySQL user to login through phpMyAdmin.
sudo mysql
Change authentication method for the root account to mysql_native_password by executing the following query.
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY 'password';
Replace password by the root password you entered while installing MySQL.
To put the changes into effect, execute the following query :
FLUSH PRIVILEGES;
Following this, we can now do a root login through phpMyAdmin. The username is root and password is the one that was setup earlier while installing MySQL.
Return back to the command line.
Secure MySQL:
sudo mysql_secure_installation
Choose options as:
- In the next screen we will be asked whether we need to setup VALIDATE PASSWORD plugin. We selected no.
- Set the password for the root MySql account.
- Remove anonymous users? We selected yes.
- Disallow root login remotely? We selected no.
- Remove test database and access to it? We chose yes.
- Reload privilege tables now? We again chose yes.
Verify MySQL is running:
sudo systemctl status mysql
Step 3: Install PHP
Install PHP and some common PHP modules by running the command:
sudo apt-get install php libapache2-mod-php php-mysql php-xmlrpc php-soap php-curl php-gd php-xml php-cli php-bcmath php-tokenizer php-pear php-curl php-memcached php-xdebug php-intl php-fpm php-pgsql php-imap php-json php-zip php-mbstring
Once PHP is installed, restart Apache for the changes to take effect by running the command:
sudo systemctl restart apache2
Verify PHP version:
php -v
Step 4: Test PHP
Create a test PHP file to verify that PHP is working correctly. Create a file called info.php
in the document root of your web server (/var/www/html/) with the following contents:
Create a test file:
cd /var/www/html/
sudo nano /var/www/html/info.php
<?php
phpinfo();
?>
Paste the key and hit “ctrl-x”, hit “y”
to save and “enter” to exit
Visit in browser:http://your-ec2-public-ip/info.php
To remove it after testing:
sudo rm /var/www/html/info.php
That’s it! You’ve now installed LAMP on your AWS Ubuntu server.
Optionally, you can also install some additional PHP modules that you may need for your web application. You can search for available PHP modules by running the command:
sudo apt-cache search php-*
sudo apt-get install php-gd
Step 5: Allow Apache through UFW Firewall (optional but recommended)
sudo ufw allow OpenSSH
sudo ufw allow in "Apache Full"
sudo ufw enable
Optional Step: Configure Virtual Hosts (for multiple sites)
Create a config file:
sudo nano /etc/apache2/sites-available/yourdomain.conf
Example content:
<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
ServerName yourdomain.com
DocumentRoot /var/www/yourdomain
<Directory /var/www/yourdomain>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
</VirtualHost>

Enable it:
sudo a2ensite yourdomain.conf
sudo systemctl reload apache2
Your new virtual host should now be accessible via its domain name.
That’s the complete process of installing LAMP on an AWS Ubuntu server, and you can now start deploying your web applications on your server.
To access MySQL database from PHP code, you can use the MySQLi or PDO extension. For example, to connect to a MySQL database using MySQLi, you can use the following code:
<?php
$servername = "localhost";
$username = "root";
$password = "your_mysql_root_password";
$dbname = "your_database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Step 6: Allow Apache through UFW Firewall (optional but recommended)
Install phpMyAdmin and required PHP extensions
sudo apt update
sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl -y
⚠️ During installation, it will ask:
- Web server to configure: Press Space to select
apache2
, then press Enter. - Configure database with dbconfig-common? Choose Yes, then set phpMyAdmin password.
If you don’t see this screen, the installer might have skipped it. You can manually reconfigure with:
sudo dpkg-reconfigure phpmyadmin
Enable the PHP mbstring extension
sudo phpenmod mbstring
sudo systemctl restart apache2
Access phpMyAdmin in Browser
http://your-ec2-public-ip/phpmyadmin
Log in using your MySQL root or created user credentials.
That’s it! You now have a fully functional LAMP stack running on your AWS Ubuntu server.
Note:
To be able to upload files using FileZilla we need to change the ownership and access modes of the server directory.
sudo chown -R ubuntu /var/www/html
sudo chmod -R 755 /var/www/html
sudo chown -R 777 /var/www/html
sudo chown www-data:www-data -R /var/www/html
any file unzip projectname.zip
sudo apt install zip unzip
unzip projectname.zip