トップ
サイト作成料金
お問い合わせ
自己紹介
ブログ
GAC blog.
カテゴリ
gacreate.com
Blog.
【備忘録】ConoHa VPSのサーバー構築 Part.2(LAMP編)
0
Linux
2023年06月01日06:09
前の記事
【備忘録】ConoHa VPSのサーバー構築 Part.1(初期設定編)


この記事では下記のソフトをインストールして設定する
httpd(apache)
php + ImageMagick
mariadb




remiリポジトリをインストール

※新しいphpのバージョンを使いたいのでremiを入れる
remiをインストール
# yum -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm

念のためremi-safeが有効かだけ確認します
[remi-safe]の部分
# vi /etc/yum.repos.d/remi-safe.repo
enabled=1

リストを表示確認
# yum list
※何度かGPG鍵の確認があるので y で進めremiリポジトリのリストも表示されたらOK



httpd、php、mariadbのインストール

httpd
# yum -y install httpd httpd-devel

php8.2 + php-fpm + ImageMagick
ApacheモジュールではなくFastCGIで実行させる
# yum -y install php82 php82-php-gd php82-php-mysqlnd php82-php-fpm php82-php-pecl-imagick

mariadb
# yum -y install mariadb mariadb-server



自動起動設定

httpdの自動起動
# systemctl enable httpd

php-fpmの自動起動
# systemctl enable php82-php-fpm

MariaDBの自動起動
# systemctl enable mariadb

サーバーを再起動させます(僕のルーティーン)
# shutdown -r now

起動確認
🟢でactive(running)ならOK
# systemctl status httpd
# systemctl status php81-php-fpm
# systemctl status mariadb



phpの初期設定

phpコマンドが使えないので使えるように
# php
-bash: php: command not found

# alternatives --install /usr/bin/php php /usr/bin/php82 1
# php -v
PHP 8.2.6 (cli) (built: May  9 2023 06:25:31) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.6, Copyright (c), by Zend Technologies

設定ファイルが/etcじゃなく/etc/opt/remi/php82にあるのでシンボリックリンクを張る
# ln -s /etc/opt/remi/php82/php-fpm.conf /etc/php-fpm.conf
# ln -s /etc/opt/remi/php82/php-fpm.d /etc/php-fpm.d
# ln -s /etc/opt/remi/php82/php.ini /etc/php.ini
# ls -l /etc/php*
lrwxrwxrwx. 1 root root 32 Jun  1 09:27 /etc/php-fpm.conf -> /etc/opt/remi/php82/php-fpm.conf
lrwxrwxrwx. 1 root root 29 Jun  1 09:27 /etc/php-fpm.d -> /etc/opt/remi/php82/php-fpm.d
lrwxrwxrwx. 1 root root 27 Jun  1 09:27 /etc/php.ini -> /etc/opt/remi/php82/php.ini

ログファイルも/var/logに置シンボリックリンク
# ln -s /var/opt/remi/php82/log/php-fpm /var/log/php-fpm
# ls -l /var/log
lrwxrwxrwx. 1 root root 31 Jun  1 16:07 php-fpm -> /var/opt/remi/php82/log/php-fpm



ページを表示させる firewallの設定

ブラウザでサーバーのIPアドレスにアクセスします
http://xxx.xxx.xxx.xxx
アクセス出来ないので設定をします
httpを追加して設定を反映させます
# firewall-cmd --zone=public --add-service=http --permanent
# firewall-cmd --reload

ブラウザで再度サーバーのIPアドレスにアクセスしてWelcomeページが表示されたらOK




phpの動作確認

index.phpの作成
# vi /var/www/html/index.php
<?php
phpinfo();

ブラウザで再度サーバーのIPアドレスにアクセスしてphpのページが表示されたらOK
Apache + phpの連携は問題なし!




httpd.confの設定

# cd /etc/httpd/conf

変更箇所だけ記述 基本的にデフォルトのディレクトリ設定は使わない
# vi httpd.conf

コメントアウト(または消す)
#<Directory />
#    AllowOverride none
#    Require all denied
#</Directory>

コメントアウト(または消す)
#DocumentRoot "/var/www/html"

コメントアウト(または消す)
#<Directory "/var/www">
#    AllowOverride None
#    # Allow open access:
#    Require all granted
#</Directory>

コメントアウト(または消す)
#<Directory "/var/www/html">
#	Options Indexes FollowSymLinks
#	AllowOverride None
#	Require all granted
#</Directory>

コメントアウト(または消す)
#ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

コメントアウト(または消す)
#<Directory "/var/www/cgi-bin">
#    AllowOverride None
#    Options None
#    Require all granted
#</Directory>

