Skip to content

Instantly share code, notes, and snippets.

@keidarcy
Last active September 22, 2020 05:25
Show Gist options
  • Save keidarcy/fd3e5423968d21e96d2b3d17e2c39f4e to your computer and use it in GitHub Desktop.
Save keidarcy/fd3e5423968d21e96d2b3d17e2c39f4e to your computer and use it in GitHub Desktop.
latest PHP + Ngnix + Redis + Mysql + Node + Composer + Git in Amazon Linux 2(2020.08)

OS

$ cat /etc/system-release
Amazon Linux release 2 (Karoo)

set LC_CTYPE to default locale: No such file or directory locale: Cannot set LC_ALL to default locale: No such file or directory

  • sudo vi /etc/environment
  • add this two lines
LANG=en_US.utf-8
LC_ALL=en_US.utf-8
  • exit and ssh in again, should no error

  • docker

## download docker
sudo yum install -y docker
sudo systemctl start docker
sudo usermod -a -G docker ec2-user

## download docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/1.26.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Instance Restart

sudo systemctl start php74-php-fpm
sudo systemctl start nginx
sudo systemctl start redis
sudo systemctl start mysqld

Install PHP

sudo yum install php

php -v  // PHP 5.4.16

sudo amazon-linux-extras install epel

sudo yum install epel-release

sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

sudo yum install -y php74 php74-php php74-php-fpm php74-php-pdo php74-php-mysqlnd

ln -fs /usr/bin/php74 /usr/bin/php

php -v // PHP 7.4.9

Install Composer

// go https://getcomposer.org/download/ get composer.phar

mv composer.phar /usr/local/bin/composer

composer -v // Composer version 1.10.10

Install Nodejs

follow this offical

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

. ~/.nvm/nvm.sh

nvm install node

node -e "console.log('Running Node.js ' + process.version)" // Running Node.js v14.8.0

Install Git

sudo yum install git

git version // git version 2.23.3

Install Ngnix

sudo yum -y install nginx

nginx -v // nginx version: nginx/1.16.1

systemctl start nginx
  • check your ip address now. Thank you for using Amazon Linux 2. should be shown

swap is 0 can't run composer install free -m

              total        used        free      shared  buff/cache   available
Mem:            957          77          82           0         797         736
Swap:             0           0           0
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -m
              total        used        free      shared  buff/cache   available
Mem:            957          79          76           0         800         734
Swap:          2047           0        2047
sudo systemctl restart php-fpm
sudo systemctl restart nginx

Config php-fpm and nginx

php-fpm

user = nginx
group = nginx

listen = 127.0.0.1:9001 // or /run/php-fpm/www.sock

listen.owner = nginx
listen.group = nginx
listen.mode = 0660


ngnix

 location / {
         try_files $uri $uri/ /index.php?$query_string;
 }

 location ~ \.php$ {
         fastcgi_pass 127.0.0.1:9001; # or /run/php-fpm/www.sock
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_index index.php;
         include fastcgi_params;
 }

Install MySQL

sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm -y

# for mysql 5.7
$ sudo yum-config-manager --disable mysql80-community

$ sudo yum-config-manager --enable mysql57-community

$ sudo yum install mysql-community-server -y

$ mysqld --version
mysqld  Ver 5.7.22 for Linux on x86_64 (MySQL Community Server (GPL))

$ sudo cat /var/log/mysqld.log | grep password # for default password

# initialize setting
$ mysql_secure_installation

Install Redis

$ sudo amazon-linux-extras install redis4.0

$ redis-server --version
Redis server v=4.0.10

Certbot and SSL

$ sudo wget -r --no-parent -A 'epel-release-*.rpm' http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/

$ sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm

$ sudo yum-config-manager --enable epel*

$ sudo yum install certbot python2-certbot-nginx

$ sudo certbot --nginx

// Congratulations! You have successfully enabled https://example.com

Install Supervisor

$ sudo easy_install pip
$ pip install supervisor
$ supervisord -v
4.2.1
echo_supervisord_conf > /etc/supervisord.conf
  • Edit Supervisor config file
// /etc/supervisor.conf
[unix_http_server]
file=/var/run/supervisor.sock

[supervisord]
logfile=/var/log/supervisord.log
logfile_maxbytes=50MB               ;デフォルト
logfile_backups=10                  ;デフォルト
loglevel=info                       ;デフォルト
pidfile=/var/run/supervisord.pid
nodaemon=false                      ;デフォルト
minfds=1024                         ;デフォルト
minprocs=200                        ;デフォルト

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock

[include]
files = /etc/supervisord.d/*.ini
  • Create Service
// /etc/systemd/system/supervisord.service
[Unit]
Description=Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
ExecStart=/usr/bin/supervisord -n -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=50s

[Install]
WantedBy=multi-user.target
  • Test Program
#!/bin/bash

while :; do
    echo "Hello, world!"
    sleep 1
done
// /etc/supervisor/hello.ini

[program:hello]
command=/home/ec2-user/hello.sh
process_name=%(program_name)s_%(process_num)02d
autostart=true
autorestart=true
user=ec2-user
numprocs=1
redirect_stderr=true
stdout_logfile=/tmp/hello.log

  • Superviosr Commands
$ systemctl start supervisord
$ systemctl status supervisord
$ systemctl stop supervisord

$ sudo systemctl enable supervisord.service

supervisorctl status

Japanese Usecase

$ sudo sed -i -e 's/;default_charset = "iso-8859-1"/default_charset = "UTF-8"/g' /etc/php.ini
$ sudo sed -i -e "s/;mbstring.language = Japanese/mbstring.language = Japanese /g" /etc/php.ini
$ sudo sed -i -e "s/;mbstring.internal_encoding = EUC-JP/mbstring.internal_encoding = UTF-8/g" /etc/php.ini
$ sudo sed -i -e "s/;mbstring.http_input = auto/mbstring.http_input = pass/g" /etc/php.ini
$ sudo sed -i -e "s/;mbstring.http_output = SJIS/mbstring.http_output = pass/g" /etc/php.ini
$ sudo sed -i -e "s/;mbstring.http_input = auto/mbstring.http_input = pass/g" /etc/php.ini
$ sudo sed -i -e "s/;mbstring.encoding_translation = Off/mbstring.encoding_translation = Off/g" /etc/php.ini
$ sudo sed -i -e "s/;mbstring.detect_order = auto/mbstring.detect_order = auto/g" /etc/php.ini
$ sudo sed -i -e "s/;date.timezone =/date.timezone = Asia\/Tokyo/g" /etc/php.ini

Useful Command

yum list | grep "\-mbstring"

systemctl list-unit-files --type=service | grep php

sudo systemctl start php-fpm

sudo systemctl status nginx

sudo amazon-linux-extras list | rg sql

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