Linux(CentOS)のApache HTTP Serverを自動起動させる

CentOS Linux
この記事は約6分で読めます。

※当ブログではアフィリエイト広告を利用しています。

結構以前ですが、CentOSへソースコードからApache HTTP Serverをインストールしました。

記事はこちら⇒LAMP構築-Linux(CentOS)へApacheをソースコードからインストール

Apache HTTP Serverを起動・停止させるためには、以下のコマンドで行うことが出来ますが、この長ったらしいコマンドを間単にし、また、自動起動させる設定方法です。

#Apache HTTP Serverの起動
$ /usr/local/apache2/bin/apachectl start

#Apache HTTP Serverの停止
$ /usr/local/apache2/bin/apachectl stop

なお、今回のバージョンは以下です。

  • CentOS-6.4-i386
  • Apache HTTP Server 2.4.6

起動スクリプトの作成

ダウンロードしたApache HTTP Serverのアーカイブ内に起動スクリプトがあるので、そちらをコピーする。

起動スクリプトは「httpd-2.4.6/build/rpm/httpd.init」です。

#起動スクリプトコピー
$ cp /home/[ユーザー]/httpd-2.4.6/build/rpm/httpd.init /etc/init.d/httpd

#起動スクリプトの編集
$ vi /etc/init.d/httpd

編集箇所は以下の3箇所です。

Apache HTTP Serverのプログラム本体である「httpd(HTTPデーモン)」、とPIDファイルの「httpd.pid」、CONFファイルの「httpd.conf」の3箇所。

#60行目(HTTPデーモン)
httpd=${HTTPD-/usr/sbin/httpd}
↓
httpd=${HTTPD-/usr/local/apache2/bin/httpd}

#61行目(PIDファイル)
pidfile=${PIDFILE-/var/run/${prog}.pid}
↓
pidfile=${PIDFILE-/usr/local/apache2/logs/httpd.pid}

#67行目(CONFファイル)
CONFFILE=/etc/httpd/conf/httpd.conf
↓
CONFFILE=/usr/local/apache2/conf/httpd.conf

自動起動の設定

以下のコマンドで、サービス一覧へ追加し、自動起動するように設定する。

#サービス一覧へ追加
$ chkconfig --add httpd

#サービス一覧を表示
$ chkconfig --list httpd
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off

#サービスをオンにする
$ chkconfig httpd on

#サービス一覧を表示
$ chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Apache HTTP Server(HTTPデーモン)を起動してみる。

コマンドは以下のよう。

なお、CONFファイル「httpd.conf」の構文チェックは「configtest」で行うことが出来る。

#Apache HTTP Server(HTTPデーモン)の起動
$ /etc/init.d/httpd start
httpd を起動中:                                            [  OK  ]

#Apache HTTP Server(HTTPデーモン)の状態確認
$ /etc/init.d/httpd status
httpd (pid  1630) を実行中...

#Apache HTTP Server(HTTPデーモン)の停止
$ /etc/init.d/httpd stop
httpd を停止中:                                            [  OK  ]

#CONFファイル「httpd.conf」の構文チェック
$ /etc/init.d/httpd configtest
Syntax OK

サービスを自動起動するように設定したので、CentOSのシステムを再起動してみる。

そうすると無事にApache HTTP Server(HTTPデーモン)が起動されていた。

#システムを再起動
$ reboot
$ shutdown -r now

#Apache HTTP Server(HTTPデーモン)の状態確認
$ /etc/init.d/httpd status
httpd (pid  1244) を実行中...

#Apache HTTP Server(HTTPデーモン)のプロセス確認
$ ps aux | grep httpd
root      1244  0.0  0.1   4628  1852 ?        Ss   05:31   0:00 /usr/local/apache2/bin/httpd
apache    1246  0.0  0.1 282360  1764 ?        Sl   05:31   0:00 /usr/local/apache2/bin/httpd
apache    1247  0.0  0.1 282360  1764 ?        Sl   05:31   0:00 /usr/local/apache2/bin/httpd
apache    1248  0.0  0.1 282360  1764 ?        Sl   05:31   0:00 /usr/local/apache2/bin/httpd
root      1404  0.0  0.0   5436   824 pts/0    S+   05:34   0:00 grep httpd

以上です。

Apache HTTP Serverの管理が少し楽になった。

参考サイト

apache2.2.15をCentOSにソースからインストール[Linux] | QUALL – blog

Linuxサーバ設定備忘録-Tomcatのインストール

chkconfig – サービスの自動起動の設定 / CentOSメモ – サンプルコードによるPerl入門

Linux
\よかったらシェアしてね!/
えふめん

大阪在住、30代。
業務系SE・社内SE。

PCトラブルの調査、自作デスクトップPCのこと、PC周辺機器のレビューなどの記事を書いています。

えふめんをフォローする
この記事が気に入ったら
いいね!しよう
最新情報をお届けします。
俺の開発研究所

コメント

タイトルとURLをコピーしました