0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WebArena VPSクラウドでCentOS6.4の初期設定を行う

Last updated at Posted at 2013-05-31

CentOSの初期設定について、素人ながら覚書。
WebArena VPSクラウドにて設定を行っているので、
CentOSの『minimal』インストールとは初期設定手順が違う事に注意。

それにしても折角セキュリティグループを利用して、
FWの設定が出来るのに、複数契約をしても、
グローバルIP経由でしかアクセスできないなんて、
WEB+DB構成が気軽に作れないな。

SSHログインのポート番号を変更する

/etc/ssh/sshd_configの設定
$ cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.`date '+%Y%m%d'`
$ sed -i 's/#Port 22/Port 61000/g' /etc/ssh/sshd_config

※Well Knownポートを除外した49152~61000のポートを指定する。
※ポート番号の決定には『乱数メーカー』を利用すると楽
http://mpnets.net/rand/index.cgi?do=1&lnum=49152&mnum=61000&enum=1

ルートユーザーのパスワードを設定する

パスワードの設定をする
$ passwd
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

※パスワードは十分な長さの英数記号入交のものが推奨される。
※パスワードの決定には『ランダムパスワード生成』を利用すると楽
http://randpass.ideeile.com/?s=1&b=1&n=1&m=1&d=16&c=1

SSHログインを再実行

管理ユーザーを追加し、管理ユーザーでSSHログインが出来るように設定する。

管理ユーザーを追加する。
$ useradd admin
$ passwd admin
Changing password for user admin.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
$ sed -i 's/#Port 22/Port 61000/g' /etc/ssh/sshd_config
$ sed -i 's/#RSAAuthentication yes/RSAAuthentication yes/g' /etc/ssh/sshd_config
$ sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
$ sed -i 's|#AuthorizedKeysFile\t.ssh/authorized_keys|AuthorizedKeysFile.ssh/authorized_keys|g' /etc/ssh/sshd_config
$ mkdir -p /home/admin/.ssh/
$ cp -a /root/.ssh/authorized_keys /home/admin/.ssh/authorized_keys
$ chmod 700 /home/admin/.ssh/
$ chmod 600 /home/admin/.ssh/authorized_keys
$ chown admin:admin /home/admin/.ssh/
$ chown admin:admin /home/admin/.ssh/authorized_keys

※本来ならば管理者用の鍵は分けた方がいいと思われる。
※(参考)ssh で公開鍵認証方式を利用するには
http://d.hatena.ne.jp/eco31/20101031/1288525237

不要な初期インストールサービスを削除する

初期インストール済みのサービスは以下の通り
openssh
httpd
webalizer
vsftpd
mysql
postgresql
gcc
php
ruby
perl
python

例Postgresqlを削除
$ yum remove postgresql* -y

※(参考)OS・ソフトウェア
http://web.arena.ne.jp/support/vps-cloud/manual/vps-cloud/centos6/software.html

Webサーバの設定をする

