LoginSignup
1
1

【CentOS8】Apache (httpd) よく使うコマンド

Last updated at Posted at 2023-09-25

CentOS8 初期設定コマンド

ip,MACアドレスに変更があった際は以下のコマンドから変更を行う

vim /etc/sysconfig/network-scripts/ifcfg-eth0 //編集
cat /etc/sysconfig/network-scripts/ifcfg-eth0 //確認

バージョン確認

cat /etc/redhat-release

アップデート

dnf update

DNS Server Setting command

nmcli connection modify ens192 ipv4.dns 8.8.8.8 //DNS serverアドレス設定

CentOS サポート終了対策

Apache install コマンド

mount /dev/cdrom /media //マウント
dnf install --disablerepo=\* --enablerepo=InstallMedia-* -y httpd httpd-tools httpd-devel httpd-manual //インストールコマンド

Apache よく使うコマンド

apache 起動・動作確認・停止

sudo systemctl start httpd.service //起動
systemctl status httpd.service //動作確認
sudo systemctl stop httpd.service //停止

apache 再起動

service httpd restart

apache 自動起動

sudo systemctl enable httpd.service //有効
systemctl is-enabled httpd.service //設定確認
sudo systemctl disable httpd.service //無効

※何も表示されない場合、CentOSのFirewallが作動していると考えられます

Firewall 設定

Firewall

systemctl stop firewalld //停止
systemctl start firewalld //起動
firewall-cmd --reload //再起動

Firewall 自動起動

systemctl enable firewalld //有効
systemctl disable firewalld //無効

Firewall 通信許可追加

firewall-cmd --add-port=80/tcp --permanent //http
firewall-cmd --add-port=443/tcp --permanent //https

Firewall リロード

firewall-cmd --reload

Firewall確認

firewall-cmd --list-all

telnet 機能 install

sudo dnf -y install telnet
sudo dnf -y install telnet-server

telnet 自動起動

systemctl enable telnet.socket

telnet サービス起動

systemctl start telnet.socket

Chrony 設定

CentOS8からntpからchronyに変更

vim /etc/chrony.conf
    #pool 2.centos.pool.ntp.org iburst //変更前
    #pool ntp.jst.mfeed.ad.jp iburst //変更後

設定反映

chronyc sources

chrony自動起動設定

systemctl enable chronyd

EPEL, Remi リポジトリ追加

CentOS8にEPELとRemiのリポジトリを追加する (パッケージ更新のため)

dnf install epel-release //EPELリポジトリのインストール
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm //Remiリポジトリのインストール

リポジトリを無効化する

dnf repolist //リポジトリ確認
dnf config-manager --disable epel epel-modular remi-modular remi-safe //EPEL、Remiのリポジトリを無効化する

無効化したリポジトリからパッケージをインストールする方法

dnf --enablerepo=epel,remi-safe repolist //無効化リポジトリのいくつかを有効化、複数のリポジトリを指定するときはカンマで区切ります
dnf -y install mod_ssl

apache 設定

apacheユーザー認証 //Basic認証追加

mkdir -p /var/www/html/member
htpasswd -c /etc/httpd/.htpasswd secret

//usernameがsecretになる
//初めてファイルを作成する場合は-cをつける

    New password:root123
    Re-type new password:root123 //パスワードは2回入力

認証ユーザーの追加

htpasswd /etc/httpd/.htpasswd 追加するユーザ名

例 htpasswd /etc/httpd/.htpasswd secret2

httpd.confに設定追加

<Directory "/var/www/html/member">
    AuthType Basic //認証の種類
    AuthName "Secret Zone" //ダイアログにメッセージを表示する名前
    AuthUserFile /etc/httpd/.htpasswd //パスワードファイル
    Require user secret //認証するユーザ //#Require valid-userでパスワードファイルに記述された全ユーザが対象となる
</Directory>

apache再起動

service httpd restart

Cookie 機能追加

cookie機能の追加(httpd.confに以下を追加)

Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure //HttpOnly属性の有効化
ExpiresActive on //Expiresヘッダの生成を有効にする
ExpiresDefault "access plus 1 hours" //個別に指定しなければ、有効期限は1時間後になる

SSL通信設定

SSL通信を行うためのモジュール追加

/etc/httpd/modules/ //モジュールが格納されているディレクトリ

mod_so.cが組み込まれているかを確認する

cd /etc/httpd/modules/
ls -a

mod_ssl.c ファイルをコンパイルする

/etc/httpd/modules/apxs -c -i -a mod_ssl.c

httpd.confに下記を追加する。(apxsコマンドでインストールすると、自動的にhttpd.confに設定が追加されている。もし追加されていなければ追加する。)

LoadModule rewrite_module modules/mod_rewrite.so

apacheのモジュール設定

mod_info モジュールを有効にする

LoadModule isapi_module modules/mod_isapi.so //httpd.confに追記

mod_status モジュールの設定

Include conf.modules.d/httpd-info.conf //httpd.confに追記

※設定はhttpd.confファイルではなくhttpd-info.confファイルで行います。
デフォルトではhttpd.confからhttpd-info.confを読み込んでいませんので、
最初に読み込むように変更します。

./usr/share/doc/httpd/ から ./etc/httpd/conf.modules.d/ へ移動

モジュールが有効になっているか確認

httpd -M

必要に応じて

service httpd restart
systemctl reload httpd.service
service httpd configtest //congigureテスト
httpd -l //apacheに組み込まれているモジュールの確認
1
1
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
1
1