Summary of installing PHP7.2 on EC2 (Amazon Linux 2) and setting php.ini

Amazon Linux will end support on December 31, 2020, so You now need to migrate to the successor OS, Amazon Linux 2.

I couldn't simply update the OS, so I created a new instance of Amazon Linux 2 and re-installed the middleware.

I thought that I would like to save the trouble of those who do the same thing later, so I summarized it. I hope it helps you, "I want to run PHP on EC2 for the time being !!!": pray:

Premise

■ It will be smooth if you finish the preparation of VPC and subnet first. (Because it is linked when creating an EC instance)

If you like, please refer to the following articles! ・ Procedure from creating VPC to creating public / private subnet ①Procedure from creating VPC to creating public / private subnet (2)

■ I will not touch on the construction procedure using CloudFormation this time.

Rough flow

  1. Create an EC2 instance
  2. Set the time zone to Japan
  3. Set the language to Japanese
  4. Set the host name ①
  5. Set the host name ②
  6. Install the LAMP environment
  7. Install the mb module
  8. Install the php-xml module
  9. Set php.ini

1. Create an EC2 instance

** ▼ Reference: Procedure to create EC2 instance from AWS console **

When you install the LAMP environment, you need to ssh to your EC2 instance. Be sure to select Select an existing key pair or Create a new key pair when creating an EC2 instance.

** ▼ Command to ssh to EC2 instance: **

ssh -i [Key pair path] ec2-user@[Public IPv4 address]

2. Set the time zone to Japan

sudo cp /usr/share/zoneinfo/Japan /etc/localtime
sudo vi /etc/sysconfig/clock

/etc/sysconfig/clock


#ZONE="UTC"
ZONE="Asia/Tokyo"
UTC=true

3. Set the language to Japanese

sudo vi /etc/sysconfig/i18n

/etc/sysconfig/i18n


LANG=ja_JP.UTF-8

4. Set the host name ①

Screenshot 2020-12-23 17.44.53.png

sudo hostnamectl set-hostname <hostname>

** ▼ Reference: Rename Amazon Linux Instance Hostname --Amazon Elastic Compute Cloud **

It is difficult to understand if you keep the default, so it is recommended to change the host name to something that is easy to understand.

5. Set the host name ② * If you don't need it, skip it.

sudo vi /etc/cloud/cloud.cfg

Add preserve_hostname: true to the end

** ▼ Reference: How to fix the host name when booting from a custom AMI on Amazon Linux 2 | Developers.IO **

Get a custom AMI → When I started a new instance from the custom AMI, the host name setting was disabled, so it is a setting to keep it enabled.

6. Install the LAMP environment

** ▼ Reference: Tutorial: Installing a LAMP Web Server on Amazon Linux 2-Amazon Elastic Compute Cloud **

7. Install the mb module

** ▼ Reference: Using mbstring with php on Amazon Linux-Qiita **

PHP has a variety of functions for working with multibyte strings. (Example: mb_substr)

If you don't install the mb module, you will get a Fatal error when you run PHP.

8. Install the php-xml module

sudo yum install --enablerepo=remi,remi-php70 php-xml
sudo systemctl restart httpd

You can check that it is installed without any problem with the following command.

[ec2-user@XXXXXX ~]$ yum list installed | grep php-xml
php-xml.x86_64                        7.2.34-1.amzn2                 @amzn2extra-php7.2
[ec2-user@XXXXXX ~]$ 

I got a PHP error Fatal error: Class'DOMDocument' not found because I didn't have the php-xml module installed: japanese_goblin:

9. Set php.ini

** ▼ Reference: [PHP] Settings you want to do after installing PHP --Qiita **

If you copy the initial state /etc/php.ini, make a backup, and then change the settings, you can restore it in case something goes wrong, so you can rest assured.

The session.hash_function etc. described as" security settings "in the above article have been removed from PHP 7.1.0, so no settings are required.

reference

--Rename Amazon Linux Instance Hostname --Amazon Elastic Compute Cloud -How to fix the host name when booting from a custom AMI on Amazon Linux 2 | Developers.IO -Tutorial: Install LAMP Web Server on Amazon Linux 2-Amazon Elastic Compute Cloud --Using mbstring with php on Amazon Linux --Qiita -[PHP] Settings you want to do after installing PHP-Qiita -PHP: Runtime Settings --Manual

Recommended Posts

Summary of installing PHP7.2 on EC2 (Amazon Linux 2) and setting php.ini
Install pyenv on EC2 (Amazon Linux)
Learn sshd_config and authorized_keys (on Amazon Linux 2)
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
[Python/Django] Summary of frequently used commands (4) -Part 2- <Production operation: Amazon EC2 (Amazon Linux 2)>
[Python/Django] Summary of frequently used commands (4) -Part 1- <Production operation: Amazon EC2 (Amazon Linux 2)>
Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2
Install Python3 and Django on Amazon Linux (EC2) and run your web server
Summary of the differences between PHP and Python
Install PHP 7 series on Amazon Linux 2 with Amazon Linux Extras
Create Amazon Linux with AWS EC2 and log in
OpenVPN Summary + Amazon Linux2
Compile and install MySQL-python for python2.7 on amazon linux
[AWS EC2] How to install Maven on Amazon Linux 2
[AWS EC2] How to install only MySQL client on Amazon Linux 2 and connect to RDS
Announcing the availability of Java 11 LTS on Amazon Linux 2
Install LAMP on Amazon Linux 2 and build a WordPress environment.
A rough summary of the differences between Windows and Linux
[AWS EC2] Settings you want to do on Amazon Linux 2
Summary of yum packages required for pip install on EC2
Launch NEM Symbol testnet node on AWS EC2 (Amazon Linux2)
Summary of Differences Between Ruby on Rails and Django ~ Basics ~
Addition of amazon linux swap
Recording and playback on Linux
Summary of Linux distribution types
Put jenv on Amazon Linux
Install tomcat 5.5 on Amazon Linux.
Use sshpass on Amazon linux2
Install Homebrew on Amazon Linux 2
A brief summary of Linux
Install strongSwan 5.9.1 on Amazon Linux 2
I tried to create an environment of MkDocs on Amazon Linux
Summary of differences between Python and PHP (comparison table of main items)
Setting up OpenSSH on Arch Linux
Install Python Pillow on Amazon Linux
Basic knowledge of Linux and basic commands
Install oracle java8 on amazon linux2
Completion of docker command on Linux
[Note] Install Imagick on Amazon Linux2
[Note] Run Django on Amazon Linux 2
Run docker-compose on Amazon Linux2 on ARM64
Introduce Python 3.5.2 environment on Amazon Linux
Summary of Python indexes and slices
Run cron on Amazon Linux (set on Linux)
Description and summary of what you need to install Chainer on Mac
The story of running the asp.net core 3.1 app on arm64 version Amazon Linux 2