2014年1月2日木曜日

MySQLをインストール

次はMySQLをインストールしてみたいと思います。
今回は久々にyumでインストールします。
まずは、以下のコマンドでMySQL Serverをインストールします。
# yum -y install mysql-server

次に、MySQLのConfファイルの修正。[mysqld]の中に、以下を追加。
# vi /etc/my.cnf
character-set-server = utf8

MySQLが起動できるか確認する。
# service mysqld start
MySQL データベースを初期化中:  Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
mysqld を起動中:                                           [  OK  ]
character-set-server = utf8

起動が確認できたら、自動起動するように設定する。
# chkconfig mysqld on

MySQLの初期設定を行います。
# 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):
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] ←空エンター
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] ←空エンター
 ... 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] ←空エンター
 ... Success!

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] ←空エンター
 - 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] ←空エンター
 ... 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!

MySQLがしっかり動くかの確認は、以下の参考ページを参考に。

http://centossrv.com/mysql.shtml

Tomcatをインストールする

ApacheもJava(JRE)もインストールできたということで、Tomcatをインストールしてみたいと思います。

まず、rootでTomcatを動かすとセキュリティ的にまずいので、Tomcat実行専用ユーザーを作ります。
ログインシェルに/sbin/nologinを指定して通常利用はできないようにしておきます。
# useradd -s /sbin/nologin tomcat

そしていつも通り、Tomcat公式ページから最新版のTomcatのダウンロードURLをゲットしてwgetでダウンロードし、解凍します。
# wget http://ftp.tsukuba.wide.ad.jp/software/apache/tomcat/tomcat-7/v7.0.47/bin/apache-tomcat-7.0.47.tar.gz
# tar zxvf apache-tomcat-7.0.47.tar.gz

解凍したTomcatを、アプリ用ディレクトリへ名前を変えて移動します。
# mv apache-tomcat-7.0.47 /usr/local/tomcat-7.0.47

次に、TomcatのWeb管理コンソールにアクセスする為のユーザーを作成する(CentOSには関係の無い、Tomcat用のユーザー)。
# vi /usr/local/tomcat-7.0.47/conf/tomcat-users.xml


※usernameとpasswordを、任意の物に変更する

最後に、Tomcat自動起動スクリプトを設置し、OS起動時にTomcatも自動起動するようにする。
# vi /etc/init.d/tomcat

#!/bin/bash
#
# Startup script for the Tomcat Servlet Container
#
# chkconfig: 2345 35 65
# description: Tomcat is the servlet container that is used in the official \
#              Reference Implementation for the Java Servlet and JavaServer \
#              Pages technologies

TOMCAT_USER=tomcat
CATALINA_HOME=/usr/local/tomcat-7.0.47

. /etc/rc.d/init.d/functions
prog=tomcat

start() {
    echo -n $"Starting $prog: "
    daemon --user $TOMCAT_USER $CATALINA_HOME/bin/startup.sh > /dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        echo_success
    else
        echo_failure
    fi
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/$prog
    return $RETVAL
}
stop() {
    echo -n $"Stopping $prog: "
    daemon --user $TOMCAT_USER $CATALINA_HOME/bin/shutdown.sh > /dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        echo_success
    else
        echo_failure
    fi
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/$prog
    return $RETVAL
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  status)
    INSTANCES=`ps --columns 512 -aef|grep java|grep tomcat|grep org.apache.catalina.startup.Bootstrap|wc -l`
    if [ $INSTANCES -eq 0 ]; then
        echo $prog is stopped
        RETVAL=3
    else
        if [ $INSTANCES -eq 1 ]; then
            echo $prog is running 1 instance...
        else
            echo $prog is running $INSTANCES instances...
        fi
        RETVAL=0
    fi
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|status|help}"
    exit 1
esac

exit $RETVAL

設置したTomcat自動起動スクリプトに実行権限を与えます。
# chmod +x  /etc/init.d/tomcat

あとは自動起動の設定!
# /sbin/chkconfig --add tomcat
http://IPアドレス:8080にアクセスしてTomcatトップページが表示されるか確認するのみ。

