さくらのVPS + CentOS7 + WEBサーバーをApacheからNginxに変更

そんなにサイトへのアクセス数があるわけでもないのですが、WEBサーバーを今流行りのNginxに変えてみました。

移行前の環境は、「ネコでもわかる!さくらのVPS講座」を一からやってサーバーをたてたので、Apacheが立ち上がっています。

php-fpmのインストール

まず、php-fpmが入っていなかったのでインストールしました。

$ yum install --enablerepo=remi,remi-php72 php-fpm

php-fpmの設定を変更

/etc/php-fpm.d/www.conf内のuserとgroupをnginxに変更します。

# vi /etc/php-fpm.d/www.conf

user = nginx
group = nginx

設定したらphp-fpmを起動します。

# systemctl start php-fpm

nginxをインストール

まずは、リポジトリを作ります。

# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1

yumでインストールします。

# yum -y --enablerepo=nginx install nginx

nginxを起動します。自動起動の設定もしておきます。

# systemctl start nginx.service
# systemctl enable nginx.service

nginxの設定

設定ファイルは、/etc/nginx/nginx.confですが、/etc/nginx/conf.d/*.confが自動で読み込まれるので、conf.dにファイルを作ってそちらに個別に設定します。

$ vi /etc/nginx/conf.d/helloworld.conf
 server { 
     listen 80; 
     serer_name hogehoge.net; 
     return 301 https://$host$request_uri; 
 } 
 server { 
     listen 443; 
     server_name hogehoge.net; 
     root /usr/share/nginx/html; 
     index index.php index.html;
    
     ssl on; 
     ssl_certificate /etc/letsencrypt/live/hogehoge.com/fullchain.pem; 
     ssl_certificate_key /etc/letsencrypt/live/hogehoge.com/privkey.pem; 
     ssl_session_timeout 1d; 
     ssl_session_cache shared:SSL:50m; 
     ssl_session_tickets on; 
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
     ssl_prefer_server_ciphers on; 

     location / { 
         try_files $uri /index.php?query_string;  
     } 
     
     location ~ .php$ { 
         fastcgi_pass   127.0.0.1:9000; 
         fastcgi_index  index.php; 
         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
         include        fastcgi_params; 
     } 
 } 

sslはApacheで作ったやつがそのまま使えました。記載したらnginxを再起動します。

# systemctl restart nginx.service

これでアクセスできるはずです。

[おまけ] WordPressでダッシュボードにアクセスできない

WordPressでダッシュボードにアクセスしようとしたら出来ないという謎な現象に困っていたのですが、下記を削除したらうまくアクセス出来ました。

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

なんでだろ?

ま、とにかく無事変更できたので、今のところ良かったかなと思います。

[追記]

↑参考にしたらできました。

参考

2 COMMENTS

現在コメントは受け付けておりません。