Webサーバの設定をする(ドメインを『www.example.com』、管理者アドレスを『root@example.com』として設定)
$ cp -a /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.`date '+%Y%m%d'`
$ cp -a /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.`date '+%Y%m%d'`
$ sed -i 's/Listen 80/#Listen 80/g' /etc/httpd/conf/httpd.conf
$ sed -i 's/#ServerName www.example.com:80/ServerName fqdn.jp/g' /etc/httpd/conf/httpd.conf
$ sed -i 's/ServerAdmin root@localhost/ServerAdmin root@example.com/g' /etc/httpd/conf/httpd.conf
$ cat <<_EOF_ > /etc/httpd/conf.d/00_listen.conf
> Listen 80
> #Listen 443
_EOF_
$ cat <<_EOF_ > /etc/httpd/conf.d/01_security.conf
> # 余計な情報を非表示にする
> TraceEnable Off
> ServerSignature Off
> ServerTokens ProductOnly
> Header unset X-Powered-By
_EOF_
$ cat <<_EOF_ > /etc/httpd/conf.d/02_performance.conf
> KeepAlive On
> # KeepAlive に関する不具合回避
> BrowserMatch "Mozilla/2" nokeepalive
> BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
> BrowserMatch "RealPlayer 4\.0" force-response-1.0
> BrowserMatch "Java/1\.0" force-response-1.0
> BrowserMatch "JDK/1\.0" force-response-1.0
> 
> # プログラムログ以外はhost名をlookupしない
> HostnameLookups off
> <Files ~ "\.(shtml|cgi|php|jsp)$">
>     HostnameLookups on
> </Files>
> 
> #テキストファイルのEtagを削除
> <Files ~ "\.(?:htm?l|css|js|txt|xml|yml|csv|)$">
> FileETag             None
> </Files>
> # 画像ファイルのEtagを削除
> <Files ~ "\.(?:ico|gif|jpeg||jpgpng|svg|swf)$">
> FileETag             None
> </Files>
> # 動画・音楽ファイルのEtagを削除
> <Files ~ "\.(?:avi|wma|wmv|mov|flv|swf|mp3|mp4|rm)$">
> FileETag             None
> </Files>
> #圧縮ファイルのEtagを削除
> <Files ~ "\.(?:zip|rar|t?gz|bz2|sit|rar|lzh|7z)$">
> FileETag             None
> </Files>
_EOF_
$ cat <<_EOF_ > /etc/httpd/conf.d/99_vhost-fqdn.jp.conf.template
> # fqdn.jp
> <VirtualHost *:80>
>  ServerName fqdn.jp
>  DocumentRoot /opt/domains/fqdn.jp/public
>  DirectoryIndex index.html index.htm index.php
> 
>  <Directory "/opt/domains/fqdn.jp/public/">
>    Options FollowSymLinks +ExecCGI
>    AllowOverride All
>    Order allow,deny
>    Allow from all
>    #Deny from 1.2.3.4/32
>  </Directory>
> 
>  #LogLevel warn
> 
>  # 画像・テキストファイルのログは取得しない
>  SetEnvIfNoCase Request_URI "\.(ico|gif|jpeg|jpg|png|svg|css|js|txt|swf)$" nolog
>  # 動画・音楽ファイルのログは取得しない
>  SetEnvIfNoCase Request_URI "\.(avi|wma|wmv|mov|flv|swf|mp3|mp4|rm)$" nolog
> 
>  CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/fqdn.jp-access_log_%Y%m%d 86400 540" combined env=!nolog
>  ErrorLog "|/usr/sbin/rotatelogs /var/log/httpd/fqdn.jp-error_log_%Y%m%d 86400 540"
> 
> #CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/ssl_fqdn.jp-access_log_%Y%m%d 86400 540" combined env=!nolog
> #ErrorLog "|/usr/sbin/rotatelogs /var/log/httpd/ssl_fqdn.jp-error_log_%Y%m%d 86400 540"
> 
> #SSLEngine on
> #SSLProtocol all -SSLv2
> 
> #SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
> #SSLCertificateFile /opt/domains/fqdn.jp/ssl.key/fqdn.jp.`date '+%Y%m%d'`.crt
> #SSLCertificateKeyFile /opt/domains/fqdn.jp/ssl.key/fqdn.jp.`date '+%Y%m%d'`.key
> #SSLCACertificateFile /opt/domains/fqdn.jp/ssl.key/fqdn.jp.`date '+%Y%m%d'`.ca.crt
> 
> #<Files ~ "\.(cgi|shtml|phtml|php3?)$">
> #SSLOptions +StdEnvVars
> #</Files>
> 
> # IEのSSL+KeepAliveの不具合に関する対応
> #BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
> #BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
> 
> </VirtualHost>
_EOF_
$ mkdir -p /opt/domains/fqdn.jp/ssl.key/

※apacheのconf.dのファイルの読み込み順はアルファベット順の為、
 ファイル名に番号をつける。

MYSQLサーバを設定する

例my.cnfを設定する
$ cp -a /etc/my.cnf /etc/my.cnf.`date '+%Y%m%d'`
$ cat <<_EOF_ > /etc/my.cnf
>[client]
>port            					= 3306
>socket          					= /var/lib/mysql/mysql.sock
>
>[mysql]
>no-auto-rehash
>#safe-updates
>prompt          					= '\u@<HOSTNAME> mysql>'
>default_character_set 				= utf8
>
>[mysqld]
>datadir = /var/lib/mysql
>port            					= 3306
>socket          					= /var/lib/mysql/mysql.sock
>skip-external-locking
>key_buffer_size						= 32M
>key_buffer 							= 32M
>max_allowed_packet 					= 2M
>table_cache 						= 256
>max_connections 					= 300
>max_connect_errors 					= 1000
>sort_buffer_size 					= 2M
>join_buffer_size 					= 256K
>read_buffer_size 					= 1M
>read_rnd_buffer_size 				= 1M
>myisam_sort_buffer_size 			= 1M
>thread_cache 						= 256
>query_cache_size 					= 0M
>query_cache_type 					= 0
>thread_concurrency 					= 4
>tmp_table_size 						= 64M
>max_heap_table_size 				= 64M
>net_buffer_length					= 16K
>
>skip-name-resolve
>character-set-server				= utf8
>default-storage-engine				= InnoDB
>
>log-bin								= mysql-bin
>relay-log=relay-mysql-bin
>binlog_format						= mixed
>
>slow_query_log 						= 1
>long_query_time 					= 3
>
>ignore-builtin-innodb
>plugin-load=innodb=ha_innodb_plugin.so;innodb_trx=ha_innodb_plugin.so;innodb_locks=ha_innodb_plugin.so;innodb_lock_waits=ha_innodb_plugin.so;innodb_cmp=ha_innodb_plugin.so;innodb_cmp_reset=ha_innodb_plugin.so;innodb_cmpmem=ha_innodb_plugin.so;innodb_cmpmem_reset=ha_innodb_plugin.so
innodb_strict_mode
>innodb_data_file_path 				= ibdata1:10M:autoextend
>innodb_data_home_dir 				= /var/lib/mysql
>innodb_file_format 					= Barracuda
>innodb_file_per_table
>innodb_buffer_pool_size 			= 600M
>innodb_additional_mem_pool_size	 	= 20M
>innodb_write_io_threads 			= 8
>innodb_read_io_threads 				= 8
>innodb_thread_concurrency 			= 16
>innodb_flush_log_at_trx_commit 		= 1
>innodb_log_file_size 				= 128M
>innodb_log_buffer_size				= 16M
>innodb_log_files_in_group 			= 2
>innodb_flush_method					= O_DIRECT
>innodb_lock_wait_timeout 			= 120
>
>[mysqldump]
>quick
>max_allowed_packet 					= 16M
>
>[myisamchk]
>key_buffer 							= 8M
>sort_buffer_size 					= 8M
>read_buffer 						= 1M
>write_buffer						= 0M
_EOF_
$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

