Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install PHP and NGINX on Amazon Linux AMI
## Install NGINX
## when installing on Amazon Linux AMI, use:
$ sudo yum install nginx -y
## when installing on Amazon Linux 2 AMI, use
$ sudo amazon-linux-extras install nginx1.12 -y
## Install PHP and PHP-FPM
# for PHP version 7.1 use php71 and php71-fpm instead
$ sudo yum install php -y
$ sudo yum install php-fpm -y
## Configure NGINX (see below)
$ sudo nano /etc/nginx/conf.d/default.conf
## Configure PHP-FPM (see below)
$ sudo nano /etc/php-fpm.d/www.conf
## Add NGINX and PHP-FPM service start to boot sequence
$ sudo chkconfig nginx on
$ sudo chkconfig php-fpm on
## Start NGINX and PHP-FPM service
$ sudo service nginx start
$ sudo service php-fpm start
## Add <file>.php to /var/www/html
## Verify configuration via http://www.domain.com/<file>.php
server {
listen 80;
listen [::]:80;
server_name www.domain.com domain.com;
location / {
root /var/www/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
## Config associated with PHP-FPM version 7.1
## Comment out the following entries (with ;)
;listen.acl_users = apache,nginx
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0666
;user = apache
;group = apache
## Add the following values instead
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0664
user = nginx
group = nginx
@xird

This comment has been minimized.

Copy link

@xird xird commented May 6, 2019

This no longer works on the latest Amazon Linux images, as nginx isn't included in the default packages. See https://stackoverflow.com/questions/37082406/how-to-install-nginx-1-9-15-on-amazon-linux-disto

@bgbruno

This comment has been minimized.

Copy link

@bgbruno bgbruno commented Jul 7, 2019

@xird agree

UPDATE
sudo yum install nginx -y
->
sudo amazon-linux-extras install nginx1.12 -y

@nrollr

This comment has been minimized.

Copy link
Owner Author

@nrollr nrollr commented Jul 7, 2019

@xird @bgbruno
Agree, a different command is required IF it concerns a type Amazon Linux 2 AMI.
The Amazon Linux AMI still accepts the sudo yum install nginx -y -command (tested with ami-05b93cd5a1b552734)

Added the command + some comments when installing on Amazon Linux 2 AMI

@bgbruno

This comment has been minimized.

Copy link

@bgbruno bgbruno commented Jul 8, 2019

@nrollr yap it was Amazon Linux 2 AMI

@eaponiente

This comment has been minimized.

Copy link

@eaponiente eaponiente commented Jul 17, 2019

does this work on Amazon Linux 2 AMI? I cant get it to work, although all of these commands does not return any error when I tried em

@3fonteinen

This comment has been minimized.

Copy link

@3fonteinen 3fonteinen commented Aug 8, 2019

@eaponiente just ran a test with amzn2-ami-hvm-2.0.20190618-x86_64-gp2 (ami-0adcddd3324248c4c) and followed the procedure step-by-step, and it works fine. Make sure the security group attached to your EC2 instance allows inbound access on port 80

@ashwinsingh2007

This comment has been minimized.

Copy link

@ashwinsingh2007 ashwinsingh2007 commented Dec 28, 2019

thanks for Amazon Linux 2 part

@DumiM

This comment has been minimized.

Copy link

@DumiM DumiM commented May 6, 2020

Thanks for this mate, helped out so much!

@LosD

This comment has been minimized.

Copy link

@LosD LosD commented May 30, 2021

The nginx1.12 topic is WAY outdated (and marked as such by the amazon-linux-extras command, if you have the topic enabled. It's hidden if you don't). Use the topic nginx1 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment