Blog

After creating and starting an EC2 instance, if you choose a minimal AMI, you can proceed to installing nginx and the latest php.  This tutorial assumes you are using RedHat Enterprise, but it should apply to CentOS too. 

Setup

Note: To facilitate getting stuff done, and to minimize permission problems, disable selinux.
If you have utilized and configured used services with selinux successfully before, then keep enabled and configure it appropriately.
https://serverfault.com/questions/30796/reasons-to-disable-enable-selinux

Temporarily disable selinux
> # sudo setenforce 0

Permanently disable selinux
> sudo vi /etc/selinux/config
SELINUX=disabled

Update OS
> sudo yum check-update
> sudo yum update -y

If the kernel was updated, reboot
> sudo reboot 

Note: Included is some information if you try to use Amazon Linux 2 as the AMI,
but it does seem to have fewer packages, related to php anyway.

amazon-linux-extras is a mechanism in Amazon Linux 2 to enable the consumption of new versions of application software on a stable operating system that is supported until June 30, 2023. Extras help alleviate the compromise between the stability of the OS and freshness of available software.

Enable Extra Packages for Enterprise Linux (EPEL) repo
Amazon Linux 2
> sudo amazon-linux-extras install epel

RedHat Enterprise (version 8)
> sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Also install remi repo to install php versions greater than the RedHat php versions
> sudo yum install http://rpms.remirepo.net/enterprise/remi-release-8.rpm

Install some extra utilities
> sudo yum install htop iftop iotop

Install nginx

Amazon Linux 2
> sudo amazon-linux-extras enable nginx1

See which version of nginx is available
> sudo yum info nginx
nginx 1.14.1

Install nginx
> sudo yum install nginx

Test the default install
Amazon Linux 2
> sudo service nginx start

RedHat Enterprise
> sudo systemctl start nginx

View you EC2 instance via its default url (find in the AWS EC2 Console)
http://ec2-1-2-3-4.us-east-9.compute.amazonaws.com

Enable nginx to run at boot
Amazon Linux 2
> sudo chkconfig nginx on

RedHat Enterprise
> sudo systemctl enable nginx

Additional actions for systemctl
> sudo systemctl start nginx      # start the server
> sudo systemctl stop nginx       # stop the server
> sudo systemctl restart nginx    # restart the server
> sudo systemctl reload nginx     # reload the server
> sudo systemctl status nginx     # get status of the server 

Install php

Amazon Linux 2
> sudo amazon-linux-extras enable php7.3

Note: php-imap is not available in Amazon Linux 2 (as of 2020-01-10)

See versions of php avail
> sudo yum module list php
Remi's Modular repository for Enterprise Linux 8 - x86_64
Name            Stream           Profiles                                 Summary
php             remi-7.2         common [d], devel, minimal               PHP scripting language
php             remi-7.3         common [d], devel, minimal               PHP scripting language
php             remi-7.4 [e]     common [d] [i], devel, minimal           PHP scripting language
Red Hat Enterprise Linux 8 for x86_64 - AppStream from RHUI (RPMs)
Name            Stream           Profiles                                 Summary
php             7.2 [d]          common [d], devel, minimal               PHP scripting language
php             7.3              common [d], devel, minimal               PHP scripting language

Enable and install php 7.4
> # sudo dnf module reset php  # resets back to RedHat version
> sudo dnf module install php:remi-7.4

Install some common packages
> sudo yum install php-cli php-common php-fpm php-json php-mbstring php-xml \
php-pdo php-mysqlnd php-gd php-gmp php-xmlrpc php-pecl-mcrypt php-pecl-zip php-imap

Start php-fpm
> sudo systemctl start php-fpm

Enable php-fpm at boot
> sudo systemctl enable php-fpm
> sudo systemctl status php-fpm

Edit php-fpm to run with nginx user, replacing the httpd or apache user
> sudo vi /etc/php-fpm.d/www.conf
user = nginx
group = nginx

Restart the services
> sudo systemctl restart php-fpm
> sudo systemctl restart nginx    

Note, if there are multiple apps per EC2, consider a php-fpm pool per app ie replace www.conf with app1.conf, app2.conf etc

Create a test php page in the default web dir
> sudo vi /usr/share/nginx/html/info.php
<?php

phpinfo();

You should be able to view the info page and php info
http://ec2-1-2-3-4.us-east-9.compute.amazonaws.com/info.php

You should now have nginx and php installed and usable.
But you will probably want to configure your application code user and permissions, which will be a later post.

-End of Document-
Thanks for reading

While Amazon Web Services (AWS) provides allot of services and administrative capabilities thru their Console web application, you still have to do some things manually. 

After creating a EC2 instance, you may find out that the Amazon Machine Image (AMI), ie install image, you used had a different OS/root partition size than the size you allocated in the AWS Console, which is where this tutorial comes into play. 

 
If you are using an AMI which has the root partition as 8GB, and you have launched an EC2 instance with 16GB, you can resize the root partition.

Resize volume

Check the used partition size via lsblk, which lists information about all available or the specified block devices
> sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1     259:1 0 8G  0 disk
├─nvme0n1p1 259:2 0 1M  0 part
└─nvme0n1p2 259:3 0 8G  0 part /


First we will extend the partition, and then we will extend the file system

Install growpart to extend a partition in a partition table to fill the available space
> sudo yum install cloud-utils-growpart 

Extend the partition, the first option is the volume ie nvme0n1, the second options is the partition number ie the 1 in p1
> sudo growpart /dev/nvme0n1 1 

