1からLinuxに本番環境の設定をする CentOS(5)Gitのインストールと設定
まずはGit(ギット)のインストール
$ sudo yum install git
$ git version
git version 1.7.1
Gitのリポジトリルートを作成
$ sudo mkdir /var/lib/git
複数人でGitを使う事を想定しているため、Git管理ツール「Gitolite」を導入
Git管理用ユーザの作成
$ sudo useradd gituser
$ sudo passwd gituser
ローカルのMac上で秘密鍵を作る
まずは公開鍵方式で作成した鍵の名前を変更する(鍵が上書きされてしまうため)。
ローカルのMac上でホームに移動する。
[local] $ cd
[local] $ cd .ssh
[local] $ ls -lt
名前の変更
[local] $ mv id_rsa login_id_rsa
[local] $ mv id_rsa.pub login_id_rsa.pub
※Windowsの人はこちらを参照(TeraTermで作るのが簡単)
SSH公開鍵暗号化方式-Tera Termで秘密鍵・公開鍵の作成 | Tera Term(テラターム)の便利な使い方
[local] $ ssh-keygen
パスフレーズは設定せずにEnterを押して進める
わかりやすいように名前変更
[local] $ mv id_rsa gitolite_id_rsa
[local] $ mv id_rsa.pub gitolite_id_rsa.pub
ローカルマシンの.ssh配下にconfigという名前でファイルを作成
[local] $ vi config
以下の内容を書き込む
HOST gitolite.example.com
HOSTNAME example.com
USER gituser
PORT zzzzz
IDENTITYFILE /Users/test/.ssh/gitolite_id_rsa
HOST:識別子、識別できれば何でも良い
HOSTNAME:サーバのIPアドレス or ドメイン名
USER:Git管理用ユーザ
PORT:zzzzz(SSH接続する際のポート番号)
IDENTITYFILE:先ほど作った秘密鍵
先ほど作った公開鍵をサーバーにアップする
[local] $ scp -P zzzzz -i /Users/test/.ssh/login ./gitolite.pub yyyy@xxx.xxx.xxx.xxx:
サーバ上にアップした公開鍵のパーミッション変更
$ cd
$ mv gitlite.pub /home/gituser
$ sudo chown gituser:gituser /home/gituser/gitolite.pub
$ sudo chmod 644 /home/gituser/gitolite.pub
$ sudo ls -lt /home/gituser/gitolite.pub
gituserでGitoliteをインストールする
$ sudo yum install perl-Time-HiRes -y
$ su gituser
$ cd
$ mkdir ~/tmp;cd ~/tmp
$ git clone git://github.com/sitaramc/gitolite
$ mkdir -p ~/bin
$ gitolite/install -to ~/bin
$ ~/bin/gitolite setup -pk ~/gitolite.pub
公開鍵は消しておく
$ rm -rf ~/gitolite.pub
ここまででリポジトリは作成された。以下のコマンドで確認できる。
$ ls -ltra ~/repositories/
drwx------ 7 gituser gituser 4096 4月 14 16:53 2015 testing.git
drwx------ 8 gituser gituser 4096 4月 14 16:53 2015 gitolite-admin.git
gituserをログアウトする
$ exit
動作確認でローカルにリポジトリを落とす
gitolite.example.comの部分に~/.ssh/configに記載したHOST部分を入力
[local] $ git clone gitolite.example.com:testing
ユーザの追加
管理ユーザーの時と同様にローカルで鍵を作る。
鍵の名前をgitolite-exampleとgitolite-example.pubする。exampleの部分がユーザ名となる。
わかりやすい場所にGitolite管理用リポジトリをクローンして、keydirフォルダに公開鍵(gitolite-example.pub)を格納する。"gitolite-"の部分は削っても良い。つまり、名前は特に問わない。以下ではexample.pubとして格納している。
[local] $ git clone gitolite.example.com:gitolite-admin
[local] $ cd gitolite-admin/keydir/
[local] $ mv /Users/test/.ssh/gitolite-example.pub ./example.pub
[local] $ git add .
[local] $ git add -m 'add new user'
[local] $ git push
git pushでサーバに鍵を格納する。
管理ファイルにユーザ追加(赤文字は先ほど記述した管理者用の設定)
[local] $ vi /Users/test/.ssh/config
HOST gitolite.example.com
HOSTNAME example.com
USER gituser
PORT zzzzz
IDENTITYFILE /Users/test/.ssh/gitolite_id_rsa
HOST gitolite-example.example.com
HOSTNAME example.com
USER gituser
PORT zzzzz
IDENTITYFILE /Users/test/.ssh/gitolite-example
HOSTとIDENTITYFILEのみ書き換える。
適当なディレクトリで動作確認をする
[local] $ git clone gitolite-example.example.com:testing
レポジトリの追加(赤い部分はデフォルト設定)
[local] $ vi gitolite-admin/conf/gitolite-conf
repo gitolite-admin
RW+ = gitlite
repo testing
RW+ = @all
repo new-repo
R = @all
RW+ = example
[local] $ cd gitolite-admin/conf
[local] $ git add .
[local] $ git commit -m 'add new-repo'
[local] $ git push
1からLinuxに本番環境の設定をする CentOS
1からLinuxに本番環境の設定をする CentOS(2)セキュリティ
1からLinuxに本番環境の設定をする CentOS(3)iptable logwatch
1からLinuxに本番環境の設定をする CentOS(4)Apache2.2を2.4にアップデートする
1からLinuxに本番環境の設定をする CentOS(5)Gitのインストールと設定
1からLinuxに本番環境の設定をする CentOS(6)gitoliteでリポジトリ運用開始
1からLinuxに本番環境の設定をする CentOS(7)tomcat7 インストール
1からLinuxに本番環境の設定をする CentOS(8)tomcat8 インストール
参考にしたページ
1からLinuxに本番環境の設定をする CentOS(4)Apache2.2を2.4にアップデートする。
Apache2.2のサービスを停止してApache2.4のサービスを起動する
まずApache2.2がプレインストールされていたのでサービスを停止する
$ chkconfig httpd off
$ chkconfig --list | grep httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
Apache2.4が入ってるレポジトリをダウンロード
$ cd /etc/yum.repos.d/
$ wget http://repos.fedorapeople.org/repos/jkaluza/httpd24/epel-httpd24.repo
sudo: wget: コマンドが見つかりません
wgetをインストール
$ yum install wget
再度ダウンロード
$ wget http://repos.fedorapeople.org/repos/jkaluza/httpd24/epel-httpd24.repo
これでyumを使ってインストールできる
$ sudo yum install httpd24.x86_64
インストールできたか確認
$ /opt/rh/httpd24/root/usr/sbin/httpd -version
Server version: Apache/2.4.6 (Red Hat)
Server built: Sep 25 2013 05:25:46
ちなみに、configファイルの位置(格納場所)
/opt/rh/httpd24/root/etc/httpd
httpd2.4のサービス起動
$ sudo chkconfig httpd24-httpd on $ chkconfig --list | grep httpd
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
httpd24-httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
どうやら、php-fpmというのも入れたほうがいいらしい
http://wiki.apache.org/httpd/PHP-FPM
とりあえず、入れてみる
$ yum install php-fpm
$ sudo /etc/init.d/php-fpm start
php-fpmを起動中: [ OK ]
$ chkconfig php-fpm --list
php-fpm 0:off 1:off 2:off 3:off 4:off 5:off 6:off
$ chkconfig php-fpm on
$ chkconfig php-fpm --list
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
設定ファイルの変更
まずはバックアップ
$ cd
$ mkdir backup
$ cp -p /opt/rh/httpd24/root/etc/httpd/conf/httpd.conf ./backup/httpd.conf
設定変更
$ sudo vi /opt/rh/httpd24/root/etc/httpd/conf/httpd.conf
変更1:連絡先メールアドレス、公開できる連絡用メールアドレスを設定
# ServerAdmin: Your address, where problems with the server should be
# e-mailed. This address appears on some server-generated pages, such
# as error documents. e.g. admin@your-domain.com
#
ServerAdmin root@localhost
変更2:Options indexesを無効化、indexesの前に-(ハイフン)を付ける、FollowSymLinksの前には+(プラス)を付ける
<Directory "/opt/rh/httpd24/root/var/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options -Indexes +FollowSymLinks
Apacheが起動できるかテストする
$ sudo service httpd24-httpd restart
Stopping httpd: [FAILED]
Starting httpd: [ OK ]
テストページが表示されている事をブラウザから確認する。
http://の後にサーバのIPアドレスもしくはドメインを入力してアクセスする。
Red Hat Enterprise Linux Test Page
テストページが表示されていればOK
1からLinuxに本番環境の設定をする CentOS
1からLinuxに本番環境の設定をする CentOS(2)セキュリティ
1からLinuxに本番環境の設定をする CentOS(3)iptable logwatch
1からLinuxに本番環境の設定をする CentOS(4)Apache2.2を2.4にアップデートする
1からLinuxに本番環境の設定をする CentOS(5)Gitのインストールと設定
1からLinuxに本番環境の設定をする CentOS(6)gitoliteでリポジトリ運用開始
1からLinuxに本番環境の設定をする CentOS(7)tomcat7 インストール
1からLinuxに本番環境の設定をする CentOS(8)tomcat8 インストール
参考にしたブログ
CentOS - install using yum Apache 2.4 - Unix & Linux Stack Exchange
1からLinuxに本番環境の設定をする CentOS(3)iptable logwatch
ここまでで、サーバへの接続は以下のコマンドによるものとなっているはずである。
$ ssh yyy@xxx.xxx.xxx.xxx -i /Users/spacenet/.ssh/id_rsa -p zzzzz
yyyはサーバ上のユーザ名 xxx.xxx.xxx.xxxはサーバのIPアドレス zzzzzはssh接続用ポート番号
iptablesがインストールされている事を確認
$ yum list installed | grep iptable
iptables.x86_64 1.4.7-14.el6 @base
iptables-ipv6.x86_64 1.4.7-14.el6 @base
target prot opt source destination
現在の設定を確認する
$ sudo iptables -L
Chain INPUT (policy ACCEPT)
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
$ sudo vi /etc/sysconfig/iptables
以下の内容に置き換え
重要:zzzzzの部分をssh用のポート番号に変換するのを忘れずに
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# SSH, HTTP
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport zzzzz -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT
iptablesを再起動して設定を反映させる
$ sudo service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules:
再度設定の確認をする
sudo iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain RH-Firewall-1-INPUT (2 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp any
ACCEPT esp -- anywhere anywhere
ACCEPT ah -- anywhere anywhere
ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns
ACCEPT udp -- anywhere anywhere udp dpt:ipp
ACCEPT tcp -- anywhere anywhere tcp dpt:ipp
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:zzzzz
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
不正アクセスがあった場合に感知できるようアクセス監視ツールを入れる
$ sudo yum install logwatch
試しにメールを送ってみる。
$ sudo logwatch --mailto foo@example.com
メールが送られてこない・・・・
メールログを確認
$ sudo tail -f /var/log/maillog
メールの設定を確認
$ sudo alternatives --config mta
There is 1 program that provides 'mta'.
Selection Command
-----------------------------------------------
*+ 1 /usr/sbin/sendmail.postfix
Enter to keep the current selection[+], or type selection number:
Postfixが設定されている。
$ service postfix status
master is stopped
どうやらPostfixが止まっている。
OS起動時に起動するように設定。
$ sudo chkconfig postfix on
忘れずにサービス起動
$ sudo service postfix start
Starting postfix: [ OK ]
再度メールのテスト送信
$ sudo logwatch --mailto foo@example.com
OK、無事メールが送られてきた。
ただし、spamフォルダに入っているので、通常フォルダに入るようにする。
ちなみに、メールのフィルタを設定するか、連絡先に送信元を追加するとspamと認識されなくなる場合が多い(メールソフトによるが)。
logwatchメール送信先設定を追加
$ sudo vi /etc/logwatch/conf/logwatch.conf
# Local configuration options go here (defaults are in /usr/share/logwatch/default.conf/logwatch.conf)
MailTo foo@example.com
つづく
参考にしたブログ
さくらのVPS を改めて使いはじめる 3 – iptables と logwatch | アカベコマイリ
CentOS5.3でWebServer(LogWatch編)
後で設定してみたいセキュリティ
1からLinuxに本番環境の設定をする CentOS
1からLinuxに本番環境の設定をする CentOS(2)セキュリティ
1からLinuxに本番環境の設定をする CentOS(3)iptable logwatch
1からLinuxに本番環境の設定をする CentOS(4)Apache2.2を2.4にアップデートする
1からLinuxに本番環境の設定をする CentOS(5)Gitのインストールと設定
1からLinuxに本番環境の設定をする CentOS(6)gitoliteでリポジトリ運用開始
1からLinuxに本番環境の設定をする CentOS(7)tomcat7 インストール
1からLinuxに本番環境の設定をする CentOS(8)tomcat8 インストール
1からLinuxに本番環境の設定をする CentOS(2)セキュリティ
root操作を禁止とし、sudoを可能にする
$ yum list installed | grep sudo
入っていなければ
$ yum install sudo
sudo を利用するユーザ(xxxx)をwheelグループに追加
$ usermod -G wheel xxxx
追加できた事を確認する
$ id ytb
uid=500(xxxx) gid=500(xxxx) 所属グループ=500(xxx),10(wheel)
wheelグループ所属ユーザにsudo実行許可を与える
$ visudo
## Allows people in group wheel to run all commands
# %wheel ALL=(ALL) ALL
コメントアウトを外す
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
sudoを実行するユーザ(xxxx)に対してコマンドパスを通す
$ su xxxx
$ cd
$ vi .bash_profile
以下の行を追加
PATH=$PATH:$HOME/bin
PATH=$PATH:/sbin
PATH=$PATH:/usr/sbin
PATH=$PATH:/usr/local/sbin
コマンドを実行して設定を反映させる
$ source ~/.bash_profile
一度サーバからログアウトして再度ログインする。
セキュリティをさらに高めるためssh接続のポート番号を変更
$ sudo vi /etc/ssh/sshd_config
#Port 22
この行を書き換えてssh接続用ポート番号を変える(ただし、0〜65535までしか指定できない。そして、悪意のあるハッカーを困らせるように大きい数にすると良い)
Port xxxxxx
保存してsshd再起動
$ sudo service sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
1からLinuxに本番環境の設定をする CentOS
1からLinuxに本番環境の設定をする CentOS(2)セキュリティ
1からLinuxに本番環境の設定をする CentOS(3)iptable logwatch
1からLinuxに本番環境の設定をする CentOS(4)Apache2.2を2.4にアップデートする
1からLinuxに本番環境の設定をする CentOS(5)Gitのインストールと設定
1からLinuxに本番環境の設定をする CentOS(6)gitoliteでリポジトリ運用開始
1からLinuxに本番環境の設定をする CentOS(7)tomcat7 インストール
1からLinuxに本番環境の設定をする CentOS(8)tomcat8 インストール
1からLinuxに本番環境の設定をする CentOS(1)
最初にセキュリティ強化のためパスワード変更
$passwd
ユーザー root のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: 全ての認証トークンが正しく更新できました。
ログイン用ユーザの作成
$useradd test
$passwd test
ユーザー test のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: 全ての認証トークンが正しく更新できました。
ssh公開鍵認証
サーバに接続を行うMacで鍵作成
ターミナルを起動
$ssh-keygen
/Users/xxxx/.sshに鍵が作成される、秘密鍵のパーミッション変更
cd /User/xxxx/.ssh
chmod 600 id_rsa
接続するユーザ(xxxx)で公開鍵をサーバ(XXX.XXX.XXX.XXX)にアップ
ssh -P 22 id_rsa.pub xxxx@XXX.XXX.XXX.XXX:
サーバにアップした公開鍵の名前とパーミッションを変更
接続するユーザ(xxxx)でログインする
$ su xxxx
$ cd
$ mkdir .ssh
$ chmod 700 .ssh
$ mv id_rsa.pub .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys
sshの設定変更
vi /etc/ssh/sshd_config
rootでのログイン不可化
PermitRootLogin no
パスワードログイン不可化
PasswordAuthentication no
sshの設定を変更したらsshの再起動
# service sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
ここまでやると、秘密鍵を盗まれない限り、第三者がサーバへのログインは出来なくなる。
※超重要※
鍵認証うまくいかなかった場合、ログイン不可になる。(rootでログインできないため)
サーバへのrootログイン状態を保持したまま、鍵認証でログインができることを確かめる事。
yumのアップデート実施
$yum update
1からLinuxに本番環境の設定をする CentOS
1からLinuxに本番環境の設定をする CentOS(2)セキュリティ
1からLinuxに本番環境の設定をする CentOS(3)iptable logwatch
1からLinuxに本番環境の設定をする CentOS(4)Apache2.2を2.4にアップデートする
1からLinuxに本番環境の設定をする CentOS(5)Gitのインストールと設定
1からLinuxに本番環境の設定をする CentOS(6)gitoliteでリポジトリ運用開始
1からLinuxに本番環境の設定をする CentOS(7)tomcat7 インストール
1からLinuxに本番環境の設定をする CentOS(8)tomcat8 インストール
16:SpringSecurityTwitter Pluginのs2-init-twitterを実行してみた
SpringSecurityTwitter Pluginのdocページには書かれてい無いが、s2-init-twitterを実行してみた。
実行結果はこちら
$ gradle grails-s2-init-twitter
:grails-s2-init-twitter
| Loading Grails 2.4.4
| Configuring classpath
| Configuring classpath.
| Environment set to development
| Environment set to development.
| Environment set to development..
| Environment set to development...
| Environment set to development....
| Environment set to development.....
| Packaging Grails application
| Packaging Grails application.
| Packaging Grails application..
| Packaging Grails application...
| Packaging Grails application....
| Packaging Grails application.....
Creating app based on configuration:
consumerKey = CONSUMER_KEY
consumerSecret = CONSUMER_KEY
language = en_US
button = [text:Login with Twitter]
popup = false
autoCreate = [active:true, roles:[ROLE_USER, ROLE_TWITTER]]
filter = [processUrl:/j_spring_twitter_security_check, processPopupUrl:/twitterAuth/popup]
domain = [classname:TwitterUser, connectionPropertyName:user]
userClassFullName = login.User
userClassName = User
domainClassName = TwitterUser
userConnectionProperty = user
userImport = import login.User
> Enter your Twitter API Key > Enter your Twitter API Consumer Key > Enter your Twitter API Consumer Secret
BUILD SUCCESSFUL
Total time: 21.094 secs
どうやら、configファイルを書き換えてから実行するべきだったようだ・・・
そもそも、いらないファイルができるのでだめそう。
grails-app/controllers/TwitterAuthController.groovy
warを作ろうとするとエラーになる
| Compiling 5 source files
[groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
[groovyc] /Users/spacenet2/app/grails-app/controllers/TwitterAuthController.groovy: 1: unable to resolve class com.the6hours.grails.springsecurity.twitter.TwitterAuthController
[groovyc] @ line 1, column 1.
[groovyc] class TwitterAuthController extends com.the6hours.grails.springsecurity.twitter.TwitterAuthController {
[groovyc] ^
[groovyc]
| Compiling 5 source files.
| Error Compilation error: startup failed:
/Users/spacenet2/app/grails-app/controllers/TwitterAuthController.groovy: 1: unable to resolve class com.the6hours.grails.springsecurity.twitter.TwitterAuthController
@ line 1, column 1.
class TwitterAuthController extends com.the6hours.grails.springsecurity.twitter.TwitterAuthController {
^
1 error
このエラーはプラグインのクラスを参照しようとしてエラーになってるんだが、プラグインのクラスを参照可能にすればエラーではなくなる。
でも、やり方がわからない・・・
誰か教えてください!!
15:えっ?GGTSってGradleのプラグインをインストールしないといけないの!!??
GGTSをエディタとして使ってるけど、なんか赤い❌が消えないなと思ってたけど、スルーしてた。
なんかよくわからない所でコンパイルエラーになる事が頻発。
ダメっぽい?
とりあえず、eclipseにインストールしてみる、今更!