目的
Ubuntu 20.04でnginx + php-fpmを設定後Raspi3B + Ubuntu 20.04 に MySQL をインストールしてみるでインストールしたMySQLにアクセスしてみる
※do-release-upgrade で Ubuntu 22.04に更新後に変更した部分を追記する
MySQLのバージョンは以下になる
# sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 8.0.30-0ubuntu0.22.04.1 (Ubuntu)
必要なパッケージのインストール
$ sudo apt install nginx
$ sudo apt install php-fpm
$ sudo apt install php7.4-mbstring
$ sudo apt install php7.4-mysql
$ sudo apt install php7.4-odbc ; 今回は使用しない
phpinfoの[Additional .ini files parsed]を確認すると追加したパッケージに対応したiniファイルが表示される
設定ファイルの編集
/etc/nginx/nginx.conf
変更なし
/etc/nginx/sites-enabled/default
以下をコメントアウトする
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
#fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; #20.04
fastcgi_pass unix:/run/php/php8.1-fpm.sock; #22.04
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /$document_root$fastcgi_script_name;
include fastcgi_params;
}
/etc/nginx/snippets/fastcgi-php.conf
以下を追加する
fastcgi_index index.php;
include fastcgi.conf;
/etc/php/7.4/fpm/php.ini (/etc/php/8.1/fpm/php.ini)
変更しない
$ sudo systemctl start nginx
$ sudo systemctl stop nginx
$ sudo systemctl restart nginx
$ sudo systemctl status nginx
$ sudo tail -f /var/log/nginx/error.log
MySQLのデモDB world をインストールする
world databaseよりworld-db.zipをDL後解凍する
Setting Up the world Database / Installationに従い
$ sudo mysql -u root -p
Enter password:
~
mysql> source /path/to/world.sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| world |
+--------------------+
mysql> use world
Database changed
mysql> create user 'demo'@'localhost' identified by 'demo';
mysql> grant select on world.* to demo@'localhost';
mysql> select user, host, plugin from mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| demo | 192.168.% | caching_sha2_password |
| demo | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | auth_socket |
+------------------+-----------+-----------------------+
mysql> show grants for demo@'localhost';
+-------------------------------------------------+
| Grants for demo@localhost |
+-------------------------------------------------+
| GRANT USAGE ON *.* TO `demo`@`localhost` |
| GRANT SELECT ON `world`.* TO `demo`@`localhost` |
+-------------------------------------------------+
php-fpmのサンプルコード
上記でインストールしたworldにアクセスしてみる
<?php
$dsn = 'mysql:host=localhost;dbname=world';
$user = 'demo';
$passwd = 'demo';
try{
$dbh = new PDO($dsn, $user, $passwd);
$stmt = $dbh->query("select count(*) from city");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<pre>";
print_r( $users );
echo "</pre>";
}catch (PDOException $e){
print('Error:'.$e->getMessage());
die();
}
?>
参考にしたのは以下のサイト
Ubuntu 20.04にNginxをインストールする方法
nginx fastcgi_params を include する箇所、割と皆間違ってるよね?
nginx と PHP-FPM の仕組みをちゃんと理解しながら PHP の実行環境を構築する
nginx で PHP を動かす
Wordpress(PHP-FPM)と Nginx との間の FastCGI の設定を見直した話
Raspi3B + Ubuntu 20.04 から Windows 10 + SQL Server 2019 Express にアクセスしてみる