Skip to content

Instantly share code, notes, and snippets.

@jojijacobk
Last active December 13, 2019 16:33
Show Gist options
  • Save jojijacobk/43614b34cb4206387af5b403969950e6 to your computer and use it in GitHub Desktop.
Save jojijacobk/43614b34cb4206387af5b403969950e6 to your computer and use it in GitHub Desktop.
Setup stack of (PHP, Nginx & MySQL) on AWS

Yum Install PHP

# For Amazon Linux Image in AWS EC2, enable yum repository for amazon linux extras to install PHP
# Enable PHP 7.3 repo
sudo amazon-linux-extras enable php7.3

# Install PHP 7.3
sudo amazon-linux-extras install php7.3

# View all available PHP packages
sudo yum list available php*

# Install any of such PHP packages
sudo yum install package-name

Configure PHP-FPM for NGINX

fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  PATH_INFO          $fastcgi_path_info;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;                                                                                                                            
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

Configure NGINX conf

/etc/nginx/nginx.conf

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

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

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" > $document_root$fastcgi_script_name '
                      '$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;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /home/ec2-user/apps/public;
 
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

/etc/nginx/default.d/php.conf

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

    fastcgi_param HTTP_PROXY "";
    fastcgi_pass php-fpm;
    fastcgi_index index.php;
    include fastcgi_params;
}

Yum Install MySQL

# Download MySQL yum repository from it's official website.
# Refer : https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

# Install MySQL yum repository
sudo yum localinstall <package>

# Verify the installed yum repository
sudo yum repolist enabled | grep "mysql.*-community.*"

# Install MySQL
sudo yum install mysql-community-server

# Start MySQL daemon
sudo service mysqld start

# Set MySQL root user
At the initial startup of server, a root user account 'root'@'localhost' is created, a temporary password is generated and stored in mysqld.log file.

# View temporary password for the new root user
sudo grep 'temporary password' /var/log/mysqld.log

# Change root password by logging in with the temporary password and set a custom password.
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxxxx';

# View all available MySQL packages
sudo yum --disablerepo=\* --enablerepo='mysql*-community*' list available

# Install any of such MySQL packages
sudo yum install package-name

Create a MySQL User

# Create a user
CREATE USER 'wpadmin'@'localhost' IDENTIFIED BY 'xxxxx';

# MySQL 8.0 version onwards has caching_sha2 authentication feature which is not supported by PHP 7.3.
# Fix this by following two steps:
# Step-1:
# Edit /etc/my.cnf
default-authentication-plugin=mysql_native_password
# Step-2:
ALTER USER wpadmin@localhost IDENTIFIED WITH mysql_native_password BY 'xxxxx';

# Grant privileges
GRANT ALL ON foo.* TO 'wpadmin'@'localhost';
FLUSH PRIVILEGES;

# Show granted privileges
SHOW GRANTS FOR 'wpadmin'@'localhost';

# Delete user
DROP USER 'wpadmin'@'localhost';

Create a MySQL Database

# Create a database
CREATE DATABASE IF NOT EXISTS foo;

# Delete database
DROP DATABASE IF EXISTS foo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment