Configure Php7.1 + Nginx on Amazon Linux 2 AMI

Prerequisite :

Step-1)Launch ec2 instance

  • First, select your region.
  • Goto Service > EC2 > launch Ec2 instance.
  • Select Amazon Linux 2 AMI.
  • Select instance type(eg. t2.micro)
  • Select Subnet.
  • Add storage.
  • Give the instance name.
  • Configure Security group(depends whether U want to enable HTTP or HTTPS).
  • Create a new key or use an existing one (depends on your choice).

Step-2)Install PHP7.1, php-fpm, nginx on ec2 instance.

  • Now login via ssh.
  • Install php7.1 and php7.1-fpm
amazon-linux-extras install php7.1-fpm -y
  • Now to go php-fpm.d/
cd /etc/php-fpm.d/
listen = /var/run/php-fpm/www.sock
listen.owner = nginx = nginx
listen.mode = 0664
user = nginx
group = nginx
  • Replace these lines.
  • Start the service.
systemctl start php-fpm

Step-3)Install Nginx and Configure.

  • install Nginx.
yum install nginx -y
  • Edit conf file of Nginx.
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ~ \.php$ {
#root /usr/share/nginx/html
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
error_page 404 /404.html;
location = /40x.html {
error_page 500 502 503 504 /50x.html;
location = /50x.html {
  • Now copy your PHP file in /usr/share/nginx/html
  • Starting services.
systemctl start nginx
  • Restarting all the services(if u have changed any conf files of Nginx, php-fpm).
systemctl restart php-fpm
systemctl restart nginx
  • Now open your public IP address, That’s all 😃



DevOps/Cloud Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store