Apache2.4系のインストール方法
Apacheのインストール
CentOS系の為yumを使っていますが、Ubuntuの場合はそこをapt-getに差し替えて下さい。
yes | sudo yum install httpd; yes | sudo yum install httpd-devel; sudo systemctl restart httpd.service; /usr/sbin/httpd -v; sudo systemctl enable httpd.service;
ps -deafw|grep httpd
と打って、httpdプロセスの存在を確認。
http://$IPアドレス/
にアクセスしてWebサーバの作動を確認。
外部からアクセスできない場合は、まずはサーバーの中で
wget -S -U test http://localhost/
と打って、正常にクロールできるか確認。
それで正常にクロールできる場合には、ファイヤーウォールがブロックしているという事になるので、ファイヤーウォールの設定を確認する。
Webコンソールの方でFirewallが提供されている場合にはそちらの方の設定を確認。
そうでない場合には、サーバ上でファイヤーウォールのprocessが走っていないか確認。
systemctl status firewalld.service
動いていて、一旦止めても問題なければ
systemctl stop firewalld.service;
そしてアクセスしてみて、Webサイトが見えればO.K。
後で適切にFirewallの設定をし直して
systemctl start firewalld.service;
で実行を再開しましょう。
WordPressやLaravelといったウェブフレームワークを使う場合、.htaccessの機能を活用する必要がある為、
sudo sed -i -e 's/AllowOverride [Nn]one/AllowOverride All/g' /etc/httpd/conf/httpd.conf ;
後、httpd.conf内の
<Directory "/var/www/html">
のディレクトリが、対象ディレクトリを含むレベルに変更する必要がある。
httpd.confの編集が終わったら
sudo apachectl restart;
で変更を反映
ドメインの取得と割り当て
お名前.com、AWSなどと契約して、DNSレコード名に対してIPアドレスを割り当てる形で名前解決できるようにする。
ウェブサーバーの方でhttpd.confを触って、VirtualHostのセクションを定義しておく。
これは後で設定するSSLの為にも必要。
例
<VirtualHost *:80> ServerName gloval.jp DocumentRoot /var/www/html </VirtualHost>
無料でhttpsでアクセスできるようにする
Let’s encryptのクライアントをインストール
dnf install -y certbot;
割り当ててるドメインを-dで指定。
例
certbot-auto run --apache -d dev.sakuhindb.com;
その後、単一でドメインとかを追加するのなら
. ~/.bash_profile;/usr/local/bin/certbot-auto certonly --apache --non-interactive --agree-tos -m $EMAIL --no-self-upgrade --post-hook "/usr/sbin/apachectl graceful" -d rentalserverhikaku.com
という形で($EMAILは自分のLet’s EncryptのログインEmailで)
vi /etc/httpd/conf/httpd-le-ssl.conf
で
<VirtualHost *:443> ServerName $DOMANNAME DocumentRoot $DOCUMENTROOT Include /etc/letsencrypt/options-ssl-apache.conf SSLCertificateFile /etc/letsencrypt/live/$DOMANNAME/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/$DOMANNAME/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/$DOMANNAME/chain.pem </VirtualHost>
という形で証明書へのパスを通して($DOMANNAMEと$DOCUMENTROOTは自分の環境に合わせて調整)
apachectl restart;
で反映
後、自動的に証明書が更新されるように、cronに設定する。
crontab -e
と打って
15 * * * * /usr/local/bin/certbot-auto renew -q --no-self-upgrade --post-hook "/usr/sbin/apachectl graceful";
といった行を追加する。
vi /etc/httpd/conf/httpd.conf
<VirtualHost *:80> ServerName blog.minakoe.jp RewriteEngine on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost> Include /etc/httpd/conf/httpd-le-ssl.conf
SELinuxの無効化
WebサーバーからDBへのアクセスだけ許可するには、Webサーバー側で
setsebool -P httpd_can_network_connect_db 1
色々面倒なのでSELinuxの機能自体を無効化するには
setenforce 0;
vi /etc/selinux/config
して
SELINUX=disabled
Firewallの設定
firewall-cmd --list-all --permanent; firewall-cmd --zone=public --add-service=http --permanent; firewall-cmd --zone=public --add-service=https --permanent;
開発環境等の為Basic認証をかけたい場合
sudo htpasswd -c /etc/httpd/conf/.htpasswd $UserID
でパスワードファイルを作り
<Location /> AuthType Basic AuthName "Basic Auth" AuthUserFile /etc/httpd/conf/.htpasswd Require valid-user </Location>
をhttpd.confに追加してサーバーを再起動
運用
攻撃や無駄なボットのアクセスをブロック
httpd.confの定義でブロックする場合
User-Agent部分によるブロックは
SetEnvIfNoCase User-Agent "^008$" bad_bot SetEnvIfNoCase User-Agent "360Spider" bad_bot SetEnvIfNoCase User-Agent "^aipbot" bad_bot SetEnvIfNoCase User-Agent "^Accelatech" bad_bot SetEnvIfNoCase User-Agent "Arachmo" bad_bot SetEnvIfNoCase User-Agent "^Bitacle" bad_bot SetEnvIfNoCase User-Agent "^Bittorrent$" bad_bot SetEnvIfNoCase User-Agent "^cfetch" bad_bot SetEnvIfNoCase User-Agent "^curl" bad_bot SetEnvIfNoCase User-Agent "Daumoa" bad_bot SetEnvIfNoCase User-Agent "^CyberNavi" bad_bot SetEnvIfNoCase User-Agent "^EmailSiphon" bad_bot SetEnvIfNoCase User-Agent "^EmailWolf" bad_bot SetEnvIfNoCase User-Agent "^ExtractorPro" bad_bot SetEnvIfNoCase User-Agent "^CherryPicker" bad_bot SetEnvIfNoCase User-Agent "^Gigabot" bad_bot SetEnvIfNoCase User-Agent "^Go " bad_bot SetEnvIfNoCase User-Agent "^IE" bad_bot SetEnvIfNoCase User-Agent "^Internet Explorer" bad_bot SetEnvIfNoCase User-Agent "^libwww" bad_bot SetEnvIfNoCase User-Agent "^lwp" bad_bot SetEnvIfNoCase User-Agent "^Microsoft-CryptoAPI" bad_bot SetEnvIfNoCase User-Agent "^Mozilla/4.0 \(compatible; ICS\)$" bad_bot SetEnvIfNoCase User-Agent "^NICErsPRO" bad_bot SetEnvIfNoCase User-Agent "^Teleport" bad_bot SetEnvIfNoCase User-Agent "^EmailCollector" bad_bot SetEnvIfNoCase User-Agent "^Wget" bad_bot SetEnvIfNoCase User-Agent "^Download Ninja" bad_bot SetEnvIfNoCase User-Agent "^HTMLParser" bad_bot SetEnvIfNoCase User-Agent "^Internet Ninja" bad_bot SetEnvIfNoCase User-Agent "MJ12bot" bad_bot SetEnvIfNoCase User-Agent "Maxthon" bad_bot SetEnvIfNoCase User-Agent "^Microsoft URL Control" bad_bot SetEnvIfNoCase User-Agent "^Mozz?illa[/ ]?\d\.?\d?$" bad_bot SetEnvIfNoCase User-Agent "^Mozilla/4\.0 \(compatible; ICS\)" bad_bot SetEnvIfNoCase User-Agent "Nessus" bad_bot SetEnvIfNoCase User-Agent "Nikto" bad_bot SetEnvIfNoCase User-Agent "^NexTools" bad_bot SetEnvIfNoCase User-Agent "^Web Downloader" bad_bot SetEnvIfNoCase User-Agent "^Website Explorer" bad_bot SetEnvIfNoCase User-Agent "^WeBoX" bad_bot SetEnvIfNoCase User-Agent "^WebAuto" bad_bot SetEnvIfNoCase User-Agent "^WebFetch" bad_bot SetEnvIfNoCase User-Agent "^WWW-Mechanize" bad_bot SetEnvIfNoCase User-Agent "OpenVAS" bad_bot SetEnvIfNoCase User-Agent "^Pockey-GetHTML" bad_bot SetEnvIfNoCase User-Agent "QuihooBot" bad_bot SetEnvIfNoCase User-Agent "^SakeBot" bad_bot SetEnvIfNoCase User-Agent "^siclab" bad_bot SetEnvIfNoCase User-Agent "^Sogou web" bad_bot SetEnvIfNoCase User-Agent "PEAR HTTP" bad_bot SetEnvIfNoCase User-Agent "Slurp China" bad_bot SetEnvIfNoCase User-Agent "Slurp China" bad_bot SetEnvIfNoCase User-Agent "^surfingFOLiO" bad_bot SetEnvIfNoCase User-Agent "UnisterBot" bad_bot SetEnvIfNoCase User-Agent "^veoh-" bad_bot SetEnvIfNoCase User-Agent "voyger" bad_bot SetEnvIfNoCase User-Agent " spider$" bad_bot SetEnvIfNoCase User-Agent "YodaoBot" bad_bot SetEnvIfNoCase User-Agent "SeaPort" bad_bot SetEnvIfNoCase User-Agent "^rtorrent" bad_bot SetEnvIfNoCase User-Agent SphSearch bad_bot SetEnvIfNoCase User-Agent "^Opera/9\.80 \(Windows NT 5\.1; U; en\) Presto/2\.10\.289 Version/12\.01" bad_bot SetEnvIfNoCase User-Agent "^Mozilla/5.0 \(Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6\) Gecko/20070725 Firefox/2.0.0.6" bad_bot SetEnvIfNoCase User-Agent "^Opera/9\.80 \(Windows NT 6.2; Win64; x64\) Presto/2\.12\.388 Version/12\.15" bad_bot SetEnvIfNoCase User-Agent "^Opera/9\.64\(Windows NT 5.1; U; en\) Presto/2\.1\.1" bad_bot SetEnvIfNoCase User-Agent "^Mozilla/4\.0 \(compatible; MSIE 8\.0; Windows NT 6\.1\)$" bad_bot SetEnvIfNoCase User-Agent "proxy" bad_bot SetEnvIfNoCase User-Agent "^\s*\(" bad_bot <Directory /> Deny from env=bad_bot ... </Directory>
IPアドレスによるブロックは
Deny from 18.207.194.237/32
とセット。
尚、AWSのクラウド環境からのクロールをブロックしたい場合には
https://github.com/hikarine3/block-awsip/blob/main/awsip_with_deny.txt
をご活用下さい。
httpd.confに反映した後は
sudo apachectl graceful
で反映
firewall-cmdでブロックする場合
firewallを使っている場合
firewall-cmd --zone=drop --permanent --add-source=[ブロックしたいIPアドレス]/32; firewall-cmd --reload;
でIPからのアクセスをブロック