※my.cnfは環境に応じて調整する。このあたりを参照にする。
http://blog.nomadscafe.jp/2012/10/mysql-mycnf-github.html

FTPサーバの設定をする。

FTPサーバの基本的な設定を行う。

vsftpd.confを設定する。
$ cp -a /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.`date '+%Y%m%d'`
$ sed -i 's/anonymous_enable=YES/anonymous_enable=NO/g' /etc/vsftpd/vsftpd.conf
$ sed -i 's/#ascii_upload_enable=YES/ascii_upload_enable=YES/g' /etc/vsftpd/vsftpd.conf
$ sed -i 's/#ascii_download_enable=YES/ascii_download_enable=YES/g' /etc/vsftpd/vsftpd.conf
$ sed -i '/tcp_wrappers=YES/a\\nuse_localtime=YES'  /etc/vsftpd/vsftpd.conf
$ sed -i 's/#chroot_list_enable=YES/chroot_list_enable=YES/g' /etc/vsftpd/vsftpd.conf
$ sed -i 's|#chroot_list_file=/etc/vsftpd|chroot_list_file=/etc/vsftpd|g' /etc/vsftpd/vsftpd.conf

FTPサーバを用のアカウントを用意する。

許可したユーザーのみftpアクセスできるように設定する。

許可したユーザーのみftpアクセスできるように設定する。
$ sed -i '/userlist_enable=YES/a\userlist_deny=NO'  /etc/vsftpd/vsftpd.conf
$ sed -i '/userlist_deny=NO/a\userlist_file=\/etc\/vsftpd\/user_list'  /etc/vsftpd/vsftpd.conf
$ cp -a /etc/vsftpd/user_list /etc/vsftpd/deny_user_list
$ cp -a /etc/vsftpd/user_list /etc/vsftpd/user_list.`date '+%Y%m%d'`
$ cat <<_EOF_ > /etc/vsftpd/user_list
admin
_EOF_
$ cp -a /etc/pam.d/vsftpd /etc/pam.d/vsftpd.`date '+%Y%m%d'`
$ sed -i 's|file=/etc/vsftpd/ftpusers|file=/etc/vsftpd/deny_user_list|g' /etc/pam.d/vsftpd
$ sed -i '/use_localtime=YES/a\listen_port=61001' /etc/vsftpd/vsftpd.conf
$ sed -i 's/connect_from_port_20=YES/connect_from_port_20=NO/g' /etc/vsftpd/vsftpd.conf
$ sed -i '/listen_port=61001/a\pasv_enable=YES' /etc/vsftpd/vsftpd.conf
$ sed -i '/pasv_enable=YES/a\pasv_address=1.2.3.4' /etc/vsftpd/vsftpd.conf
$ sed -i '/pasv_address=1.2.3.4/a\pasv_min_port=61002' /etc/vsftpd/vsftpd.conf
$ sed -i '/pasv_min_port=61002/a\pasv_max_port=61003' /etc/vsftpd/vsftpd.conf
$ chkconfig vsftpd on
$ /etc/init.d/vsftpd start

※pasvモードの設定がうまくいかないので対処中。

SSHログインのルートユーザーログインを拒否する

sshd_configを設定する。
$ sed -i 's/#PermitRootLogin yes/#PermitRootLogin no/g' /etc/ssh/sshd_config
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?