Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shekarsiri/e66ec7ee3e95f3d08fffc2a22eecc978 to your computer and use it in GitHub Desktop.
Save shekarsiri/e66ec7ee3e95f3d08fffc2a22eecc978 to your computer and use it in GitHub Desktop.

LEMP in Ubuntu 16.04 Server - AWS EC2/RDS

  1. Pre-usage:
sudo apt-get update && sudo apt-get upgrade -y
  1. Add ubuntu or the user used to sudo group:
sudo usermod -aG sudo ubuntu
  1. Check if ssh connection allows root access (if yes, change PermitRootLogin to no or prohibit-password:
sudo vi /etc/ssh/sshd_config

PermitRootLogin prohibit-password

  1. Install Nginx:
sudo apt-get install nginx
  1. Add ssh connections as an allow option for firewall:
sudo ufw allow ssh
  1. Add nginx server as an allow option for firewall:
sudo ufw allow 'Nginx HTTP'
  1. Enable the firewall:
sudo ufw enable
  1. Check status of firewall:
sudo ufw status
  1. Add PHP repository from Ondrej:
sudo add-apt-repository ppa:ondrej/php
  1. Update:
sudo apt-get update
  1. Install PHP 7.1 FPM and most used extensions:
sudo apt-get install php7.1-fpm php7.1-curl php7.1-soap php7.1-pgsql php7.1-mysql php7.1-sqlite3 php7.1-mbstring php7.1-xml php7.1-mcrypt php7.1-zip
  1. Config PHP to make it safer:

Before doing it, check where its the php.ini file by typing: php -i | grep "Loaded Configuration File"

sudo vi /etc/php/7.1/fpm/php.ini

Edit cgi.fix_pathinfo:

cgi.fix_pathinfo=0

Edit expose_php:

expose_php = off

  1. Config Nginx:
sudo vi /etc/nginx/sites-available/default

Add index.php to list of files to interpret when the sites is rendered

Hide nginx version in http header:

sudo vi /etc/nginx/nginx.conf

Edit: server_tokens off

Restart it:

sudo service nginx restart
  1. Install Composer:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

php composer-setup.php

php -r "unlink('composer-setup.php');"

mv composer.phar /usr/local/bin/composer
  1. Clone/deploy the project into EC2, enter the project folder and install dependencies:
composer install
  1. Start services:
sudo service php7.1-fpm start && sudo service nginx restart
  1. Enable HTTP and HTTPS on firewall:
sudo ufw allow http && sudo ufw allow https
  1. Add folder/file permissions into cache directory:
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
  1. Use Let's Encrypt to install TLS certificates:
sudo add-apt-repository ppa:certbot/certbot

sudo apt-get update

sudo apt-get install python-certbot-nginx

In case of using MySQL in EC2 instance instead of using a RDS service:

  • Install MySQL Server:
sudo apt-get install mysql-server
  • Run secure installation of MySQL:
mysql_secure_installation

Useful Links

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