Skip to content

Instantly share code, notes, and snippets.

@RahulDey12
Last active November 29, 2021 15:49
Show Gist options
  • Save RahulDey12/d2021c495f835297226df9c73c389332 to your computer and use it in GitHub Desktop.
Save RahulDey12/d2021c495f835297226df9c73c389332 to your computer and use it in GitHub Desktop.
Amazon Linux 2 PHP setup
#!/bin/sh
if [ $(id -u) -ne 0 ]; then
echo "You should be root. Try with sudo" >&2
exit 1
fi
show_loading()
{
TEXT=$1
if [ $? -eq 0 ]; then
echo "$TEXT Succeded."
else
echo "$TEXT Failed."
fi
}
# Updating dipendency
echo "Updating Dependency..."
yum update -y > /dev/null
show_loading "Dependency Update"
# Installing & configuring webserver
echo "Installing NGINX..."
amazon-linux-extras install -y nginx1 > /dev/null
show_loading "NGINX Installation"
systemctl enable nginx
# Installing PHP
echo "Installing & Configuring PHP..."
amazon-linux-extras enable php8.0 > /dev/null \
&& yum clean metadata > /dev/null \
&& yum install -y php-{cli,common,mysqli,pdo,gd,mbstring,xml,intl,readline,bcmath,soap,exif,fpm,pear,devel} make gcc autoconfig > /dev/null \
&& pecl channel-update pecl.php.net > /dev/null \
&& pear channel-update pear.php.net > /dev/null \
&& printf "\n" | pecl install redis -q > /dev/null
show_loading "PHP Installation"
INI_FILE=$(php --ini | sed "s/Loaded Configuration File:\s*//" | head -n 2 | tail -n 1)
echo "extension=redis.so" >> "${INI_FILE}"
systemctl enable php-fpm
# Installing Composer
echo "Installing Composer..."
php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer > /dev/null
show_loading "Composer Installation"
# Install Mariadb Client
echo "Installing necessary tools..."
yum install -y mariadb > /dev/null \
&& amazon-linux-extras install -y epel > /dev/null \
&& yum update -y > /dev/null \
&& yum install jq -y > /dev/null \
&& yum install -y ansible > /dev/null \
&& yum install -y amazon-efs-utils > /dev/null
show_loading "Tools Installation"
# Configuring NGINX
echo "Configuring NGINX..."
cat << EOF > /etc/nginx/default.d/php.conf
# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name;
include fastcgi_params;
fastcgi_param HTTP_X_FORWARDED_PROTO \$cloudfront_proto;
fastcgi_param HTTP_X_FORWARDED_PORT \$cloudfront_port;
}
EOF
cat << EOF > /etc/nginx/conf.d/cf-request.conf
map \$http_cloudfront_forwarded_proto \$cloudfront_proto {
default "http";
https "https";
}
map \$cloudfront_proto \$cloudfront_port {
default "80";
https "443";
}
EOF
cat << EOF > /etc/nginx/conf.d/site.conf
server {
listen 80;
server_name site.com;
root /data/site/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
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; }
error_page 404 /index.php;
include /etc/nginx/default.d/*.conf;
location ~ /\.(?!well-known).* {
deny all;
}
}
EOF
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old
cat /etc/nginx/nginx.conf | sed 's/listen.*//p' | tee /etc/nginx/nginx.conf > /dev/null
echo "Configuring NGINX Done."
# starting services
systemctl start nginx
systemctl start php-fpm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment