Nginx のインストール

リポジトリの追加

nginx.repo を追加し、最新版を取って来れられるようにします。

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1

インストール

# yum -y install nginx

起動チェック

以下を実行し、ブラウザでアクセスできるかを確認。

# systemctl start nginx

自動起動サービスの登録

自動起動に登録されているかチェック。

# systemctl list-unit-files | grep nginx

nginx.service		disabled

自動起動に登録。

# systemctl enable nginx.service

Nginx で php を使う

Nginx で php を使う場合、php-fpm をインストールします。

# yum -y install epel-release
# yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# yum -y install --enablerepo=remi,remi-php72 php php-opcache php-fpm

php-fpm の起動と、php-fpm を自動起動に設定します。

# systemctl start php-fpm
# systemctl enable php-fpm.service

nginx の設定

設定ファイルのバックアップ

# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org

#******************************************************************************
#	nginx 共通設定
#******************************************************************************
user	nginx;
pid		/var/run/nginx.pid;

# ワーカープロセス数
worker_processes	auto;
# エラーログの出力先
error_log			/var/log/nginx/error.log;

events {
	# 1ワーカー毎の接続数
	worker_connections	2048;
}

http {
	include			/etc/nginx/mime.types;
	default_type	application/octet-stream;
	server_tokens	off;

	# ログフォーマットの指定
	log_format main	'$remote_addr - $remote_user [$time_local] "$request" '
					'$status $body_bytes_sent "$http_referer" '
					'"$http_user_agent" "$http_x_forwarded_for"';

	# アクセスログの出力先
	access_log	/var/log/nginx/access.log main;

	# ファイル読み込み、レスポンス送信を sendfile API で行う
	sendfile	on;
	tcp_nopush	on;

	# タイムアウト時間
	keepalive_timeout		3;
	# オープンしたファイル情報をキャッシュ
	open_file_cache			max=100000 inactive=60s;
	# オープンエラー情報はキャッシュしない
	open_file_cache_errors	off;

	# gzip 圧縮転送
	gzip				on;
	gzip_http_version	1.0;
	gzip_comp_level		1;
	gzip_disable		"msie6";
	gzip_min_length		1k;
	gzip_types			text/css
						text/javascript
						application/javascript
						application/json;

	include /etc/nginx/conf.d/*.conf;
}

server {
	listen			80;
	server_name		localhost;
	root			/var/www;
	index			index.php index.html index.htm;

	location / {
		rewrite ^.*$ /index.php last;

		location ~* \.(css|js|ico|gif|jpg|png|avi|mpg|mp4|webm|wmv|mp3|ogg|woff|txt|map)$ {
			root /var/www;
		}

		# php の実行設定
		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;
		}
	}
}

https 接続の設定

オレオレ証明書の作成。

# mkdir /etc/nginx/ssl/
# cd /etc/nginx/ssl/
# openssl genrsa 2048 > server.key
# openssl req -new -key server.key > server.csr
# openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt
# rm server.csr
# chmod 400 server.*

server {
	listen				443 ssl;
	ssl_certificate		/etc/nginx/ssl/server.crt;
	ssl_certificate_key	/etc/nginx/ssl/server.key;
	server_name			localhost;
	root				/var/www-ssl;
	index				index.php index.html index.htm;
}

設定を反映。

# systemctl reload nginx

/var/log/nginx/error.log に (Permission denied とか FastCGI sent in stderr:) と表示されるようなら、
SELinux が原因かもしれません。SELinux を一時的に無効にしてみます。

# setenforce Permissive

永続的に無効にする場合は、/etc/sysconfig/selinux を書き換え再起動します。

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled

centos/v7/nginx_のインストール.txt · 最終更新: 2018/10/17 11:38 by admin
 
特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC Attribution-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki