It is a procedure to build an LNPP environment other than a LAMP environment on Amazon Linux2.
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.
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) ...
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
(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
With this, the environment construction → operation check from the browser has been completed!
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.
Ident, MD5, Trust comparison to prevent PostgreSQL authentication failure | etuts +
[centos7] Reflect the settings of php.ini with nginx | Yuki Cat House
Recommended Posts