Skip to content

Instantly share code, notes, and snippets.

@sumardi
Last active November 2, 2024 14:29
Show Gist options
  • Save sumardi/5559803 to your computer and use it in GitHub Desktop.
Save sumardi/5559803 to your computer and use it in GitHub Desktop.
Install PHP-FPM, Nginx & MySQL on EC2 with Amazon Linux AMI
# Install linux update, followed by GCC and Make
sudo yum -y update
sudo yum install -y gcc make
# Install Nginx and PHP-FPM
sudo yum install -y nginx php-fpm
# Install PHP extensions
sudo yum install -y php-devel php-mysql php-pdo \
php-pear php-mbstring php-cli php-odbc \
php-imap php-gd php-xml php-soap
# Install PHP-APC
sudo yum install -y php-pecl-apc
sudo yum install -y pcre-devel
# Install MySQL
sudo yum -y install mysql-server mysql
# Nginx Configuration
sudo nano /etc/nginx/conf.d/default.conf
# PHP-FPM Configuration
sudo nano /etc/php-fpm.d/www.conf
# Autostart Nginx, PHP-FPM and MySQL
sudo chkconfig nginx on
sudo chkconfig mysqld on
sudo chkconfig php-fpm on
location / {
root /var/www/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/
html$fastcgi_script_name;
include fastcgi_params;
}
[...]
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
;listen.owner = nobody
listen.owner = nginx
;listen.group = nobody
listen.group = nginx
;listen.mode = 0666
listen.mode = 0664
user = nginx
group = nginx
[...]
Copy link

ghost commented Aug 26, 2015

This is great script - Would it be possible to have an instruction on how to install php v5.6 ?

@cybercussion
Copy link

I was able to do that with sudo yum install -y nginx php56-fpm.x86_64

Also FYI anyone that uses phpmyadmin if you ever add -
location ~ .css {
add_header Content-Type text/css;
}
location ~ .js {
add_header Content-Type application/x-javascript;
}
It will break the ability for get_scripts.js.php to work correctly.

@chrsc
Copy link

chrsc commented Oct 14, 2015

@moball php56 is available in Amazon Linux now. Use php56 instead of php for the yum installs.

@JohnWestlund
Copy link

@santoshachari looks like your resolved this for yourself on serverfault

server {

       location / {
                root   /var/www/html;
                index  index.php index.html index.htm;
        }

        location ~ \.php$ {
        fastcgi_pass    unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
            include         fastcgi_params;
         }

}

@santoshachari
Copy link

Thanks JohnWestlund. It was an arrow shot in the dark.

@cybercussion You can check my gist here: https://gist.github.com/santoshachari/23ef757d593e5ccd3d31

@aindong
Copy link

aindong commented Nov 18, 2015

Hi, I followed this but I'm getting 403 forbidden when I access my index.php,
here's my virtual.conf

server {
    location / {
        root   /home/ec2-user/medix-clinic/public;
        index  index.php index.html index.htm;
    }

    location ~ \.php$ {
        fastcgi_pass    unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include         fastcgi_params;
    }
}

@ravindranathbr
Copy link

ravindranathbr commented Sep 2, 2016

Instead of this
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;

try this
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

@timint
Copy link

timint commented Mar 30, 2017

sudo /etc/init.d/nginx start

Starting nginx: nginx: [emerg] "location" directive is not allowed here in /etc/nginx/conf.d/default.conf:1 [FAILED]

@jojomartius
Copy link

@timint put a

server {
listen 80;
root /var/www/html;
...
}

around the file pasted here and drop the old content where my ... was ;)

@Mrchux
Copy link

Mrchux commented Oct 11, 2017

I keep getting 502 Bad Gateway using nginx/1.12.1 on AWS EC2, is there something i'm doing wrong?

below is my nginx.conf


# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    index   index.php index.html index.htm;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  my-public-ip-here;
        root /var/www/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

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

        # redirect server error pages to the static page /40x.html
        #
        #error_page 404 /404.html;
        #   location = /40x.html {
        #}

        # redirect server error pages to the static page /50x.html
        # #    location = /50x.html {
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;

#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        # It is *strongly* recommended to generate unique DH parameters
#        # Generate them with: openssl dhparam -out /etc/pki/nginx/dhparams.pem 2048
#        #ssl_dhparam "/etc/pki/nginx/dhparams.pem";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#        ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}





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