On my last post, I wrote about how to create and launch an Amazon EC2 instance on Amazon Web Services (AWS). We had created an Amazon EC2 instance under “AWS Free Tier” and launched it successfully. Now we will go through step-by-step guide to SSH into the instance and then install Nginx, MySQL & PHP (LEMP) – which will be enough for us to host a simple PHP application.
We can use the public IPv4 address or the public IPv4 DNS name to SSH into the instance that we just created.
Make sure to set access permission to the ports by which we will need to communicate under the security group inbound rules. For example, here we are allowing access from anywhere to port 22 via SSH/TCP; so that we can SSH into the instance.
It’s time to login to our instance via SSH using the .pem file – we have gotten it earlier while launching our instance.
We will need to change the permission of the file first by running the following command:
1 |
sudo chmod 400 ~/Keys/kodeeo-sumonmselim-ec2.pem |
Replace the file name and location according to your environment.
Then we will run the SSH command to login:
1 |
ssh ubuntu@PUBLIC_IP_ADDRESS_OR DNS_OF_THE_INSTANCE |
You will need to replace the private key file name and server IP address as per your environment and information.
After successful login, you will see something like this on your terminal.
We have successfully got access to our instance and now we will install Nginx, MySQL and PHP to prepare a simple web server so that we can run and host our PHP application there.
First of all, we will install Nginx by running the following command:
1 |
sudo apt install nginx |
This will install the Nginx server on our instance. Next we will install MySQL.
1 |
sudo apt install mysql-server |
You will need to provide some information like MySQL root password while installing the MySQL server.
Next we will run sudo mysql_secure_installation to configure and set some basic settings for the MySQL server.
At last, we will install PHP (php-fpm) to our instance along with some other modules.
1 |
sudo apt install php8.2-fpm php8.2-mysql php8.2-cli php8.2-curl |
Next, we will need to configure the default virtual host so that we can access our web pages.
Run the following command:
1 |
sudo nano /etc/nginx/sites-available/default |
Edit the file according to your need and you will be ready. We need to tell Nginx that all our PHP files should be run by php-fpm. Here’s the information from my file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
server { listen 80; // replace with your Amazon EC2 public DNS name or domain name server_name _; // location to your root folder root /usr/share/nginx/html; index index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 /index.php; sendfile off; location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php8.2-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location ~ /\.ht { deny all; } } |
You need to restart Nginx by running the following command and then you can visit your Public DNS name or domain name address to see the default folder of your web server.
Hope this helps. If you have any questions, feel free to ask on the comment section.
Thanks, I will try it.
Thanks for share with us