2013年12月13日金曜日

CentOSにJREをインストール

Java(JRE)をインストールしてみます。
CentOSにデフォルトでついてくるOpenJDKではなく、OracleのJREを入れます。
開発はしないのでJREでいいよねってことで。

また、JREを複数のバージョンをインストールして切り替えられるようにします。

まずは、下記サイトからJREをダウンロードする。
http://www.oracle.com/technetwork/java/javase/downloads/index.html

で、インストール。
# rpm -ivh jre-7u45-linux-x64.rpm

使用するJavaを選択します。
# alternatives --install /usr/bin/java java /usr/java/default/bin/java 17045
第一引数が実行するリンクを指定、第二引数が登録対象のグループを指定、第三引数が実際に実行するコマンドのパスを指定、第四引数が優先度を指定する。
17045は、JRE1.7.0_45なので「17045」とした。

次に、使用するJavaを指定します。
# alternatives --config java

1 プログラムがあり 'java' を提供します。

  選択       コマンド
-----------------------------------------------
*+ 1           /usr/java/default/bin/java

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:1
使用したいJavaのバージョンを指定して、Enterをクリック。

最後に、「java -version」でそれが指定されたか確認。


参考HP様
http://d.hatena.ne.jp/necoyama3/20110505/1304587888
http://www.aishinsys.co.jp/wordpress/?p=551

2013年12月9日月曜日

Apache2.4をCentOS6.4にソースからインストール

Apacheをソースからインストールしてみる。
本来chekcinstall入れたんだから、RPM化しろよ!って話だけど、今回はソースから入れるお勉強と言うことで。

インストールに際していろいろ調べてたら、Apache2.4系から2.2系とは入れ方が若干異なるんだってさ。
Apache Portable Runtim(APR)ってのを入れなくちゃいけないんだって。
なので、まずはAPRとAPR-utilがインストール済みか確認。
# rpm -qa | apr
apr-util-ldap-1.3.9-3.el6_0.1.x86_64
apr-1.3.9-5.el6_2.x86_64
apr-util-1.3.9-3.el6_0.1.x86_64

上記のように出てきたら、aprもapr-utilもインストール済み。
でも最新版のAPRとAPR-Utilを使わないとうまくいかない場合があるみたいなので、アンインストールします。
アンインストールしようと「rpm -e apr」をやろうとすると、依存性エラーでアンインストールができません。
仕方なく、startxでGUIを起動し、そっちからアンインストール(GUIでやれば、依存性のある物も一緒に消してくれるみたい)
かっこわりー。

で、最初にopenssl-develをインストール
# yum -y install openssl-devel

次に、https://apr.apache.org/download.cgi から、APRの最新版をダウンロード&解凍
# wget http://ftp.jaist.ac.jp/pub/apache//apr/apr-1.5.0.tar.gz
# tar zxvf apr-1.5.0.tar.gz

解凍したAPRをインストールします。
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr1.5.0
# make
# make install

次に、APR-utilをダウンロード&インストールします。
最新版はAPRと同じページから取得します。
# wget http://ftp.jaist.ac.jp/pub/apache//apr/apr-util-1.5.3.tar.gz
# tar zxvf apr-util-1.5.3.tar.gz

インストール!
# cd apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util-1.5.3 --with-apr=/usr/local/apr1.5.0
# make
# make install

あと、PCREとか言う物もインストールが必要みたい。
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/で最新版を探してDL&解凍します。
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
# tar zxvf pcre-8.33.tar.gz

インストール!
# cd pcre-8.33
# ./configure --prefix=/usr/local/pcre-8.33
# make
# make install

https://httpd.apache.org/download.cgiで、最新版Apacheをで調べて、ダウンロード&解凍します。
# wget http://ftp.tsukuba.wide.ad.jp/software/apache//httpd/httpd-2.4.7.tar.gz
# tar zxvf httpd-2.4.7.tar.gz

