この記事はcentos7+nginx+php7向けに書いています。
OSを入れたあと、必ず最初にする設定をメモしています。()
環境
OS確認
$ cat /etc/redhat-release
日本語化(ロケールの変更)
$ localectl set-locale LANG=ja_JP.UTF-8
確認
$ localectl status
ホスト名の変更
$ hostnamectl set-hostname ***.com
$ vi /etc/hosts
SELinuxを切る
$ setenforce 0
Disabledを確認
$ getenforce
SELINUX=disabledを確認
$ less /etc/selinux/config
systemctlでbashの補完をきかせる
$ yum install -y bash-completion
ネット関係のコマンド(ifconfig,nslookupなど)を入れる
$ yum install -y net-tools bind-utils
yum
EPELリポジトリを追加
$ yum -y install epel-release
Remiリポジトリを追加
$ rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
or
$ wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
$ rpm -Uvh remi-release-7.rpm
最新化
$ yum update
接続制限
$ vi /etc/ssh/sshd_config
PermitRootLogin no
…
PasswordAuthentication no
…
AuthorizedKeysFile .ssh/authorized_keys //公開鍵認証によるSSH接続の許可
sshd再起動
$ systemctl restart sshd
確認
$ systemctl status sshd.service
root権限のユーザhogeの作成
$ useradd hoge
$ passwd hoge
$ vi /etc/sudoers
hoge ALL=NOPASSWD: ALL
鍵の作成
$ cd /home/hoge/
$ ssh-keygen -t rsa
※.ssh/配下にid_rsa、id_rsa.pubという2つのファイルが作られる。前者が秘密鍵、後者が公開鍵
$ mkdir .ssh
$ mv id_rsa.pub .ssh/authorized_keys
$ chmod -R 700 /home/hoge/.ssh/
Port空け
※centos7はcentos6と違いnetstatがない
空いてるポートの確認
$ ss -nat
使っているポートの確認
$ yum -y install lsof
$ lsof -i:<port番号>
firewallの確認
$ systemctl status firewalld
$ firewall-cmd --list-all
webサーバ
$ firewall-cmd --add-service=http --zone=public --permanent
$ firewall-cmd --add-service=https --zone=public --permanent
メールサーバ
$ firewall-cmd --add-service=smtp --zone=public --permanent
反映
$ firewall-cmd --reload
確認
$ firewall-cmd --list-all
SSHのポート変更
$ vi /etc/ssh/sshd_config
- # Port 22
+ Port xxxx
$ systemctl restart sshd
firewalldの設定
$ vi /usr/lib/firewalld/services/ssh.xml
- <port protocol="tcp" port="22"/>
+ <port protocol="tcp" port="xxxx"/>
反映
$ firewall-cmd --reload
確認
$ firewall-cmd --list-all
webサーバ(Nginx)
インストール
$ yum install -y nginx
確認※Vが大文字だとオプションまで表示
$ nginx -V
モジュールを一覧で確認
$ nginx -V 2>&1 | grep -oP '[a-z_]+_module'
他に必要そうなものもインストールしておく
$ yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
※モジュールを後から入れたい場合は、ソースからビルドする必要がある
$ mkdir /usr/local/src/source/ngingx-X.XX.X/
$ cd /usr/local/src/source/ngingx-X.XX.X/
$ wget https://nginx.org/download/nginx-X.XX.X.tar.gz
$ tar -xzvf nginx-X.XX.X.tar.gz
$ wget モジュール
※依存関係があれば他にもインストール
$ tar -xzvf モジュール
$ cd nginx-X.XX.X
$ ./configure --with-モジュール ・・・・・
$ make
$ make install
設定
$ vi /etc/nginx/nginx.conf
コンフィグチェックして起動
$ nginx -t
$ systemctl start nginx.service
再起動
$ systemctl reload nginx.service
確認
$ systemctl status nginx.service
ログ
$ ls -la /var/log/nginx/
自動起動
$ systemctl enable nginx.service
※ベーシック認証
$ htpasswd -c -b /etc/httpd/conf/.htpasswd ユーザ名 パスワード
オプション -c : 新しいパスワードファイルを作成する。既存ファイルがあった場合は上書きされる。
オプション -b : パスワードを対話式ではなくコマンドライン引数として入力できるようにする
php(+php-fpm)
インストール
$ yum -y install --enablerepo=remi,remi-php71 php php-devel php-mbstring php-pdo php-gd php-xml php-mcrypt
ヴァージョン確認
$ php -v
モジュール確認
$ php -m
php.iniの編集
#X-Powered-Byの出力禁止
expose_php = Off
date.timezone = "Asia/Tokyo"
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = UTF-8
mbstring.http_output = pass
mbstring.encoding_translation = On
mbstring.detect_order = auto
mbstring.substitute_character = none
memory_limit 512M
その他、個別の設定に関しては、nginxで
fastcgi_param PHP_VALUE "display_errors=on
upload_tmp_dir=/home/hoge/temp/
post_max_size=80M
upload_max_filesize=4M
";
のようにして設定する
composer
$ curl -sS https://getcomposer.org/installer | php'
$ mv composer.phar /usr/local/bin/composer'
パスが通っている場所にリネームする
php-fpm
$ yum -y install --enablerepo=remi,remi-php71 php-fpm
$ php-fpm -v
設定
$ vi /etc/php-fpm.d/www.conf
nginxの実行ユーザと合わせる
user = nginx
group = nginx
;UNIX socketで通信する
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
;php.iniのmax_execution_timeと揃える
request_terminate_timeout = 30
起動
$ systemctl start php-fpm.service
php-fpm.sockが作られる
$ ls -la /var/run/php-fpm
nginx側の設定に反映して、再起動
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
$ systemctl reload nginx.service
自動起動
$ systemctl enable php-fpm.service
自動起動
リストの確認
$ systemctl list-unit-files -t service
※※cent6 のときの chkconfig --list
設定
$ systemctl enable <サービス名>.service