最後に追加
IncludeOptional conf.d/virtualhost/*.conf
ServerTokens ProductOnly



virtualhostの設定

# cd /etc/httpd/conf.d
# mkdir virtualhost
# cd virtualhost
# vi gacreate.com.conf
<Directory /www/gacreate.com/>
    Options +ExecCGI
    Options FollowSymLinks
    AllowOverride All
    DirectoryIndex index.php
    <FilesMatch \.(php)$>
        SetHandler "proxy:unix:/var/opt/remi/php82/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
</Directory>

<VirtualHost *:80>
    ServerName gacreate.com
    DocumentRoot /www/gacreate.com/www
    ServerAdmin gacreate.com
</VirtualHost>

ディレクトリを作成
僕の場合は/wwwに複数のサイトを設置する構成
# mkdir -p /www/gacreate.com/www
ディレクトリをmiyakinの権限に変える
# chown -R miyakin:miyakin /www

index.phpを作成
# vi /www/gacreate.com/www/index.php
<?php
print("test");

httpdのconfファイルの構文チェック
# apachectl configtest
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ~~~~~~~~. Set the 'ServerName' directive globally to suppress this message
Syntax OK

httpd再起動
# systemctl restart httpd



php.confの設定

# vi /etc/httpd/conf.d/php82-php.conf
コメントアウト
#<IfModule !mod_php5.c>
#  <IfModule !mod_php7.c>
#    <IfModule !mod_php.c>
#      # Enable http authorization headers
#      SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
#
#      <FilesMatch \.(php|phar)$>
#          SetHandler "proxy:unix:/var/opt/remi/php82/run/php-fpm/www.sock|fcgi://localhost"
#      </FilesMatch>
#    </IfModule>
#  </IfModule>
#</IfModule>

httpdを再読み込み
# systemctl reload httpd



php-fpmの設定

# vi /etc/php-fpm.d/www.conf
[www]
user = apache
group = apache
listen = /var/opt/remi/php82/run/php-fpm/www.sock
listen.acl_users = apache
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

slowlog = /var/opt/remi/php82/log/php-fpm/www-slow.log

security.limit_extensions = .php

php_admin_value[error_log] = /var/opt/remi/php82/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/opt/remi/php82/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/opt/remi/php82/lib/php/wsdlcache

php-fpmを再起動
# systemctl restart php82-php-fpm



SELinuxコンテキスト

/wwwにhttpdで実行可能なラベル付けをする
# semanage fcontext -a -t httpd_sys_content_t "/www(/.*)?"

設定反映
# restorecon -Rv /www
Relabeled /www from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

確認
# semanage fcontext -l | grep /www
/www(/.*)?                                         all files          system_u:object_r:httpd_sys_content_t:s0

# ls -Z /
unconfined_u:object_r:httpd_sys_content_t:s0 www



php.iniの設定

デフォルト値から変更した項目を箇条書き
max_execution_time = 300
error_reporting = E_ALL
post_max_size = 1024M
upload_max_filesize = 1024M
date.timezone = "Asia/Tokyo"
mysqli.default_socket = /run/mariadb/mysqld.sock
session.name = GASESSID
session.auto_start = 1
mbstring.language = Japanese

httpdを再読み込み
# systemctl reload httpd

ブラウザでサーバーのIPアドレスにアクセスしてページが表示されたらOK



mariadbの設定

だいぶ昔から書き足しなので、そろそろしっかりチューニングをしたい。。
# vi /etc/my.cnf
[client]
user            = mysql
#password       = your_password
port            = 3306
socket          = /run/mariadb/mysqld.sock
default-character-set = utf8mb4

# Here follows entries for some specific programs

# The MariaDB server
[mysqld]
datadir=/var/lib/mysql
port            = 3306
socket          = /run/mariadb/mysqld.sock
character-set-server = utf8mb4
skip-character-set-client-handshake
max_connections = 512
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=mysql-bin
binlog_format=mixed
server-id       = 1
query_cache_size = 4M
query_cache_limit = 1M

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
default-character-set = utf8mb4

mariadbを再起動
# systemctl restart mariadb



データベースのユーザー設定

mysqlにログイン
# mysql -u

ユーザーを追加
MariaDB> GRANT ALL PRIVILEGES ON *.* TO username@localhost IDENTIFIED BY 'password' WITH GRANT OPTION;
rootのパスワードを設定
MariaDB> SET PASSWORD FOR root@localhost=PASSWORD('password');



phpから接続確認

<?php
$cid = mysqli_connect("localhost", "username", "password");
mysqli_select_db($cid, "mysql");
$query_id = mysqli_query($cid, "SELECT User FROM user");
for($i = 0; $i < mysqli_num_rows($query_id); $i++) {
        $data[$i] = mysqli_fetch_array($query_id, MYSQLI_ASSOC);
}
print_r($data);


配列が表示出来たらLAMP環境の完成!


次は
【備忘録】ConoHa VPSのサーバー構築 Part.3(ただのメモ編)

記事の内容で気づきや発見、助けられた場合にPayPalで送金(投げ銭)する事もできます。

頂いたお金はサイト運用費に当てさせて頂きます。

コメントを残して頂くだけでもモチベーションアップに繋がります(*^^*)

* 誤字脱字、内容の間違いのご指摘もお願いします
* ネガティブコメントは非表示にする場合がございます
* 独自のスパムチェックを導入してます