AWS EC2 Adminer Setup
November 28, 2024 · AI Conversations
### Prerequisites
1. **AWS EC2 Instance** (Linux-based, e.g., Amazon Linux 2, Ubuntu).
2. **SSH access** to your EC2 instance.
3. **MySQL** (or other supported database) installed on the instance or accessible remotely.
4. **Security Group** with ports 22 (SSH) and 8080 (or your chosen port for Adminer) open.
### Step 1: SSH into Your EC2 Instance
ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-ip
*Use `ubuntu` as the username if using an Ubuntu-based instance.*
### Step 2: Update System Packages
sudo apt update && sudo apt upgrade -y # For Ubuntu
sudo yum update -y # For Amazon Linux 2
### Step 3: Install Apache (or Nginx)
For this example, we'll use Apache.
# Install Apache
sudo apt install apache2 -y # For Ubuntu
sudo yum install httpd -y # For Amazon Linux 2
# Start and enable Apache
sudo systemctl start apache2 # For Ubuntu
sudo systemctl start httpd # For Amazon Linux 2
sudo systemctl enable apache2 # For Ubuntu
sudo systemctl enable httpd # For Amazon Linux 2
### Step 4: Install PHP
# Install PHP
sudo apt install php php-mysql -y # For Ubuntu
sudo amazon-linux-extras install php8.0 -y # For Amazon Linux 2
# Restart Apache to load PHP
sudo systemctl restart apache2 # For Ubuntu
sudo systemctl restart httpd # For Amazon Linux 2
### Step 5: Download Adminer
# Navigate to the web root directory
cd /var/www/html
# Download the latest Adminer (version 4.8.1 as of now)
sudo wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1.php -O adminer.php
# Adjust permissions
sudo chown www-data:www-data adminer.php # For Ubuntu
sudo chown apache:apache adminer.php # For Amazon Linux 2
### Step 6: Configure Apache (Optional)
Create a Virtual Host for Adminer:
sudo nano /etc/apache2/sites-available/adminer.conf # For Ubuntu
sudo nano /etc/httpd/conf.d/adminer.conf # For Amazon Linux 2
Add the following content:
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html
ServerName your-ec2-public-ip
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the site and restart Apache:
sudo a2ensite adminer.conf # For Ubuntu
sudo systemctl reload apache2 # For Ubuntu
sudo systemctl restart httpd # For Amazon Linux 2
### Step 7: Access Adminer in Your Browser
– Open your browser and go to:
http://your-ec2-public-ip/adminer.php
### Step 8: Security Best Practices (Optional)
1. **Password Protect Adminer**
sudo htpasswd -c /etc/apache2/.htpasswd adminuser
Add to your Apache config:
<Directory "/var/www/html">
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
2. **Restrict IP Access** (only allow specific IPs)
<Directory "/var/www/html">
Require ip your-ip-address
</Directory>
3. **Use HTTPS** (Install Certbot for Let's Encrypt)
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache
You now have Adminer running on your AWS EC2 instance!
### Prerequisites
– **EC2 Ubuntu instance** running (e.g., Ubuntu 20.04 or later).
– **SSH access** to the instance.
– **Security Group** with the following ports open:
– **22** for SSH.
– **80** for HTTP (or **443** if you plan to use HTTPS).
### Step 1: SSH into Your EC2 Instance
Make sure you use the correct username (`ubuntu` for Ubuntu).
ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
### Step 2: Update System Packages
sudo apt update && sudo apt upgrade -y
### Step 3: Install Apache Web Server
sudo apt install apache2 -y
– Start and enable Apache to run on boot:
sudo systemctl start apache2
sudo systemctl enable apache2
### Step 4: Install PHP and MySQL Extensions
sudo apt install php libapache2-mod-php php-mysql -y
– Restart Apache to load PHP:
sudo systemctl restart apache2
### Step 5: Download Adminer
– Navigate to the web server root directory:
cd /var/www/html
– Download the latest version of Adminer:
sudo wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1.php -O adminer.php
– Adjust permissions:
sudo chown www-data:www-data adminer.php
sudo chmod 644 adminer.php
### Step 6: Configure Apache for Adminer (Optional)
To make accessing Adminer easier, you can create a dedicated configuration file:
1. **Create a Virtual Host configuration**:
sudo nano /etc/apache2/sites-available/adminer.conf
2. **Add the following content**:
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html
Alias /adminer /var/www/html/adminer.php
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/adminer_error.log
CustomLog ${APACHE_LOG_DIR}/adminer_access.log combined
</VirtualHost>
3. **Enable the new site and reload Apache**:
sudo a2ensite adminer.conf
sudo systemctl reload apache2
### Step 7: Access Adminer
– Open your browser and navigate to:
http://your-ec2-public-ip/adminer
### Step 8: Secure Adminer (Optional)
#### 1. Password Protect Adminer
– Create a password file:
sudo htpasswd -c /etc/apache2/.htpasswd adminuser
– Update your Apache configuration:
<Directory "/var/www/html">
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
– Reload Apache:
sudo systemctl reload apache2
#### 2. Enable HTTPS with Let's Encrypt (Optional)
– Install Certbot:
sudo apt install certbot python3-certbot-apache -y
– Obtain and install an SSL certificate:
sudo certbot --apache
– Follow the prompts to secure your Adminer installation with HTTPS.
### Step 9: Allow HTTP/HTTPS in the Security Group
Ensure your EC2 instance's **Security Group** allows inbound traffic on ports **80** (HTTP) and **443** (HTTPS):
– Go to the **AWS Management Console**.
– Navigate to **EC2 > Security Groups**.
– Edit the inbound rules to include:
– Type: HTTP, Protocol: TCP, Port: 80, Source: 0.0.0.0/0
– Type: HTTPS, Protocol: TCP, Port: 443, Source: 0.0.0.0/0
You should now have **Adminer** up and running on your AWS EC2 Ubuntu instance.
### 1. Basic PHP Info Page
Create a `phpinfo.php` file to check if PHP is working correctly:
sudo nano /var/www/html/phpinfo.php
Add the following code:
<?php
// phpinfo.php
phpinfo();
– Save and exit (`CTRL + X`, then `Y`, then `Enter`).
– Access it in your browser:
http://your-ec2-public-ip/phpinfo.php
This will display detailed information about your PHP configuration, including the PHP version, loaded modules, and environment variables.
### 2. PHP Code for MySQL Database Connection
To demonstrate how you can connect to a MySQL database using PHP (which can be helpful when using Adminer), you can create a `dbtest.php` file.
Create the file:
sudo nano /var/www/html/dbtest.php
Add the following PHP code:
<?php
// dbtest.php
$servername = "localhost"; // Use "localhost" if MySQL is on the same server, or replace with your database server IP
$username = "your_username"; // Replace with your MySQL username
$password = "your_password"; // Replace with your MySQL password
$dbname = "your_database"; // Replace with your MySQL 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 to the database.";
// Close the connection
$conn->close();
### Steps to Run the MySQL Connection Test:
1. **Save and exit** (`CTRL + X`, then `Y`, then `Enter`).
2. Make sure your MySQL server is running:
sudo systemctl start mysql
3. **Open the test file in your browser**:
http://your-ec2-public-ip/dbtest.php
If the connection is successful, you will see:
Connected successfully to the database.
If there are any issues, you'll see an error message detailing what went wrong.
### 3. Clean Up (Optional)
After testing, it's a good practice to remove these test files to avoid exposing sensitive information.
sudo rm /var/www/html/phpinfo.php
sudo rm /var/www/html/dbtest.php
This setup should help you verify that your PHP and MySQL environments are correctly configured on your AWS EC2 instance.
Let's fix this step-by-step.
### Step 1: Verify PHP Installation
First, make sure PHP is installed:
php -v
You should see the PHP version if it's installed correctly. If not, install PHP:
sudo apt update
sudo apt install php libapache2-mod-php php-mysql -y
### Step 2: Enable the PHP Module for Apache
Ensure that the PHP module is enabled in Apache:
sudo a2enmod php
Then, restart Apache:
sudo systemctl restart apache2
### Step 3: Test PHP with Apache
Create a test PHP file to confirm that Apache is serving PHP correctly.
sudo nano /var/www/html/test.php
Add the following code:
<?php
echo "PHP is working!";
– Save and exit (`CTRL + X`, then `Y`, then `Enter`).
– Open your browser and go to:
http://your-ec2-public-ip/test.php
If you see **"PHP is working!"**, then PHP is properly configured.
### Step 4: Ensure the Correct `adminer.php` File
If the test file works, but Adminer still shows PHP code, it could be an issue with the file itself.
1. **Re-download Adminer** to make sure it's correct:
cd /var/www/html
sudo wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1.php -O adminer.php
2. **Check Permissions**:
sudo chown www-data:www-data adminer.php
sudo chmod 644 adminer.php
3. **Restart Apache**:
sudo systemctl restart apache2
### Step 5: Clear Browser Cache and Test Again
Open your browser and go to:
http://your-ec2-public-ip/adminer.php
If everything is set up correctly, you should see the **Adminer login interface** instead of raw PHP code.
### Step 6: (Optional) Check Apache Configuration
If the problem persists, you might need to check the Apache configuration:
1. Open the Apache configuration file:
sudo nano /etc/apache2/apache2.conf
2. Ensure the following lines are present:
<FilesMatch ".php$">
SetHandler application/x-httpd-php
</FilesMatch>
3. Save the file and restart Apache:
sudo systemctl restart apache2
### Step 7: Clean Up (Optional)
After everything is working, delete the test PHP file:
sudo rm /var/www/html/test.php
These steps should resolve the issue of Adminer showing PHP code instead of the interface.
### Here’s how to fix it:
### Step 1: Move Adminer to the Correct Directory
Make sure **Adminer** is placed in the web server's root directory (`/var/www/html`).
1. **Navigate to the web root**:
cd /var/www/html
2. **Check if `adminer.php` exists**:
ls -l
If it's not there, download Adminer again:
sudo wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1.php -O adminer.php
3. **Set the correct permissions**:
sudo chown www-data:www-data adminer.php
sudo chmod 644 adminer.php
### Step 2: Remove the Default `index.html` File
The default Apache page is served by `index.html` in `/var/www/html`. You need to either remove or rename it.
sudo rm /var/www/html/index.html
### Step 3: Restart Apache
After making changes, restart Apache to apply them:
sudo systemctl restart apache2
### Step 4: Test Adminer in Your Browser
Now, try accessing Adminer again:
http://your-ec2-public-ip/adminer.php
You should see the **Adminer login interface**.
### Step 5: (Optional) Configure Apache to Serve Adminer by Default
If you want **Adminer** to load automatically when you visit your server's IP address:
1. **Create or edit an `.htaccess` file**:
sudo nano /var/www/html/.htaccess
2. **Add the following line**:
DirectoryIndex adminer.php
3. **Enable `.htaccess` files** (if not already enabled):
– Open the Apache configuration file:
sudo nano /etc/apache2/apache2.conf
– Find the section “ and ensure it contains:
AllowOverride All
4. **Restart Apache**:
sudo systemctl restart apache2
Now, if you visit:
http://your-ec2-public-ip/
It should automatically show the **Adminer** interface.
### Step 6: Clear Browser Cache
Sometimes, your browser might cache the default page. Clear your browser cache or open the link in an incognito window.
These steps should resolve the issue of seeing the default Apache page instead of Adminer.
Here’s how to fix this:
### Step 1: Ensure the Correct Directory Structure
Make sure that you have the Adminer files organized properly. If you downloaded Adminer into a folder, it might look something like this:
/var/www/html/adminer/
├── adminer.php
└── index.php
### Step 2: Access the Adminer Interface
To access Adminer, you should navigate to the `adminer` folder in your browser:
http://your-ec2-public-ip/adminer/adminer.php
or if you're using `index.php`:
http://your-ec2-public-ip/adminer/index.php
### Step 3: Configure Apache to Serve Adminer from a Subdirectory (Optional)
If you want to simplify access to Adminer, you can set up a specific configuration.
#### Option 1: Change the DirectoryIndex
If you want Apache to serve `adminer.php` by default when accessing the `adminer` directory, you can create or edit an `.htaccess` file:
1. **Create or edit the `.htaccess` file** in the `adminer` directory:
sudo nano /var/www/html/adminer/.htaccess
2. **Add the following line**:
DirectoryIndex adminer.php
#### Option 2: Create a Virtual Host for Adminer
You can also create a dedicated virtual host configuration for Adminer if you want it to be accessible at a simpler URL like `http://your-ec2-public-ip/adminer`.
1. **Create a new virtual host file** for Adminer:
sudo nano /etc/apache2/sites-available/adminer.conf
2. **Add the following configuration**:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/adminer
DirectoryIndex adminer.php
<Directory /var/www/html/adminer>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/adminer_error.log
CustomLog ${APACHE_LOG_DIR}/adminer_access.log combined
</VirtualHost>
3. **Enable the new site configuration**:
sudo a2ensite adminer.conf
4. **Reload Apache** to apply the changes:
sudo systemctl reload apache2
### Step 4: Test the Configuration
Once the above changes are made, try accessing Adminer again at:
http://your-ec2-public-ip/adminer/
This should now bring up the Adminer interface without needing to specify the `adminer.php` file.
### Step 5: Check Apache Configuration
If you are still facing issues, ensure that your Apache configuration allows the use of `.htaccess` files. Check the `/etc/apache2/apache2.conf` file and ensure the `AllowOverride All` directive is present in the appropriate directory section for `/var/www/html/`.
### Step 6: Check File Permissions
Ensure that the files in the `adminer` directory have the correct permissions:
sudo chown -R www-data:www-data /var/www/html/adminer
sudo chmod -R 755 /var/www/html/adminer
### Final Note
If you follow these steps, you should be able to access the Adminer interface without issues. If problems persist, please check the Apache error logs for any relevant error messages:
sudo tail -f /var/log/apache2/error.log
These logs can provide insight into what's going wrong.
### Step 1: Remove the Adminer Files
If you installed Adminer in the `/var/www/html/adminer` directory, you can remove it using the following command:
1. Open your terminal (or SSH into your server).
2. Run the command to remove the `adminer` folder:
sudo rm -rf /var/www/html/adminer
This command will delete the entire `adminer` directory and all of its contents.
### Step 2: Remove Virtual Host Configuration (If Created)
If you set up a virtual host for Adminer, you should also remove that configuration:
1. **Disable the site configuration**:
sudo a2dissite adminer.conf
2. **Remove the configuration file**:
sudo rm /etc/apache2/sites-available/adminer.conf
3. **Reload Apache to apply changes**:
sudo systemctl reload apache2
### Step 3: Clean Up (Optional)
If you created any specific logs for Adminer or modified any Apache configurations specifically for Adminer, you may want to clean those up as well.
1. **Remove any Adminer-specific log files**:
sudo rm /var/log/apache2/adminer_error.log
sudo rm /var/log/apache2/adminer_access.log
### Final Note
After performing these steps, **Adminer** will be completely uninstalled from your Ubuntu server. If you need to reinstall it later, you can always download it again from the [Adminer website](https://www.adminer.org/).