<コピペ用>Nginxの初期設定
Nginxインストール
FastCGIキャッシュを使用しない最低限の設定です。
#リポジトリ用の設定ファイルを作成
touch /etc/yum.repos.d/nginx.repo
#設定を記述
echo -e "[nginx]\nname=nginx repo\nbaseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/\ngpgcheck=0\nenabled=0" >> /etc/yum.repos.d/nginx.repo
#参考:記述内容
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=0
#Nginxインストール
yum --enablerepo=nginx -y install nginx
Nginxの初期設定
いろいろ設定方法がありますが、
とりあえず、最低のこの二つでOK。
基本設定ファイル
「/etc/nginx/nginx.conf」の変更です。
#とりあえずバックアップ
cp /etc/nginx/nginx.conf /etc/nginx/nginx.bak
#設定変更
sed -i -e "s/^worker_processes.*$/worker_processes auto;/g" -e "s/keepalive_timeout.*$/keepalive_timeout 10;\nserver_tokens off;\nserver_names_hash_bucket_size 128;/g" /etc/nginx/nginx.conf
#変更前
worker_processes 1;
keepalive_timeout 65;
#変更後
worker_processes auto;
keepalive_timeout 10;
server_tokens off; #追加
server_names_hash_bucket_size 128; #追加
「worker_processes」はCPUのコア数を指定します。
さくらVPS2Gなら「3」を指定しても良い。
デフォルトのサイト設定ファイル
「/etc/nginx/conf.d/default.conf」の変更です。
ここはほぼphpMyAdmin用です。
#とりあえずバックアップ
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.bak
#設定変更一括コピペ
sed -i -e "/pass the PHP scripts/,/#}/ s/#/ /g" -e "/location ~ \\\.php\$ {/,/}/ s/html;/\/var\/www\/html;/g" -e "/location ~ \\\.php\$ {/,/}/ s/127\.0\.0\.1:9000;/unix:\/var\/run\/php-fpm\/php-fpm\.sock;/g" -e "/location ~ \\\.php\$ {/,/}/ s/\/scripts\$fastcgi_script_name;/\$document_root\$fastcgi_script_name;/g" -e "/location \/ {/,/}/ s/\/usr\/share\/nginx\/html;/\/var\/www\/html;/g" -e "/location \/ {/,/}/ s/index.html/index.php index.html/g" -e "s/pass the PHP scripts/#pass the PHP scripts/g" /etc/nginx/conf.d/default.conf
#変更前
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
#変更後
location / {
root /var/www/html
index index.php index.html index.htm;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#Nginxを起動
systemctl start nginx.service
#Nginxの自動起動ON
systemctl enable nginx.service
#ファイアーウォールでhttpを許可
firewall-cmd --add-service=http --zone=public --permanent
# firewalldのリロード
firewall-cmd --reload