CentOS8 + nginx1.18 + php-fpm7.4 + MariaDB10.5 + CodeIgniter4 development environment preparation memo

Overview

As of September 27, 2020, we have built a development environment that adopts each environment described in [Environmental information (version, etc.)](#Environmental information (version, etc.)), so a memo when preparing the environment. Leave as.

Environmental information (version, etc.)

Memo of various setting file paths

environment File Path
nginx /etc/nginx/conf.d/default.conf
nginx /etc/nginx/nginx.conf
php /etc/php.ini
php-fpm /etc/php-fpm.d/www.conf
MariaDB /etc/my.cnf.d/server.cnf
MariaDB /etc/my.cnf.d/client.conf

Other settings of CentOS

# timedatectl set-timezone Asia/Tokyo
# localectl set-locale LANG=ja_JP.UTF-8
# source /etc/locale.conf

# dnf install -y langpacks-ja
# dnf install -y vim
# dnf install -y elfutils-libelf-devel
# dnf -y update

# vim /etc/selinux/config

SELINUX=disable ← set to disable

Nginx setup

Install Nginx with dnf

# vim /etc/yum.repos.d/nginx.repo

ini:/etc/yum.repos.d/nginx.repo


[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1 
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

On the following site, the installation procedure was described with the following definition, but unlike the above settings, it seemed that a slightly older version was set up. (Above: 1.18 as of 1.14 2020.09.26 below) https://www.nginx.com/resources/wiki/start/topics/tutorials/install/

nginx.repo


[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
# dnf -y install nginx

Confirmation of Nginx setup completion

# nginx -v

nginx version: nginx/1.18.0

# systemctl enable nginx
# systemctl start nginx

If you access the following and the nginx screen (Welcome to nginx!) Is displayed, it's OK. http://192.168.33.10

php-fpm 7.4 setup

Add repository

The standard repository for CentOS8 is the version of php-fpm 7.2.24 as of 09.26.2020. Since 7.4 is the latest, I would like to use 7.4. So, set up an additional repository and set it up from there.

# dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
# dnf module enable php:remi-7.4

Install php-fpm 7.4

Since it is necessary to modify it appropriately according to the development requirements, refer to the following site and install any PHP extension module required. https://www.php.net/manual/ja/extensions.php

# dnf module install php:remi-7.4

Final confirmation of metadata expiration: 0:32:It was held 40 hours ago on September 26, 2020 at 19:39:36.
The dependency has been resolved.
========================================================================================================================================
Package architecture version repository size
========================================================================================================================================
group/Installing module package:
 php-cli                       x86_64              7.4.10-1.el8.remi                                    remi-modular              4.6 M
 php-common                    x86_64              7.4.10-1.el8.remi                                    remi-modular              1.2 M
 php-fpm                       x86_64              7.4.10-1.el8.remi                                    remi-modular              1.6 M
 php-mbstring                  x86_64              7.4.10-1.el8.remi                                    remi-modular              527 k
 php-xml                       x86_64              7.4.10-1.el8.remi                                    remi-modular              214 k
Dependency installation in progress:
 httpd-filesystem              noarch              2.4.37-21.module_el8.2.0+494+1df74eae                AppStream                  36 k
 oniguruma5php                 x86_64              6.9.5+rev1-2.el8.remi                                remi-safe                 206 k
 php-json                      x86_64              7.4.10-1.el8.remi                                    remi-modular               75 k
Installing weak dependencies:
 nginx-filesystem              noarch              1:1.14.1-9.module_el8.0.0+184+e34fea82               AppStream                  24 k
Installing module profile:
 php/common                                                                                                                            
Module stream enabled:
 httpd                                             2.4                                                                                 
 nginx                                             1.14                                                                                
 php                                               remi-7.4                                                                            

Transaction overview
========================================================================================================================================
Installation 9 packages

Confirmation of php-fpm setup completion

# php -v

PHP 7.4.10 (cli) (built: Sep  1 2020 13:58:08) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

# php-fpm -v
PHP 7.4.10 (fpm-fcgi) (built: Sep  1 2020 13:58:08)
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Check the location of the php.ini file just in case

# php -i | grep php.ini
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini

Change php.ini settings

Change the contents of php.ini

/etc/php.ini


;Default character code
default_charset = UTF-8
;X that outputs PHP version information to HTTP response-Powered-Do not include By header
expose_php = Off

;Memory usage limit
memory_limit = 256M
;Maximum size of POST request data
post_max_size = 128M
;Maximum size of uploaded file when uploading file
upload_max_filesize = 100M

;Error log
error_log = /var/log/php_error.log
;Output error
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
;Error display (because it is convenient during development)
display_errors = On
display_startup_errors = On

[Date]
date.timezone = Asia/Tokyo

[mbstring]
;mbstring default language
mbstring.language = Japanese
;Do not automatically convert the encoding of HTTP input characters to the encoding of internal characters
mbstring.encoding_translation = Off
;Priority when automatically detecting character code
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII

Settings to link php-fpm and nginx

# cp -p /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.org
# vim /etc/nginx/conf.d/default.conf

config:/etc/nginx/conf.d/default.conf


server {
  listen 80;
  server_name localhost;

  #Setting the document root (set the same location as the standard document root such as apache)
  root /var/www/html;
  index index.php index.html index.htm;

  location / {
    #Checks the existence of files and dirs in the specified order, and returns the first one found.
    #If none of them exist, the path transitions to the last specified path.
    try_files $uri $uri/ /index.php$is_args$args;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #Nginx on one server+php-When running fpm, Unix sockets run faster.
    # www.The location of sock is/etc/nginx/conf.d/php-fpm.Match with the conf settings.
    fastcgi_pass   unix:/run/php-fpm/www.sock;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
  }

  access_log /var/log/nginx/access.log;
  error_log  /var/log/nginx/error.log;
}

Change the settings so that the vagrant user runs both nginx and php-fpm related.

/etc/nginx/nginx.conf


#Changed the following to vagrant
user  vagrant;

conf:/etc/php-fpm.d/www.conf


#Changed the following to vagrant
user = vagrant
group = vagrant

#Uncomment and change to vagrant
listen.owner = vagrant
listen.group = vagrant
listen.mode = 0660

# listen.owner and listen.Comment out the following to specify the group
;listen.acl_users = apache,nginx

Restart the service for the settings to take effect

# systemctl restart php-fpm
# systemctl restart nginx

Confirmed operation with phpinfo ()

Prepare a sample program that executes phpinfo and check the cooperation of nginx + php-fpm.

/var/www/html/index.php


<?php

phpinfo();

If you access the following URL and the contents of phpinfo () are displayed, the link setting of nginx and php-fpm is completed.

http://192.168.33.10/

MariaDB setup

Add repository

MariaDB installed in the CentOS8 repository is 10.3, and the latest version is 10.5 at the moment, so I would like to install the latest version as well.

# vim /etc/yum.repos.d/mariadb.repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos8-amd64
module_hotfixes=1
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1


# dnf info MariaDB-server MariaDB-devel --disablerepo=* --enablerepo=mariadb

Since we have confirmed that the repository has been added normally above, we will actually install it.

Install MariaDB with dnf

# dnf instal MariaDB-server MariaDB-devel --disablerepo=* --enablerepo=mariadb

error: 
problem: cannot install the best candidate for the job
  - nothing provides libaio.so.1()(64bit) needed by MariaDB-server-10.5.5-1.el8.x86_64
  - nothing provides libaio.so.1(LIBAIO_0.1)(64bit) needed by MariaDB-server-10.5.5-1.el8.x86_64
  - nothing provides libaio.so.1(LIBAIO_0.4)(64bit) needed by MariaDB-server-10.5.5-1.el8.x86_64
  - nothing provides lsof needed by MariaDB-server-10.5.5-1.el8.x86_64
  - nothing provides perl(DBI) needed by MariaDB-server-10.5.5-1.el8.x86_64
(To skip non-installable packages'--skip-broken'Or try adding'--nobest'Do not use only the best candidate packages)

Somehow an error will occur. The cause of the error seems to be that libaio, lsof, perl-DBI are required for MariaDB installation. Therefore, install the corresponding three.

# dnf install libaio
# dnf install lsof
# dnf install perl-DBI

I have installed the three points that were pointed out, so I will try again.

# dnf install MariaDB-server MariaDB-devel --disablerepo=* --enablerepo=mariadb


Final confirmation of metadata expiration: 0:10:It was held 24 hours ago on September 27, 2020 at 07:09:32.
error: 
problem: package MariaDB-server-10.5.5-1.el8.x86_64 requires galera-4, but none of the providers can be installed
  - cannot install the best candidate for the job
  - nothing provides libboost_program_options.so.1.66.0()(64bit) needed by galera-4-26.4.3-1.rhel8.0.el8.x86_64
  - nothing provides libboost_program_options.so.1.66.0()(64bit) needed by galera-4-26.4.4-1.rhel8.0.el8.x86_64
  - nothing provides libboost_program_options.so.1.66.0()(64bit) needed by galera-4-26.4.5-1.el8.x86_64
  - nothing provides socat needed by galera-4-26.4.5-1.el8.x86_64
(To skip non-installable packages'--skip-broken'Or try adding'--nobest'Do not use only the best candidate packages)

It's still useless. I get angry when there is no libboost_program_options, so install boost-program-options.

# dnf install boost-program-options

Challenge to be honest for the third time I was able to start the installation successfully.

# dnf install MariaDB-server MariaDB-devel --disablerepo=* --enablerepo=mariadb

Confirm MariaDB installation

# mysql -V

mariadb  Ver 15.1 Distrib 10.5.5-MariaDB, for Linux (x86_64) using readline 5.1

MariaDB autostart settings

# systemctl start mariadb
# systemctl enable mariadb
# systemctl status mariadb

Initial setting with mysql_secure_installation

mysql_secure_installation


# mysql_secure_installation

#Enter the current password for MariaDB's root account?
Enter current password for root (enter for none): Enter

# unix_Change to authentication using socket?
# unix_socket is when the login user name of CentOS and the user name on MariaDB side are the same name.
# ID/A mechanism to authenticate without using PASS. (MariaDB 10.Was it added from 4? )
Switch to unix_socket authentication [Y/n] : n

#Change MariaDB root account password?
Change the root password? [Y/n] : Y
    New password: Hoge1234
    Re-enter new password: Hoge1234

#Delete anonymous users?
Remove anonymous users? [Y/n] Y

#Prohibit login with remote MariaDB root account?
Disallow root login remotely? [Y/n] Y

#I have a "test" database that anyone can access, but should I delete it?
Remove test database and access to it? [Y/n] Y

#If you reload the permission table, all the changes so far can be reflected immediately, but do you?
Reload privilege tables now? [Y/n] Y

Check MariaDB initial settings

Since the password for the MariaDB root account is set in the above settings, make sure that you can access it properly with that password.

# mysql -u root -p
Enter password: Hoge1234

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.5.5-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)

MariaDB [(none)]> show variables like "chara%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)

OK.

Now that MariaDB has been built, it's time to create the users and databases to use with MariaDB. (Since it is a separate development environment, you can use all root users, but just in case.)

Settings for using Japanese with MariaDB without problems

To handle Japanese, change the character code to ʻutf8mb4`.

conf:/etc/my.cnf.d/server.cnf


[mariadb]
character-set-server = utf8mb4

conf:/etc/my.cnf.d/client.cnf


[client-mariadb]
default-character-set = utf8mb4

Make sure it is set to ʻutf8mb4`.

# systemctl restart mariadb
# mysql -u root -p

MariaDB [(none)]> show variables like "chara%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.002 sec)

Creating databases and users

Creating a database and creating a user

# mysql -u root -p

MariaDB [(none)]> create database yudb;

Creating user "yu" accessible from all hosts
MariaDB [(none)]> create user yu;
MariaDB [(none)]> set password for yu@'%'=password('yupass');

MariaDB [(none)]> select user, host from mysql.user;
+-------------+-----------+
| User        | Host      |
+-------------+-----------+
| yu          | %         |
| mariadb.sys | localhost |
| mysql       | localhost |
| root        | localhost |
+-------------+-----------+
4 rows in set (0.004 sec)

Grant permissions to crud the "yudb" database when accessed by the "yu" user from all hosts
MariaDB [(none)]> grant select,insert,update,delete ON yudb.* TO yu@'%';

Reflect authority
MariaDB [(none)]> flush privileges;

Connection test from PHP program to MariaDB

Change the program of index.php to check the operation of DB access using MySQLi, and check the connection to php-> MariaDB.

/var/www/html/index.php


<?php

$server = 'localhost';
$user = 'yu';
$pass = 'yupass';
$dbname = 'yudb';

$my = new MySQLi($server, $user, $pass, $dbname);
$my->set_charset('utf8mb4');
$sql = 'select now() as n from dual';
$result = $my->query($sql);
//Get the contents of the database
while($row = $result->fetch_assoc() ){
    var_dump($row);
}
//Close DB connection
$my->close();

http://192.168.33.10/

Fatal error: Uncaught Error: Class'MySQLi' not found in /var/www/html/index.php:8 Stack trace: # 0 {main} thrown in /var/www/html/index.php on line 8 And an error will occur.

This is an error that the PHP extension cannot be found because the mysqli extension is not installed. Therefore, install it with the following command.

# dnf -y install php-mysqlnd

http://192.168.33.10/ If you access it again, you can check the connection result to MariaDB normally as shown below.

array(1) { ["n"]=> string(19) "2020-09-27 15:11:27" }

CodeIgniter environment preparation

From composer preparation

We will prepare the development environment for CodeIgniter. I will use composer, so first prepare composer.

# cd /usr/local/src
# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

#The following hash is Latest: v1.10.It's 13 things.
#Https if the target version is different://getcomposer.org/download/Please refer to.
# php -r "if (hash_file('sha384', 'composer-setup.php') === '795f976fe0ebd8b75f26a6dd68f78fd3453ce79f32ecb33e7fd087d39bfeb978342fb73ac986cd4f54edd0dc902601dc') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

# php composer-setup.php
# php -r "unlink('composer-setup.php');"
# mv composer.phar /usr/local/bin/composer
# composer -V
Composer version 1.10.13 2020-09-09 11:46:34

Create a new CodeIgniter4 project with composer

Create a CodeIgniter4 project using composer.

# cd /var/www/html
# composer create-project codeigniter4/appstarter codeigniter4

The following problem seems to have occurred, so delete the codeigniter4 project once created, reinsert the necessary extension modules, and recreate the project.

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - codeigniter4/framework v4.0.4 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - codeigniter4/framework v4.0.3 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - codeigniter4/framework v4.0.2 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - codeigniter4/framework v4.0.1 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - codeigniter4/framework 4.0.0 requires ext-intl * -> the requested PHP extension intl is missing from your system.
    - Installation request for codeigniter4/framework ^4 -> satisfiable by codeigniter4/framework[4.0.0, v4.0.1, v4.0.2, v4.0.3, v4.0.4].
# dnf install php-intl

When I run composer create-project again, I get a warning, but the Problem is gone, so I'm done.

http://192.168.33.10/codeigniter4/public/index.php

スクリーンショット 2020-09-27 15.52.39.png

Change nginx document root settings to match codeigniter4 project

Under {project-name} / public / seems to be the document root of codeigniter4 installed by composer. So, change the document root of nginx.

conf:/etc/nginx/conf.d/default.conf


root /var/www/html/codeigniter4/public;

If you restart nginx after the above changes, you can access the codeigniter4 project at the following URL. http://192.168.33.10

CodeIgniter Preferences

There is an environment variable setting file called ʻenv, but it cannot be used as it is. Copy it to a file named .env` and start using it.

# cd /var/www/html/codeigniter4
# cp -p env .env

/var/www/html/codeigniter4/.env


#Settings for checking error information during development
CI_ENVIRONMENT = development

# config.php base_The one that was set in the url
app.baseURL = 'http://192.168.33.10/'

#Connection information to the MySQL database
database.default.hostname = localhost
database.default.database = yudb
database.default.username = yu
database.default.password = yupass
database.default.DBDriver = MySQLi
database.default.charset = utf8mb4

Recommended Posts

CentOS8 + nginx1.18 + php-fpm7.4 + MariaDB10.5 + CodeIgniter4 development environment preparation memo
Java development environment memo
[Eclipse Java] Development environment setting memo
Build a development environment on AWS EC2 with CentOS7 + Nginx + pm2 + Nuxt.js
CentOS8 + Anaconda + Django development environment construction procedure
[Personal memo] Java development environment is ready
CentOS8.2 (x86_64) + ruby2.5 + Rails5.2 + MariaDB (10.3.17) environment construction
Command to install nginx / PHP7 / php-fpm on CentOS7
[Memo] Create a CentOS 8 environment easily with Docker