Monday, February 17, 2020

AWS RedHat install nginx and php


After creating and starting an EC2 instance, install 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.

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-2.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

No comments:

Post a Comment