$ cat contents/tech/830.md

How to Install WordPress on Amazon Linux2

公開日
カテゴリ
分量
1,552 文字 ・ 約2分

This the procedures help you install a nginx web server with PHP and MariaDB support on your Amazon Linux 2 instance


Enviroments

Use the latest package provided by AWS.

  • Amazon Linux 2
  • Nginx 1.12.x
  • PHP 7.2.x
  • PHP-FPM 7.x
  • MariaDB 5.x
  • WordPress 5.x lang Japanese

Deploying EC2 instance

You need to set up a security group to allow TCP ports 22, 80, 443 from your environment.

  • AMI: Amazon Linux 2
  • Instance Type: t3a.micro
  • Storage: gp2 8GB
  • etc…

Setting time zone

reference: https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/set-time.html

Set timezone.

$ sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

Various installations

reference: https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/amazon-linux-ami-basics.html

Install Nginx

$ sudo amazon-linux-extras install nginx1.12

Install MariaDB

$ sudo yum install mariadb mariadb-server

Install PHP

sudo amazon-linux-extras install php7.2

Install PHP etc…

$ sudo yum install php-mysqlnd php-mbstring php-pear php-fpm php-mcrypt php-gd php-opcache php-pecl-apcu-bc

Setting Nginx

Make site.conf

server {
  listen 80;
  server_name www.example.net;
  root /var/www/site;
  index index.php index.html index.htm;
  access_log /var/log/nginx/site_access.log;
  error_log /var/log/nginx/site_error.log;

  location ~* /wp-config.php {
    deny all;
  }

  location ~ .php$ {
    fastcgi_pass unix:/run/php-fpm/www.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include fastcgi_params;
  }
}

Config check

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Start service & enable to start the Nginx service

$ sudo systemctl enable nginx
$ sudo systemctl start nginx

When you access “http://[The IP for your EC2]” you can acess the “Nginx default page”

Setting PHP

Editing php.ini

;hidden version
expose_php = Off
 
;set timezone
date.timezone = "Asia/Tokyo"
 
;set language codde
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = auto

Setting PHP-FPM

Editing www.conf

user = nginx
group = nginx

Start service & enable to start the PHP-FPM service

$ sudo systemctl enable php-fpm
$ sudo systemctl start php-fpm

Setting MariaDB

Start service & enable to start the PHP-FPM service

$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb

Initial settings for MariaDB database

$ sudo mysql_secure_installation
...
Enter current password for root (enter for none):
-> press "Enter key"
...
Set root password? [Y/n] Y
...
-> setting mariadb root password
...
Remove anonymous users? [Y/n] Y
...
Disallow root login remotely? [Y/n] Y
...
Remove test database and access to it? [Y/n] Y
...
Reload privilege tables now? [Y/n] Y
...
Thanks for using MariaDB!

Editing my.cnf

[mysqld]
character-set-server = utf8

Creating user for mariadb database

“user” ,”password”, “wordpress” are your self

$ mysql -u root -p
CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* TO "user"@"localhost" IDENTIFIED BY "password";
FLUSH PRIVILEGES;

Install the WordPress

Do download and put files

$ curl -O https://ja.wordpress.org/latest-ja.tar.gz
$ tar xzvf latest-ja.tar.gz
$ sudo mkdir -p /var/www/site
$ sudo cp -rp wordpress/* /var/www/site/
$ sudo chown -R nginx:nginx /var/www/site
$ cd /var/www/site
$ sudo mv wp-config-sample.php wp-config.php

Editing wp-config.php

/** database name */
define('DB_NAME', 'wordpress');
 
/** mariadb user name */
define('DB_USER', 'user');
 
/** mariadb password */
define('DB_PASSWORD', 'password');
 
/** mariadb hostname or ip */
define('DB_HOST', 'localhost');
 
/** table character set */
define('DB_CHARSET', 'utf8');

and paste secret-key

Access “https://api.wordpress.org/secret-key/1.1/salt/”
and paste secret-key

for example

define('AUTH_KEY',         'afu afu afuuufufuufuf';
define('SECURE_AUTH_KEY',  'afufufufuuuuu';
.....
...

Reboot EC2

$ sudo reboot

For test setting

Rewrite hosts file on your PC and access the site “www.example.net” on EC2.

for example MacOS /etc/hosts
(for Windows, C:\Windows\System32\drivers\etc\hosts)

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
 
<PublicIP for EC2> www.example.net

How? Was the WordPress initial settings screen displayed?

Go to wordpress install page http://www.example.net/wp-admin/install.php

After this, set up WordPress, set up DNS settings correctly for production, and set up EC2 security groups appropriately.

Don’t forget to restore your PC’s hosts settings.