0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

apacheからnginxに移行

Posted at

✅ 1. 準備段階
▸ Nginxのインストール
サーバーにNginxをインストールします。

bash
コピーする
編集する
sudo apt install nginx # Ubuntu/Debian系

または

sudo yum install nginx # CentOS系
▸ Apache設定のバックアップ
Apacheの設定ファイル(/etc/apache2/ や /etc/httpd/)や .htaccess をバックアップします。

✅ 2. 設定の移行
▸ 仮想ホスト(VirtualHost)の移行
Apacheの設定例:

apache
コピーする
編集する

ServerName example.com
DocumentRoot /var/www/html

Nginxの対応例:

nginx
コピーする
編集する
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html index.htm index.php;
}
▸ .htaccess の変換
Nginxは .htaccess をサポートしていないため、書かれていたリダイレクトやリライトルールをNginxの設定に手動で移行する必要があります。

Apache例:

apache
コピーする
編集する
RewriteEngine On
RewriteRule ^about$ about.html [L]
Nginx例:

nginx
コピーする
編集する
location /about {
rewrite ^/about$ /about.html break;
}
▸ PHPの対応(php-fpmを使用)
Apacheのmod_phpとは違い、Nginxではphp-fpmが必要です。

bash
コピーする
編集する
sudo apt install php php-fpm
Nginx設定例:

nginx
コピーする
編集する
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
※PHPのバージョンによりソケットのパスが異なる場合があります。

▸ SSL(HTTPS)設定
ApacheのSSL設定をNginx形式に変換します:

nginx
コピーする
編集する
server {
listen 443 ssl;
server_name example.com;

ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;

...

}
✅ 3. サービスの切り替え
▸ Apacheを停止・無効化:
bash
コピーする
編集する
sudo systemctl stop apache2
sudo systemctl disable apache2
▸ Nginxを起動・有効化:
bash
コピーする
編集する
sudo systemctl start nginx
sudo systemctl enable nginx
▸ 設定テスト:
bash
コピーする
編集する
sudo nginx -t
✅ 4. その他の注意点
.htaccess を使っていた場合、その機能はすべてNginx設定に書き換える必要があります。

WordPressやLaravelなどのフレームワークでは、専用のNginx設定が必要になる場合があります。

ファイアウォールのポート(80, 443)が開いているか確認してください。

✅ Nginx設定の例(SSL + PHP対応):
nginx
コピーする
編集する
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl;
server_name example.com www.example.com;

root /var/www/html;
index index.php index.html;

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?