Setting up a high-performance web server stack using NGINX, PHP, and MySQL on Amazon Linux 2023 is a popular choice for developers deploying applications on AWS EC2.

This complete guide walks you through installing and configuring the LEMP stack (Linux, NGINX, MySQL, PHP) step by step.

Prerequisites

  • Amazon EC2 instance running Amazon Linux 2023
  • SSH access to your server
  • Basic command-line knowledge

Step 1: Install NGINX

Start by updating your system and installing NGINX.

sudo dnf update
sudo dnf install -y nginx
sudo systemctl start nginx.service
sudo systemctl status nginx.service
sudo systemctl enable nginx.service

Once installed, open your browser and enter your server’s public IP to confirm NGINX is running.

Step 2: Install PHP

Next, install PHP 8.2 on your server.

sudo dnf install php8.2 -y
php -v

To optimize performance, update the memory_limit inside /etc/php.ini.

Tip:

You can locate your PHP configuration file using phpinfo().

Step 3: Install MySQL

Now let’s install MySQL server.

sudo dnf install wget
sudo wget https://dev.mysql.com/get/mysql80-community-release-el9-3.noarch.rpm
sudo dnf install mysql80-community-release-el9-3.noarch.rpm
sudo dnf update
sudo dnf install mysql-community-server
sudo systemctl start mysqld
sudo systemctl enable mysqld
sudo systemctl status mysqld

Secure MySQL Installation

sudo grep 'temporary password' /var/log/mysqld.log
sudo mysql_secure_installation -p
mysql -u root -p

Fix PHP MySQL Connection Issue

If PHP cannot connect to MySQL, install the MySQL driver:

sudo yum -y install php-mysqlnd

Step 4: Configure NGINX for Your Domain

Ensure your domain is pointed to your EC2 instance using an A record.

Create NGINX Config File

cd /etc/nginx
cd conf.d
sudo nano mysite.conf

Paste the following configuration:

server {
    listen 80;
    server_name mysite.com;
    root /var/www/html/mysite.com;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    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; }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Restart NGINX

sudo systemctl restart nginx

Test Your Setup

Create an index.php file inside:

/var/www/html/mysite.com

Then open your domain in a browser.

Permissions Setup

Set correct ownership for your web directory:

sudo chown -R "$USER":www-data /var/www/html

Laravel Storage Permissions

sudo chmod -R 777 /var/www/html/storage/*

If needed, apply permissions to other directories:

sudo chmod -R 777 /path/to/directory

Conclusion

You have successfully installed and configured NGINX, PHP, and MySQL on Amazon Linux 2023. This LEMP stack setup is optimized for performance and scalability, making it ideal for hosting modern web applications like Laravel, WordPress, and custom PHP apps.

SEO Keywords

  • NGINX PHP MySQL Amazon Linux 2023
  • LEMP stack AWS setup
  • Install NGINX Amazon Linux 2023
  • PHP MySQL EC2 setup
  • AWS web server configuration

Leave a Reply

Your email address will not be published. Required fields are marked *