Verify 
> sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1     259:1 0 16G 0 disk
├─nvme0n1p1 259:2 0 1M  0 part
└─nvme0n1p2 259:3 0 16G 0 part /
The 8GB partition now lists as 16GB

But the file system is still 8GB
> df -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2   8G  1.9G   8G  24% /
 
Now extend the file system 
> sudo xfs_growfs /
 
Verify
> df -h 
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p2   16G  1.9G   16G  12% /
 
Your good to use your fully allocated partition now.

-End of Document-
Thanks for reading

While Amazon Web Services (AWS) provides allot of services and administrative capabilities thru their Console web application, you still have to do some things manually.

 After creating a EC2 instance, you may want to attach additional storage.  The additional storage can be used to host your application independent of the OS/root partition, allowing you to more easily migrate, backup, and manage your application and it's data.

 After creating the additional storage and attaching the volume to your EC2 instance, you still need to tell the OS on the EC2 about the extra storage, which is where this tutorial comes into play.

 

Mount attached volume 

List partitions via lsblk, which lists information about all available or the specified block devices
> sudo lsblk
NAME        MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

nvme1n1     259:0    0  50G  0 disk            <- no mount point
nvme0n1     259:1    0  16G  0 disk
├─nvme0n1p1 259:2    0   1M  0 part
└─nvme0n1p2 259:3    0  16G  0 part /

nvme0n1
Is the OS/root partition 

nvme1n1     
Is the external attached volume 

Verify that there is no data on the partition
> sudo file -s /dev/nvme1n1

Response if no file system thus no data
/dev/nvme1n1: data

Response if the partition has already been formatted
/dev/nvme1n1: SGI XFS filesystem data

If no data, create a file system
> sudo mkfs -t xfs /dev/nvme1n1 

Make the mount directory, which can be any name, but `data` is generic enough
> sudo mkdir /data

Mount the partition to the directory
> sudo mount /dev/nvme1n1 /data

Edit fstab to mount on boot
fstab defines your volumes and mount points at boot, so make a copy first
> sudo cp /etc/fstab /etc/fstab.orig 

Find the UUID of device, which will be used in fstab to identity the volume
> sudo blkid 

Edit fstab; Use your UUID; match existing entry spacing
the option nofail allows the boot sequence to continue even if the drive fails to mount
> sudo vi /etc/fstab
UUID=123ebf5a-8c9b-1234-1234-1234f6f6ff30 /data      xfs     defaults,nofail 0 2 

To verify the fstab configuration works, without rebooting, unmount and then auto mount the volume
> sudo umount /data> sudo mount -a

List partitions, and you will see your data directory, which you can now utilize
> lsblk
NAME        MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme1n1     259:0    0  50G  0 disk /data      <- it worked
nvme0n1     259:1    0  16G  0 disk
├─nvme0n1p1 259:2    0   1M  0 part
└─nvme0n1p2 259:3    0  16G  0 part /

> ls -l /data

-End of Document-
Thanks for reading

Sometimes you may have multiple code repositories which you always end up checking out together, and deploying together.  Or maybe it seemed like a good organization idea to separate your code based on functionality, but in practice, it has become cumbersome.  

With a few commands, you can merge the multiple repositories into one repository, keeping their history.  You can also keep the separate repositories in their own sub directory, thus maintaining the organization of your code, but utilize one repository to facility development, branches, and deployments. 

Example list of current separate repositories:
    ls 
    /local_git/old_project_1
    /local_git/old_project_2

Update your local repositories with the latest code
Pull and Commit/Push any changes 

Create a new repository for the combined project and push
    cd /local_git
    mkdir new_combined_project
    touch README.md
    git init .
    git commit -m "add readme"
    git remote add origin remote https://github.com/your_repo_url
    git push 

Add the first separate repository to the new combined repository
    cd /local_git/new_combined_project
    git remote add old_project_1 ../old_project_1 

List repositories
    git remote -v    

    old_project_1  ../old_project_1.git (fetch)
    old_project_1  ../old_project_1.git (push)
    origin  https://github.com/your_repo_url.git (fetch)
    origin  https://github.com/your_repo_url.git (push)

Note: If you get the wrong path to your local repository, you can remove repository entries
    git remote remove old_project_1 

Fetch the branch/tags/master for the first separate repository
    git fetch old_project_1 --tags 

Merge the files and histories for the first separate repository
    git merge --allow-unrelated-histories old_project_1/master
    list of files 

You should have a list of files and directories from the first separate repository
    ls
    your files from old_project_1

Optionally create a sub directory to move the files into.
    cd /local_git/new_combined_project
    mkdir old_project_1   

Move the files and folders into the new nested directory
    git mv !(old_project_1|old_project_2) old_project_1 

Note: If you just mv or cut/paste the files into the new directory, git may not persist the history for those files.

Note: !() excludes the listed directory/file

Note: If you get an error about unknown bash command !, enable the glob extension
    shopt -s extglob 

Your directories/files from the first separate repository should now be in
    /local_git/new_combined_project/old_project_1
and you should have the git history for old_project_1 

Consolidated commands repeating for the second separate repository
    cd /local_git/new_combined_project
    ls
    git remote add old_project_2 ../old_project_2
    git remote -v
    git fetch old_project_2 --tags
    git merge --allow-unrelated-histories old_project_2/master
    mkdir old_project_2
    git mv !(old_project_1|old_project_2) old_project_2
    ls
    ls old_project_2 

Your multiple separate repositories are now merged into one repository, with their history.  After verifying by checking out to a new directory, viewing it's history, you can remove the prior separate repositories.

Reference: https://stackoverflow.com/a/10548919

 

-End of Document-
Thanks for reading