インストール - NginX(SUSE)
NginXのインストール方法
パッケージ管理システムからインストール
まず、システムの更新を行う。
sudo zypper update
次に、NginXをインストールする。
NginXは、CentOSやSUSEの公式リポジトリからインストールできる。
ただし、常に最新バージョンが提供されるとは限らない。
sudo zypper install nginx
NginXサービスを自動起動する。
sudo systemctl enable nginx
NginXを開始する。
sudo systemctl start nginx
NginXのステータスを確認する場合は、次のコマンドを実行する。
sudo sytemctl status nginx
ソースコードからインストール
NginXのビルドに必要なライブラリをインストールする。
sudo zypper install libxslt-devel pcre2-devel gd-devel
OpenSSLの公式Webサイトにアクセスして、OpenSSL(1.X.Y)のソースコードをダウンロードする。
ダウンロードしたファイルを解凍する。
tar xf openssl-<バージョン>
NginXの公式Webサイトにアクセスして、NginXのソースコードをダウンロードする。
ダウンロードしたファイルを解凍する。
tar xf nginx-<バージョン>.tar.xz cd nginx-<バージョン>
NginXをビルドおよびインストールする。
NginXのビルドにおいて、ビルドディレクトリを作成すると失敗することに注意する。
./configure --prefix=/<NginXのインストールディレクトリ>/Nginx \ --sbin-path=/<NginXのインストールディレクトリ>/nginx \ --conf-path=/<NginXのインストールディレクトリ>/nginx.conf \ --pid-path=/<NginXのインストールディレクトリ>/nginx.pid \ --with-threads --with-file-aio --with-pcre --with-openssl=<上記でダウンロードおよび解凍したOpenSSLのソースコードのトップディレクトリ> \ --with-http_v2_module --with-http_ssl_module --with-http_addition_module \ --with-http_xslt_module --with-http_xslt_module=dynamic \ --with-http_image_filter_module --with-http_image_filter_module=dynamic --with-http_dav_module --with-http_mp4_module --with-http_gunzip_module \ --with-http_auth_request_module --with-http_secure_link_module --with-http_stub_status_module --with-http_sub_module \ --with-http_perl_module --with-http_perl_module=dynamic --with-perl_modules_path=/<NginXのインストールディレクトリ>/Perl \ --with-mail --with-mail=dynamic --with-mail_ssl_module \ --with-cc=/usr/bin/gcc --with-cpp=/usr/bin/g++
NginXを開始する場合、以下のコマンドを実行する。
cd <NginXのインストールディレクトリ> sudo ./nginx
NginXを停止する場合、以下のコマンドを実行する。
# NginXのプロセスIDを確認する ps aux | grep -i nginx # NginXを停止する sudo kill <プロセスID 1> <プロセスID 2>
または、/etc/systemd/systemディレクトリに、NginXのSystemdサービスユニットを作成する。
sudo vi /etc/systemd/system/nginx.service
# /etc/systemd/system/nginx.serviceファイル [Unit] Description=The nginx HTTP and reverse proxy server After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] PIDFile=/<NginXのインストールディレクトリ>/nginx.pid ExecStartPre=/<NginXのインストールディレクトリ>/nginx -t ExecStart=/<NginXのインストールディレクトリ>/nginx -g "daemon off;" ExecReload=/bin/kill -s HUP $MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=mixed PrivateTmp=true # added automatically, for details please see # https://en.opensuse.org/openSUSE:Security_Features#Systemd_hardening_effort ProtectSystem=full #ProtectHome=read-only PrivateDevices=true ProtectHostname=true ProtectClock=true ProtectKernelTunables=true ProtectKernelModules=true ProtectKernelLogs=true ProtectControlGroups=true RestrictRealtime=true # end of automatic additions [Install] WantedBy=multi-user.target
ファイアウォールの設定
次に、ファイアウォール設定にいくつかの変更を加えて、httpサービスを許可する。
sudo firewall-cmd --add-port=80/tcp --permanent sudo firewall-cmd --reload
Nginxのテスト
Nginxが正しく実行されていることを確認する。
これを行うには、htmlファイルを作成し、それをNginxのルートディレクトリに配置する必要がある。
テキストエディタで以下のファイルを作成する。
sudo nano /srv/www/htdocs/index.html <html> <body> <h1>Welcome to osradar</h1> </body> </html>
最後に、Webブラウザを開き、http://localhost/ にアクセスして、これが表示されるか確認する。
NginX上でPHP7.4(PHP-FPM)の有効化
PHPをNginX上で動作するようにするには、PHP-FPMにいくつかの追加設定を行う必要がある。
まず、PHP-FPMの構成ディレクトリ(/etc/php7/fpm)にあるphp-fpm.confファイルのerror_logキーを以下のように編集する。
sudo cp /etc/php7/fpm/php-fpm.conf.default /etc/php7/fpm/php-fpm.conf
sudo vi /etc/php7/fpm/php-fpm.conf # /etc/php7/fpm/php-fpm.confファイル error_log = /var/log/php-fpm.log
次に、www.conf構成ファイルで構成済みプールの正しい設定を定義する。
sudo cp /etc/php7/fpm/php-fpm.d/www.conf.default /etc/php7/fpm/php-fpm.d/www.conf
sudo vi /etc/php7/fpm/php-fpm.d/www.conf # /etc/php7/fpm/php-fpm.d/www.confファイル user = nginx group = nginx
次に、/etc/php7/cli/php.iniファイルを作成・編集する。
sudo vi /etc/php7/cli/php.ini # /etc/php7/cli/php.iniファイル cgi.fix_pathinfo=0
----- ここは不要かも? -----
次に、/etc/php7/fpm/php.iniファイルを作成・編集する。
php.iniファイルをコピーして、php.iniファイルのcgi.fixキーを編集する。
sudo cp /etc/php7/cli/php.ini /etc/php7/fpm/
sudo vi /etc/php7/fpm/php.ini # /etc/php7/fpm/php.iniファイル cgi.fix_pathinfo=0
そして、/etc/php7/fpm/php.iniファイルをPHP構成ディレクトリにコピーする。
sudo cp /etc/php7/fpm/php.ini /etc/php7/conf.d/
----- ここまで -----
次に、NginXを構成するため、/etc/nginx/nginx.confファイルを編集する。
/etc/nginx/nginx.confファイルの48行目に以下の内容を追記する。
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org
sudo vi /etc/nginx/nginx.conf # /etc/nginx/nginx.confファイル location / { root /srv/www/htdocs/; index index.php index.html index.htm ; } # このセクションは、127.0.0.1:9000でlistenしているFastCGIサーバにPHPスクリプトを渡すために使用される location ~ \.php$ { root /srv/www/htdocs/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # proxy_pass http://127.0.0.1; }
その後、php-fpmサービスを有効にして開始する。
sudo systemctl enable php-fpm sudo systemctl restart php-fpm sudo systemctl restart nginx
最後に、Webブラウザを起動して、http://localhost/index.php にアクセスして、正常に表示されるか確認する。
Xdebugの設定
PHP-FPMの初期設定では、9000番ポートを使用しているので、Xdebugのデバッグポートと競合が発生する。
Xdebugのデバッグポートを以下のように変更する。
sudo vi /etc/php7/conf.d/xdebug.ini # /etc/php7/conf.d/xdebug.iniファイル xdebug.remote_port=9009
PHP-FPMとNginXを再起動する。
sudo systemctl restart php-fpm sudo systemctl restart nginx