Build an LNPP environment on Amazon Linux 2

It is a procedure to build an LNPP environment other than a LAMP environment on Amazon Linux2.

Comparison with LAMP

The following is a comparison of the LNPP environment constructed this time with the familiar LAMP environment.

LNPP LAMP
OS Linux Linux
WEB server Nginx Apache
DB PostgreSQL MySQL
Programming language PHP PHP

The WEB server is Nginx instead of Apache, and the DB is PostgreSQL instead of MySQL.

Environment, version information

procedure

1. Software installation

First, connect to the Amazon Linux 2 environment by SSH and install various software required to build the environment. This time, I decided to use the extras library of Amazon Linux2.

(1) Execute the following command to check the list of topics available in the Extras Library.

amazon-linux-extras

In the initial state, it should be as follows.

・ ・ (Omitted above)
 35  kernel-ng                available    [ =stable ]
 36  BCC                      available    [ =0.x  =stable ]
 37  mono                     available    [ =5.x  =stable ]
 38  nginx1=latest            available      [ =stable ]
 39  ruby2.6                  available    [ =2.6  =stable ]
 40  mock                     available    [ =stable ]
 41  postgresql11=latest      available     [ =11  =stable ]
 42  php7.4=latest            available     [ =stable ]
 43  livepatch                available    [ =stable ]
 44  python3.8                available    [ =stable ]
 45  haproxy2                 available    [ =stable ]
(Omitted below) ...

(2) Install Nginx, PostgreSQL, PHP. Execute the following three commands in order.

sudo amazon-linux-extras install nginx1
sudo amazon-linux-extras install postgresql11
sudo amazon-linux-extras install php7.4
sudo yum install postgresql-server postgresql-devel postgresql-contrib

(3) Execute the amazon-linux-extras command again and check that Nginx, PostgreSQL and PHP are enabled.

amazon-linux-extras

If it changes from available to enabled as shown below, it's OK!

・ ・ (Omitted above)
 35  kernel-ng                available    [ =stable ]
 36  BCC                      available    [ =0.x  =stable ]
 37  mono                     available    [ =5.x  =stable ]
 38  nginx1=latest            enabled      [ =stable ]
 39  ruby2.6                  available    [ =2.6  =stable ]
 40  mock                     available    [ =stable ]
 41  postgresql11=latest      enabled      [ =11  =stable ]
 42  php7.4=latest            enabled      [ =stable ]
 43  livepatch                available    [ =stable ]
 44  python3.8                available    [ =stable ]
 45  haproxy2                 available    [ =stable ]
(Omitted below) ...
  1. Nginx (1) First, execute the following command to check the Nginx settings.
vi /etc/nginx/nginx.conf

The document root for HTTP connections is /usr/share/nginx/html Therefore, create the file you want to display on the WEB page under it (of course you can change the settings)

By the way, the relevant part is as follows (the part with root).

・ ・ (Omitted above)
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
(Omitted below) ...

(2) Execute as follows to create index.php that displays PHP settings.

sudo vi /usr/share/nginx/html/index.php

When the file opens, copy and paste the following contents in insert mode → save.

python


<?php 
phpinfo();

(3) Start Nginx.

sudo systemctl start nginx.service

(4) Hit the following URL in your browser and check that the PHP settings are displayed.

http://Public IPv4 DNS/index.php

image.png

  1. PostgreSQL Next, set up PostgreSQL.

(1) First, execute the following command to initialize.

sudo postgresql-setup initdb

(2) Check the storage location of the setting file.

sudo find / -name pg_hba.conf

In my case, it was as follows.

/var/lib/pgsql/data/pg_hba.conf

(3) Open the setting file and change the contents as follows so that you can log in temporarily without a password (set in the following procedure).

sudo /var/lib/pgsql/data/pg_hba.conf

■ Before change

・ ・ (Omitted above)
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
(Omitted below) ...

↓ (Changed "ident" (2 places) to "trust")

■ After change

・ ・ (Omitted above)
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
(Omitted below) ...

(4) Start PostgreSQL, log in → set a password.

sudo systemctl start postgresql.service
psql -h localhost -p 5432 -U postgres

If "postgres = #" is displayed, you have logged in, so execute the following DDL to set a password.

python


alter role postgres with password 'postgres';

(5) Modify the configuration file as follows so that you cannot log in without a password.

・ ・ (Omitted above)
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
(Omitted below) ...

(6) Check if pdo_pgsql exists (for PDO connection this time)

php -m | grep pdo

If pdo_pgsql is not displayed, execute the following command to install it.

