nginx とは web サーバーの一種です。
chatGPT によると「Webサーバーとは、ネットワーク上でHTTP通信を待ち受け、URLで指定されたコンテンツ(HTML、画像、プログラムの実行結果など)を返す常駐プログラムである。」
apache も web サーバーですが、その 2 つを比較すると、
| 項目 | nginx | Apache |
|---|---|---|
| 分類 | Webサーバー | Webサーバー |
| 主な用途 | 静的配信・リバースプロキシ・負荷分散 | 動的配信・汎用Webサーバー |
| 開発元 | Igor Sysoev(個人) | Apache Software Foundation |
| 初公開 | 2004年 | 1995年 |
| 設計思想 | イベント駆動・非同期 | プロセス/スレッドベース |
| 同時接続数 | 非常に強い | 多くなると弱くなりがち |
| メモリ使用量 | 少ない | 比較的多い |
| 静的ファイル配信 | 非常に高速 | 普通 |
| 動的コンテンツ | 外部(PHP-FPM等) | モジュールで直接処理可 |
| PHPとの関係 | PHP-FPM必須 | mod_php が使える |
| 設定方法 | 設定ファイル中心 | 設定+.htaccess |
| .htaccess | 使用不可 | 使用可能 |
| ディレクトリ単位設定 | 不可 | 可能 |
| リバースプロキシ | 得意 | 可能だが主用途ではない |
| SSL/TLS | 標準で高速 | 標準対応 |
| 主な用途例 | 本番環境・高負荷サイト | 開発環境・共有サーバー |
| 学習コスト | やや高い | 低め |
apache と比較して軽くて動作は遥かに高速です。
以前から私の祖父から孫までの 5 代にわたるアルバムを自作の php プログラムで作っていましたが、しょせん素人が作るものであまりセンスがあるとは言えないものでした。
また、いずれレンタルサーバーにデプロイすることを考えており、laravel で作って強烈なアクセス制限をかけた方が良いのではないかと考えるようになりました。
現在ローカルで laravel でアルバムアプリを作成したのですが、動画が重すぎてロード時間がかなりかかってほとんどフリーズの状態になってしまいます。
そこで、apache から nginx に変更したところ爆速になりました。xserver なんかも nginx を使ってるようなので、apache から nginx に切り替えようと思います。
nginx 設定はかなりわかりにくいのでマニュアル化しました。
Apache と nginx はポート 80 を使うので、Apache を停止・無効化します。
sudo systemctl stop apache2sudo systemctl disable apache2sudo apt updatesudo apt install nginx -yバージョン確認
nginx -v
nginx version: nginx/1.24.0 (Ubuntu)sudo systemctl start nginxsudo systemctl enable nginx確認します。
sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled) Active: active (running) since Thu 2026-01-15 07:34:59 JST; 35min ago Docs: man:nginx(8) Process: 7428 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited,> Process: 7430 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0> Main PID: 7431 (nginx) Tasks: 13 (limit: 37954) Memory: 9.0M (peak: 10.6M) CPU: 22msmkdir -p /home/moheno/wwwchmod 755 /home/mohenosudo nano /etc/nginx/sites-available/minimal以下のように編集。
server { listen 80; server_name localhost;
root /home/moheno/www; index index.html;}cat << 'EOF' > /home/moheno/www/index.html<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>nginx test</title></head><body> <h1>nginx is working</h1> <p>Document root: /home/moheno/www</p></body></html>EOFsudo rm -f /etc/nginx/sites-enabled/defaultsudo ln -s /etc/nginx/sites-available/minimal /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl restart nginx
nginx を停止するには、
sudo systemctl stop nginx起動しないようにしておくには、
sudo systemctl disable nginxnginx を完全に削除するには以下を実行します。
sudo systemctl stop nginxsudo systemctl disable nginxsudo apt purge nginx nginx-common nginx-coresudo apt autoremovesudo systemctl start apache2自動起動を有効化するには、
sudo systemctl enable apache2family-albumというプロジェクトを作成した場合は、その public となります。
family-album/├── app/├── bootstrap/├── config/├── public/ ← Webサーバーが見るのはここだけ│ └── index.php ← ここから Laravel が起動する├── routes/├── storage/└── vendor//etc/nginx/sites-available/family-album を設定します。
sudo nano /etc/nginx/sites-available/family-album以下のように編集します。
server { listen 80; server_name localhost;
root /home/moheno/family-album/public; index index.php;
location / { try_files $uri $uri/ /index.php?$query_string; }
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.4-fpm.sock; }
location ~ /\.(?!well-known).* { deny all; }}fastcgi_pass unix:/var/run/php/php8.4-fpm.sock; の部分は php のバージョンに合わせます。
有効化します。
sudo ln -s /etc/nginx/sites-available/family-album /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl reload nginxパーミッション設定。
sudo chmod 755 /home/mohenosudo chown -R moheno:www-data /home/moheno/family-albumsudo chmod -R 755 /home/moheno/family-albumsudo chmod -R 775 /home/moheno/family-album/storage /home/moheno/family-album/bootstrap/cachephp fpm を起動。
sudo systemctl start php8.4-fpmsudo systemctl enable php8.4-fpm