To complete this project, you will need an AWS account and a virtual server with Ubuntu Server OS.
- Register a new AWS account.
- Launch a new EC2 instance in your preferred region using the t2.micro family and Ubuntu Server 20.04 LTS (HVM).
IMPORTANT: Save your private key (.pem file) securely and do not share it.
- Open Terminal.
- Use the same key downloaded from AWS; no need to convert it.
- Change directory to the location of your PEM file (likely in the Downloads folder):
cd ~/Downloads
- Change permissions for the private key file (.pem):
sudo chmod 0400 <private-key-name>.pem
- Connect to the instance:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>
- You can use 750 hours (31.25 days) of t2.micro server per month for the first 12 months FOR FREE.
- Stop your EC2 instance when not in use.
- By default, there is a soft limit of maximum 5 running instances at the same time.
- Note: Every time you stop and start your EC2 instance, you will have a new IP address. Update your SSH credentials accordingly.
Install Nginx using apt package manager:
sudo apt update
sudo apt install nginx
Check the status of the Nginx service:
sudo systemctl status nginx
Open TCP port 80 to allow incoming traffic:
# Add a rule to EC2 configuration
# to open inbound connection through port 80
Verify the installation locally:
# Access server via DNS name
curl http://localhost:80
# Access server via IP address
curl http://127.0.0.1:80
Access the server from the internet:
# Access using Public IP address
curl -s http://<Public-IP-Address>:80
If the server responds, it is correctly installed and accessible. Also can check it on browser
Install MySQL using apt package manager:
sudo apt install mysql-server
Log in to the MySQL console:
sudo mysql
Run a security script and set the root user password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PassWord.1';
exit
Run the interactive script to secure the installation:
sudo mysql_secure_installation
Answer the prompts to configure password validation and set a root password.
Test if you can log in to the MySQL console:
sudo mysql -p
To exit the MySQL console:
exit
For increased security, create dedicated user accounts for each database.
Note: PHP applications on MySQL 8 require mysql_native_password as the authentication method.
MySQL is now installed and secured. Next, we will install PHP.
Install PHP-FPM and PHP-MySQL packages:
sudo apt install php-fpm php-mysql
Confirm the installation by typing Y and pressing ENTER.
PHP components are now installed. Next, you will configure Nginx to use them.
Create a new server block configuration file for your domain:
sudo nano /etc/nginx/sites-available/projectLEMP
Paste the following configuration:
server {
listen 80;
server_name projectLEMP www.projectLEMP;
root /var/www/projectLEMP;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file. Then, activate the configuration:
sudo ln -s /etc/nginx/sites-available/projectLEMP /etc/nginx/sites-enabled/
Test the configuration for syntax errors:
sudo nginx -t
If no errors are reported, reload Nginx:
sudo systemctl reload nginx
Verify that your website is working by opening it in a browser using your server's IP address or public DNS name.
Your Nginx server is now configured to use PHP processing.
Create a test PHP file named info.php in your document root:
sudo nano /var/www/projectLEMP/info.php
Add the following PHP code to the file:
<?php
phpinfo();
Save and close the file.
Access the page in your web browser using your server's domain name or public IP address, followed by /info.php:
http://server_domain_or_IP/info.php
You will see a page with detailed information about your PHP server.
After reviewing the information, remove the file for security reasons:
sudo rm /var/www/projectLEMP/info.php
Nginx can handle .php files by successfully executing the PHP code.
Create a test database and configure access:
-
Connect to the MySQL console using the root account:
sudo mysql
-
Create a new database:
CREATE DATABASE `example_database`;
-
Create a new user with full privileges on the database:
CREATE USER 'example_user'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; GRANT ALL ON example_database.* TO 'example_user'@'%';
-
Exit the MySQL shell:
exit
-
Test the new user's permissions:
mysql -u example_user -p SHOW DATABASES;
Create a test table and insert sample data:
CREATE TABLE example_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
INSERT INTO example_database.todo_list (content) VALUES ("My first important item");
Create a PHP script to retrieve and display the data:
-
Create a new PHP file:
nano /var/www/projectLEMP/todo_list.php
-
Add the following PHP code to the file:
<?php $user = "example_user"; $password = "password"; $database = "example_database"; $table = "todo_list"; try { $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password); echo "<h2>TODO</h2><ol>"; foreach($db->query("SELECT content FROM $table") as $row) { echo "<li>" . $row['content'] . "</li>"; } echo "</ol>"; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); }
-
Save and close the file.
Access the page in your web browser using your server's domain name or public IP address, followed by /todo_list.php
:
http://server_domain_or_IP/todo_list.php
You should see a page displaying the content from your test table.