sudo yum install --enablerepo=remi,remi-php74 php-pgsql

(7) Restart fpm-php for the changes to take effect.

sudo systemctl restart php-fpm

(8) Create postgres.php to check the connection from your browser.

sudo vi /usr/share/nginx/html/postgres.php

In insert mode, describe as follows → save.

python


<?php

$DBHOST = "127.0.0.1";
$DBPORT = "5432";
$DBNAME = "postgres";
$DBUSER = "postgres";
$DBPASS = "postgres";

try{
  //DB connection
  $dbh = new PDO("pgsql:host=$DBHOST;port=$DBPORT;dbname=$DBNAME;user=$DBUSER;password=$DBPASS");
  print("Successful connection".'<br>');
}catch(PDOException $e){
  print("Connection Failed".'<br>');
  print($e.'<br>');
  die();
}
//Close the connection to the database
$dbh = null;
?>

(8) Hit the following URL in your browser, and if "Connection successful" is displayed, it's OK!

http://Public IPv4 DNS/postgres.php

image.png

With this, the environment construction → operation check from the browser has been completed!

At the end

Apache and MySQL are good, but I want to touch on Nginx and PostgreSQL, which I'm not familiar with. I especially stumbled on the link between PostgreSQL and PHP (PDO), but I'm glad I was able to do it.

[Linux] Timezone change | Qiita Is very helpful.

reference

Comparison with LAMP

procedure

1. Software installation

  1. Nginx
  1. PostgreSQL

Recommended Posts

Build an LNPP environment on Amazon Linux 2
Build an Arch Linux environment on Raspberry Pi
Create an environment for MkDocs on Amazon Linux (attempted)
How to build a Python environment on amazon linux 2
Introduce Python 3.5.2 environment on Amazon Linux
Install LAMP on Amazon Linux 2 and build a WordPress environment.
Build an NFS server on Arch Linux
[Linux] Build a Docker environment with Amazon Linux 2
Build a Selenium environment on Amazon Linux 2 in the shortest time
Set up an Objective-C 2.0 development environment on Linux
How to build Java environment on Ubuntu (Linux)
Build an OpenCV4 environment on Raspberry Pi using Poetry
Build an Anaconda virtual environment
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
Build Python environment on Windows
Put jenv on Amazon Linux
Compactly build an Oracle database (19c) on Linux on VirtualBox
Build python environment on windows
Install tomcat 5.5 on Amazon Linux.
Use sshpass on Amazon linux2
Install Homebrew on Amazon Linux 2
Install strongSwan 5.9.1 on Amazon Linux 2
Linux environment construction (on WSL environment)
Build an environment for machine learning using Python on MacOSX
Build an Ubuntu python development environment on Google Cloud Platform
Introducing Kaggle's Docker Image on Windows to build an environment
Build an environment on windows10 where you can try MXNet
Prepare pipenv environment with amazon Linux 2
Install Python Pillow on Amazon Linux
Install oracle java8 on amazon linux2
Try installing OpenAM on Amazon Linux
Install pyenv on EC2 (Amazon Linux)
Create a Linux environment on Windows 10
[Linux] Docker environment construction on Redhat
[Note] Install Imagick on Amazon Linux2
[Note] Run Django on Amazon Linux 2
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Run docker-compose on Amazon Linux2 on ARM64
Build a python3 environment on CentOS7
Run cron on Amazon Linux (set on Linux)
Create an AWS Cloud9 development environment on your Amazon EC2 instance
I'll install Ruby on EC2 (Amazon Linux2) 2020
[UE4] Build DedicatedServer on Windows and Linux
Building a Python3 environment with Amazon Linux2
Build an environment for Blender built-in Python
Python 2.7, 3.4, 3.5 extension module build environment on Windows
I created an SFTP-only user on Linux.
Build Python3 and OpenCV environment on Ubuntu 18.04
Build a python environment on MacOS (Catallina)
Use Numpy, Scipy, scikit-learn on Amazon Linux
Build a simple WebDAV server on Linux
How to update php on Amazon linux 2
Learn sshd_config and authorized_keys (on Amazon Linux 2)
Upgraded mysql on Cloud9 (Amazon Linux) (5.5 to 5,7)
Build a Samba server on Arch Linux
Build Python environment with Anaconda on Mac
How to install Anisble on Amazon Linux 2
Build WordPress on CentOS 8 in LAMP environment
[Linux] Build a jenkins environment with Docker
Run Keycloak on Amazon Linux 2 without Docker
Create an OpenCV3 + python3 environment on OSX