以前書いた記事は古くなったので、新しく書き直しました。自分用に。
さくらのVPSを使っているので、ところどころ www????.sakura.ne.jp として入力しています。
目次
ユーザ作成
まずはrootのパスワードを変更します。
# passwd作業用アカウントを作成します。
ユーザーhoge作成
# useradd hoge
hogeのパスワード設定
# passwd hoge
hogeをwheelグループに入れる
# usermod -G wheel hoge
作業用アカウント(wheelグループ)以外ではrootになれないように制限します。
sudoできるユーザを指定。
# visudoこの行を有効化
%wheel ALL=(ALL) ALLsuできるユーザを指定。
# vi /etc/pam.d/suこの行を有効化
auth required /lib/security/$ISA/pam_wheel.so use_uidrootで直接ログインできないようにします。
# vi /etc/ssh/sshd_configyesをnoに変更する
PermitRootLogin noSSH再起動。
# systemctl restart sshdTCP wrapperでsshにログインできるホストを制限します。
拒否リスト。
# vi /etc/hosts.deny以下を追加
sshd : all許可リスト。
# vi /etc/hosts.allow以下を追加
sshd : localhost
sshd : hoge.com
sshd : .jpこれでサーバ自身と.jp以外の人はアクセスできなくなりました。ちゃんと自分のプロバイダに合致したホスト名を指定しないと繋がらなくなります。
yumでRemiリポジトリを使う
標準のyumだと古いパッケージばかりなので、新しいのがインストールできるRemiリポジトリを使用します。
CentOS7用
# wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# rpm -Uvh remi-release-7.rpmRemiを指定してインストールする場合には以下のように記述します。
# yum --enablerepo=remi install (パッケージ名)アップデートは新旧入り混じると面倒なので、Remiは入れたのは個別でやった方が良いのかな。
通常パッケージ全部アップデート
# yum update
Remiで入れたの個別アップデート
# yum --enablerepo=remi update (パッケージ名)
ファイアウォール
起動
# systemctl enable firewalld
# systemctl start firewalld設定追加
# 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 --add-port=110/tcp --zone=public --permanent
# firewall-cmd --add-port=587/tcp --zone=public --permanent
# firewall-cmd --reload現在の設定は以下で確認できます。
# firewall-cmd --list-all --zone=publicwebサーバ
Apache 2.4をインストールします。
# yum install httpd起動
# systemctl enable httpd
# systemctl start httpdドメインごとに管理するユーザを分けます。普通にユーザを作成すると自分のホームディレクトリより上の階層に行けてしまうので、chrootを使って行けないようにします。
グループ作成。
# groupadd sftpgroupグループに所属するユーザにchrootを適用します。
# vi /etc/ssh/sshd_configサブシステム変更
#Subsystem sftp /usr/libexec/openssh/sftp-server
Subsystem sftp internal-sftp
グループと行ける範囲を末尾に追加
Match group sftpgroup
ChrootDirectory ~
ForceCommand internal-sftp
sshdを再起動します。設定間違うとサーバにアクセスできなくなるので、念の為ターミナルをもう1つ開いておいたり。
# systemctl restart sshdあとはユーザを作成していけば /home/hoge/ がsshのルートディレクトリになります。
ユーザ作成
# useradd -g sftpgroup hoge
# passwd hoge
ホームディレクトリの所有者をrootにする
# chown root:root /home/hoge
# chmod 755 /home/hoge
ディレクトリ作成とパーミッション変更
# mkdir /home/hoge/public_html
# chmod 777 /home/hoge/public_html
バーチャルホスト
1台のサーバで複数のドメインを運用したいので、バーチャルホストの設定をします。
# vi /etc/httpd/conf/httpd.confこの行のコメントアウトを外す
NameVirtualHost *:80ドメインごとにconfファイルを作成します。以下はhoge.com用です。
# vi /etc/httpd/conf.d/hoge.conf<VirtualHost *:80>
ServerAdmin webmaster@hoge.com
DocumentRoot /home/hoge/public_html
ServerName hoge.com
ServerAlias www.hoge.com
ErrorLog logs/hoge.com-error_log
CustomLog logs/hoge.com-access_log common
</VirtualHost>
公開前にサイトを確認できるようにします。ユーザー名がhogeの場合、こんなチルダ付のURLで見れるようになります。
http://www????.sakura.ne.jp/~hoge/
# vi /etc/httpd/conf.d/userdir.confコメントアウト
#UserDir disabled
有効化
UserDir public_html
Allにする
AllowOverride All
必要なものだけ
Options IncludesNoExec ExecCGI FollowSymLinks
SSL
OpenSSLは既に入っているので、mod_sslをインストールします。
# yum --enablerepo=remi install mod_sslSSL証明書は無料のLet’s Encryptを使います。取得方法は省略。
SSLサーバの設定は以下にまとめて書いておきます。
vi /etc/httpd/conf.d/vhost.conf<virtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/hoge.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hoge.com/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/hoge.com/chain.pem
ServerAdmin webmaster@hoge.com
DocumentRoot /home/hoge/public_html
ServerName hoge.com
ServerAlias www.hoge.com
ErrorLog logs/hoge.com-error_log
CustomLog logs/hoge.com-access_log common
</VirtualHost>
設定できたら再起動。
# systemctl restart httpdMySQL(MariaDB)
MariaDBインストールします。
# yum install mariadb mariadb-server起動
# systemctl enable mariadb
# systemctl start mariadb初期設定
# mysql_secure_installation
出てくる質問は以下の通り。
・MariaDBのrootにパスワード設定
・匿名ユーザ(anonymous)の削除
・リモートからrootユーザ接続禁止
・テスト用データベースの削除文字コードをUTF-8にする。
vi /etc/my.cnf.d/server.cnf[mysqld]
の後に以下を追加
character-set-server=utf8終わったら再起動
# systemctl restart mariadbPHP
PHP7とそれに関係するものをインストール。
# yum install --enablerepo=remi,remi-php70 php php-devel php-mysqlnd php-pear php-mbstring php-pdo php-gdphpMyAdmin
せっかくなのでMySQL(MariaDB)の管理ページを入れる。
# yum --enablerepo=remi-php70 install phpmyadminここにもアクセス制限。
vi /etc/httpd/conf.d/phpMyAdmin.conf<RequireAny>
の下に
Require host .jp
などメールサーバ
ない場合はインストール。
# yum install postfix cyrus-sasl cyrus-sasl-md5Postfix(送信)の設定。
# vi /etc/postfix/main.cf以下のように修正
myhostname = www????.sakura.ne.jp
mydomain = www????.sakura.ne.jp
myorigin = $mydomain
inet_interfaces = all
#inet_interfaces = localhost
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
home_mailbox = Maildir/
smtpd_banner = $myhostname ESMTP unknown
以下を追加(スパム対策)
disable_vrfy_command = yes
smtpd_helo_required = yes
以下を追加(バーチャルドメイン用)
virtual_alias_maps = hash:/etc/postfix/virtual
以下を追加(SMTP-Auth用)
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = cyrus
smtpd_sasl_path = smtpd
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = smtpd_relay_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
以下を追加(メールへの添付可能サイズを無制限にする)
message_size_limit = 0
mailbox_size_limit = 0
メール送信時にサブミッションポート(587)を使用します。
# vi /etc/postfix/master.cf以下の行のコメントアウトを外す
submission inet n - n - - smtpd
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING再起動。
# systemctl restart postfix
# systemctl enable postfixSMTP認証のためにSaslauthdを使用します。アプリと認証処理を分離してくれるものらしい。
# vi /etc/sysconfig/saslauthdLinuxユーザのパスワード(/etc/shadow)を使う
MECH=shadow起動します。
# systemctl enable saslauthd
# systemctl start saslauthdDovecot(受信)をインストールします。
yum install dovecot設定していきます。
# vi /etc/dovecot/conf.d/10-mail.conf以下の行のコメントアウトを外す
mail_location = maildir:~/Maildir# vi /etc/dovecot/dovecot.confprotocols = imap pop3 lmtp
listen = *# vi /etc/dovecot/conf.d/10-auth.confパスワードを平文にする
disable_plaintext_auth = no# vi /etc/dovecot/conf.d/10-ssl.confSSL接続しない
ssl = noDovecotを起動します。
# systemctl enable dovecot
# systemctl start dovecotuseradd時にメールボックス用のディレクトリを自動で作成されるようにします。
# mkdir -p /etc/skel/Maildir/{new,cur,tmp}
# chmod -R 700 /etc/skel/Maildir/メールアドレス作成
メールアカウント用のユーザをシェルログイン不可で作成します。
# useradd -s /sbin/nologin hoge-info
# passwd hoge-infouseraddしたらメールボックスはこのように作成されました。
/home/hoge-info/Maildir/new/
/home/hoge-info/Maildir/cur/
/home/hoge-info/Maildir/tmp/メールアドレスとその振り分け処理を作成します。ドメインごとにanythingの行を入れないとメールが使用できません。
# vi /etc/postfix/virtualhoge.com anything
info@hoge.com hoge-info
web@hoge.com hoge-web振り分けを反映させます。
# postmap /etc/postfix/virtualまとめ
1記事にまとめてみたけど、やっぱ設定項目多いね。