Apacheをインストール!
# cd httpd-2.4.7
# ./configure \
--prefix=/usr/local/apache2.4.7 \
--enable-so \
--enable-ssl \
--enable-rewrite \
--with-apr=/usr/local/apr1.5.0 \
--with-apr-util=/usr/local/apr-util-1.5.3 \
--with-pcre=/usr/local/pcre-8.33 \
--with-berkeley-db \
--enable-dav

上記のオプションは、
・prefixでインストール先を指定
・DSO, SSL, mod_rewrite モジュールをApacheに組み込み
・with-apr, with-apr-util オプションは、APR, APR-utilがインストールされているディレクトリのパスを指定

で、次にいよいよApacheインストール。
# make
# make install

これでApacheのインストールは完了!

次は設定。

User apache       #ユーザーをapacheに
Group apache      #グループをapacheに

ServerAdmin hostmasnter@example.com #サーバーアドミンのコメントを外して、適当なメールアドレスにする

ServerName example.com:80   #サーバーネームのコメントを外して、そのサーバのドメインにする

最低限、上記の設定で動くはず。
最後に起動!
# /usr/local/apache2.4.7/bin/apachectl start


次にOS起動時にApacheが自動起動するように設定します。
まずは起動スクリプトの編集。
# vi /usr/local/apache2.4.7/bin/apachectl
↓2、3行明に以下を追加
# chkconfig:345 91 91
# description:Apache Boot

apachectlをinit.dにシンボリックリンクを張ります。
ln -s /usr/local/apache2.4.7/bin/apachectl /etc/init.d/httpd

自動起動するように設定
# chkconfig --add httpd

もし「It's Work!」が見られない場合、ファイアーウォールとSELinuxの設定を見直してね。

次はSSLの設定をやってきたいと思います!


参照HP様
http://apache24.jugem.jp/?eid=2
http://blog.jicoman.info/2012/12/apache_source/
http://plaza.rakuten.co.jp/zhenwu/diary/201207220000/

2013年8月25日日曜日

ChecnkInstallの環境を整える

CentOS 6.4(64bit)で、ソースからrpmを作ってくれるcheckinstallのインストールを行う
完全にこちらのコピペですが、リンク先がなくなったときのこととか考えてここにも備忘録として残す。

まず、checkinstallの最新版はGITにあるので、GITをインストールしなくてはいけない。
   # yum -y install git

あと、Developmentツールもインストールする必要があるので、yumで入れちゃう。
   # yum groupinstall "Development Tools"

次に、checkinstallに必要な、依存パッケージをインストールします。
   # yum install -y gettext rpm-build

checkinstallの最新ソースを、GITから落としてくる。
※なぜかcheckinstallの公式リポジトリからは落とせなかったので、以下のgithubから入手
   # cd /usr/local/src/
   # git clone https://github.com/buserror-uk/checkinstall.git

以下のように設定ファイルを変更
※githubからDLした場合、設定済みっぽいけど一応
   # cd checkinstall
   # sed -i 's|MAKEPKG=/sbin/makepkg|MAKEPKG=|' checkinstallrc-dist
   # sed -i 's|INSTYPE=""|INSTYPE="R"|' checkinstallrc-dist
   # sed -i 's|TRANSLATE=1|TRANSLATE=0|' checkinstallrc-dist
   # sed -i 's|EXCLUDE=""|EXCLUDE="/selinux"|' checkinstallrc-dist
   # sed -i 's|/lib|/lib64|' installwatch/Makefile
   # sed -i 's|CONFDIR=$(PREFIX)/lib/checkinstall|CONFDIR=$(PREFIX)/etc|' Makefile
   # sed -i '66s|^$|CHECKINSTALLRC=$INSTALLDIR/etc/checkinstallrc|' checkinstall

makeとmakeinstallをやっちゃう
   # make
   # make install

chcekinstallのバージョン確認で、バージョンが確認できればインストールOK!
   # checkinstall -v

初回のみ、作業用ディレクトリを掘ってやる必要があるらしい。
   # mkdir -p /root/rpmbuild/SOURCES

