LoginSignup
0
1
個人開発エンジニア応援 - 個人開発の成果や知見を共有しよう!-

[AmazonLinux2023版] Webサーバ公開手順メモ

Last updated at Posted at 2023-10-04

最近AmazonLinux2023でちょっとしたWebサーバを稼働させたので、作業を残しておく。

  • 単一のEC2、m3.smallなど、とにかくSpecの小さいインスタンスで動作させる
  • 非エンジニアがWebサーバを見に行くときに困らないよう、信頼済み証明書を入手
  • ある程度安定して動くよう自サーバ内で死活監視。+αでCloudWatchEventでインスタンス自体の再起動設定もいれとく

セキュリティグループの設定

443,80を開けておく

Apache install

sudo su - 
yum update 
yum install httpd

echo "test" > /var/www/html/index.html

systemctl restart httpd

ブラウザで確認(httpで)

SSL対応

  • 自前でserver_keyを生成する場合、自己署名となってしまい、ブラウザアクセス時に「信頼できないサイト」と表示されてしまう。
  • フリーでSSL証明書を入手できる手段として、letsencryptを利用する。

mod_ssl install

yum -y install mod_ssl
httpd -M |grep ssl
> ssl_module (shared)

server_key 生成

cd ~/
openssl genrsa > server.key
openssl req -new -key server.key > server.csr
# 適当に名前やら会社名やら入れる
openssl x509 -req -signkey server.key < server.csr > server.crt

mkdir /etc/httpd/conf/ssl.key
mkdir /etc/httpd/conf/ssl.crt
mv server.key /etc/httpd/conf/ssl.key/
mv server.crt /etc/httpd/conf/ssl.crt/

vi /etc/httpd/conf.d/ssl.conf
> # SSLCertificateFile /etc/pki/tls/certs/localhost.crt
> SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt
> # SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
> SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key

systemctl restart httpd

letsencryptで信頼済み証明書を手に入れる

python3 -m venv /opt/certbot/
/opt/certbot/bin/pip install --upgrade pip
/opt/certbot/bin/pip install certbot certbot-apache
ln -s /opt/certbot/bin/certbot /usr/bin/certbot
cd /var/www/html/
certbot certonly --webroot -w /var/www/html -d hoge.com --renew-by-default --email hogehoge@hoge.com

cron設定(死活監視して自動的に再起動)

# install cron
sudo yum install cronie -y

# apacheとmysqldの監視をしておく。(mysqldは使っている場合のみ。)
## mysqldの監視
cat checkMysqld.sh
> PROCESS=`ps -ef | grep /usr/libexec/mariadbd | grep -v grep | wc -l`
> if test $PROCESS -eq 0
> then
>         LOG=`/usr/bin/systemctl restart mysqld`
> fi

## apacheの監視
cat checkHttpd.sh
> PROCESS=`ps -ef | grep /usr/sbin/httpd | grep -v grep | wc -l`
> if test $PROCESS -eq 0
> then
>         LOG=`/usr/bin/systemctl restart httpd`
> fi

chmod 755 checkMysqld.sh
chmod 755 checkHttpd.sh

# cron設定
crontab -e
> * 1 * * * /hoge/checkMysqld.sh
> * 1 * * * /hoge/checkHttpd.sh

備考

Seleniumのインストール

  • スクレイピングしたサイトのミラーを公開する場合などに利用するため、Seleniumのインストール手順もメモしておく。
#pip
wget pip3

#requirements.txt
selenium==4.10.0
pytest
flake8

pip3 install -r requirements.txt

# chrome install
vi /etc/yum.repos.d/google-chrome.repo

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=0
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

yum install --enablerepo=google-chrome google-chrome-stable

google-chrome --version
> Google Chrome 115.0.5790.110 

# chromedriver install
# https://chromedriver.chromium.org/downloads ここからインストールされたChromeに対応するバージョンをとってくる
wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/115.0.5790.110/linux64/chromedriver-linux64.zip

mv chromedriver-linux64/chromedrive /usr/local/bin/

# 実行したらエラーになった。
[235925:235925:0731/065241.048446:ERROR:zygote_host_impl_linux.cc(100)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

> オプション指定で。
    options = webdriver.ChromeOptions()
    options.add_argument("--no-sandbox")
    options.add_argument("--headless")
    driver = webdriver.Chrome(options=options)

参考

0
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
0
1