インストールしたcheckinstallを利用して、chekcinstall自体をrpm化する。
   # checkinstall

生成されたrpmを確認する。
   # rpm -qlp /root/rpmbuild/RPMS/x86_64/checkinstall-20130825-1.x86_64.rpm

問題なければインストールする。
   # rpm -ivh /root/rpmbuild/RPMS/x86_64/checkinstall-20130825-1.x86_64.rpm

念のための確認は、元ネタさん参照。

2013年8月24日土曜日

Linuxにパッケージをインストールする際、rpm(yum)とソースからコンパイルする方法があるが、それについて調査してみた。

○rpm(yum)のメリット
・yum install xxxというコマンド一発でインストールされるので、とにかくラク
・パッケージ管理をしっかりやってくれるため、アップデートやアンインストールらラク

○rpm(yum)のデメリット
・同じパッケージを複数入れられない
 (たとえばApacheを複数バージョン入れて、それぞれポートを変えて運用みたいなことはできない)
・yumのリポジトリに最新のパッケージが上がっていない場合、古いバージョンのパッケージを使わざる終えない
 (リポジトリにRPMforge、Remi、EPELを追加してあげることで、多少は逃れられる)参考

○ソースコンパイルのメリット
・パッケージの最新版が利用できる
・同じパッケージを複数入れられる

○ソースコンパイルのデメリット
・パッケージ管理をやってくれないため、自分でやる必要がある
 →何かしらのドキュメントを作って一覧管理する必要がある。
 →アンインストールのことを考え、./configureにオプションを追加する必要がある
 →ただし、ソースをrpmに変換してインストールしてあげればパッケージ管理してもらえる

つまり以下の方針が立てられる
・基本的にはrpm(yum)でインストールする
・ただし、同様のパッケージを複数入れる必要がある場合、ソースコンパイルにてインストールする
 (Apache、Tomcat、Rubyなどなど)
・OpenSSLやOpneSSHなど、セキュリティ的に最新版を入れてあげる必要があることをそうていして、ソースからrpmを生成する方法を確立する
 ⇒(追記)どうやら、Red Hat系(CentOS含)は、OpenSSHやOpenSSLに独自のカスタマイズを入れてるから、自力でのアップデートはよした方がいいみたい。

こんなかんじだろうか?
⇒まずはじめに、ソースからrpmを生成する方法を確立しよう!

CentOSインストール時にやること

CentOSをインストールする際、基本はCUIで操作するけど、たまにGUI使いたくなる可能性がある場合、以下のようにすれば良い。

・CentOSをインストールする際、GUIを一緒にインストールする
・/etc/inittabで、ランレベルを「3」にする
・/usr/bin/startxに、「LANG=ja_JP.UTF-8」の表記を追加する ※startxしたときに、日本語で立ち上がる

ネットワークの設定
vi /etc/sysconfig/network-scripts/ifcfg-eth0 で以下をやる。
 - ONBOOTを「yes」にする
 ※IPを固定にしたい場合、以下の2つを追加する
 - BOOTPROTO=static
 -IPADDR=192.168.0.10
 -NETMASK=255.255.255.0
 -GATEWAY=192.168.0.1
 -DNS1=192.168.0.1

いらないサービスの停止
以下のコマンドで、起動中のサービスを確認し、いらないものは停止(または削除)する

chkconfig --list | grep  "3:on"
・メールサーバは使わないだろうからOFFにしよう(chkconfig postfix off)

○RPMリポジトリを追加しよう!
http://dqn.sakusakutto.jp/2013/02/centos6_yum_remi_epel_rpmforge.htmlを参照

○アンチウイルスソフトをインストールしよう
yum –y --enablerepo=epel install clamdで Clam AntiVirus をインストール
あとはhttp://centossrv.com/clamav.shtmlを参照して、設定

○SELinuxを無効化する
SELinuxを無効化したい場合、/etc/sysconfig/selinuxを開いて、以下のように書き換える。
SELINUX=disabled