Ubuntu16:Webサーバー設定

Apache2のインストール

Apache2 をインストールします。

root@www:~# apt-get -y install apache2

ウェルカムページを削除します。

root@www:~# rm -f /var/www/html/index.html

security.confの設定します。

root@www:~# vi /etc/apache2/conf-enabled/security.conf

25行目:変更します。

ServerTokens Prod

dir.confの設定します。

root@www:~# vi /etc/apache2/mods-enabled/dir.conf

2行目:ディレクトリ名のみでアクセスできるファイル名を追記します。

DirectoryIndex index.html index.cgi index.pl index.php index.rb index.py index.xhtml index.htm

apache2.confの設定します。

root@www:~# vi /etc/apache2/apache2.conf

70行目:サーバー名追記します。

ServerName ubuntuserver.jp

000-default.confの設定します。

root@www:~# vi /etc/apache2/sites-enabled/000-default.conf

11行目:管理者アドレス変更します。

ServerAdmin webmaster@ubuntuserver.jp

ドキュメントルート所有者を編集ユーザー(webmaster)に変更します。

root@www:~# chown webmaster. /var/www/html/

ドキュメントルート所有者を確認します。

root@www:~# ll /var/www/
合計 0
drwxr-xr-x 2 root root 6 4月 24 22:46 cgi-bin
drwxr-xr-x 2 webmaster webmaster 6 4月 24 22:46 html

URLのwwwを設定します。

root@www:~# vi /etc/apache2/sites-available/virtualhost-00-ubuntuserver.jp.conf

wwwなしに統一します。

<VirtualHost *:80>
    ServerName ubuntuserver.jp
    DocumentRoot /var/www/html
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.ubuntuserver\.jp
    RewriteRule ^(.*)$ http://ubntuserver.jp/$1 [R=301,L]
</VirtualHost>

再起動します。

root@www:~# systemctl restart apache2

HTMLテストページを作成します。

root@www:~# vi /var/www/html/index.html
<html>
<body>
<div style="width: 100%; font-size: 20px; font-weight: bold; text-align: center;">
Test Page
</div>
</body>
</html>

Webブラウザを起動し、「http://ubuntuserver.jp」にアクセスし、動作確認をします。

Webブラウザを起動

TCP80番ポート開放

ルーターの設定で、TCP80番ポートを開放します。(設定方法はご自身の環境に合わせて調べてください。

ポート開放テスト

こちらのサイトで「ホスト名(ubuntuserver.jp)」、「ポート番号(80)」の開放確認をします。

Perlのインストール

Perl をインストールします。

root@www:~# apt-get -y install perl

CGI モジュールを有効にします。

root@www:~# a2enmod cgi

apache2を再起動します。

root@www:~# systemctl restart apache2

「html」ディレクトリで CGI の実行を許可する設定します。

root@www:~# vi /etc/apache2/conf-available/html.conf

拡張子 cgi、pl、rb、py を CGI として設定します。

<Directory "/var/www/html">
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl .rb .py
</Directory>

拡張子の設定を有効にします。

root@www:~# a2enconf html

「/usr/local/bin/perl」で、Perlコマンドへアクセスできるようにします。

root@www:~# ln -s /usr/bin/perl /usr/local/bin/perl

Perlのパスを確認します。

root@www:~# whereis perl
perl: /usr/bin/perl /usr/local/bin/perl /usr/share/man/man1/perl.1.gz

再起動します。

root@www:~# systemctl restart apache2

CGIテストページを作成します。

root@www:~# vi /var/www/html/index.cgi
#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<html>\n<body>\n";
print "<div style=\"width: 100%; font-size: 20px; font-weight: bold; text-align: center;\">\n";
print "CGI Test Page";
print "\n</div>\n";
print "</body>\n</html>\n";

パーミッションを設定します。

root@www:~# chmod 705 /var/www/html/index.cgi

Webブラウザを起動し、「http://ubuntuserver.jp/index.cgi」にアクセスし、動作確認をします。

Webブラウザを起動

PHPのインストール

PHP をインストールします。

root@www:~# apt-get -y install php php-cgi libapache2-mod-php php-common php-pear php-mbstring php-mysql

PHP を設定します。

root@www:~# a2enconf php7.0-cgi

php.iniを設定します。

root@www:~# vi /etc/php/7.0/apache2/php.ini

924行目:行頭の「;」を削除しコメント解除し、自身のタイムゾーンを追記します。

date.timezone = "Asia/Tokyo"

再起動します。

root@www:~# systemctl restart apache2

PHPテストページを作成します。

root@www:~# vi /var/www/html/index.php
<html>
<body>
<div style="width: 100%; font-size: 20px; font-weight: bold; text-align: center;">
<?php
print "PHP Test Page";
?>
</div>
</body>
</html>

Webブラウザを起動し、「http://ubuntuserver.jp/index.php」にアクセスし、動作確認をします。

Webブラウザを起動

Rubyのインストール

Ruby をインストールします。

root@www:~# apt-get -y install ruby

CGI モジュールを有効にします。

root@www:~# a2enmod cgi

再起動します。

root@www:~# systemctl restart apache2

Rubyテストページを作成します。

root@www:~# vi /var/www/html/index.rb
#!/usr/bin/ruby

print "Content-type: text/html\n\n";
print "<html>\n<body>\n";
print "<div style=\"width: 100%; font-size: 20px; font-weight: bold; text-align: center;\">\n";
print "Ruby Test Page";
print "\n</div>\n";
print "</body>\n</html>\n";

パーミッションを設定します。

root@www:~# chmod 705 /var/www/html/index.rb

Webブラウザを起動し、「http://ubuntuserver.jp/index.rb」にアクセスし、動作確認をします。

Webブラウザを起動

Pythonのインストール

Python をインストールします。

root@www:~# apt-get -y install python

CGI モジュールを有効にします。

root@www:~# a2enmod cgi

再起動します。

root@www:~# systemctl restart apache2

Python テストページを作成します。

root@www:~# vi /var/www/html/index.py
#!/usr/bin/env python

print "Content-type: text/html\n\n";
print "<html>\n<body>\n";
print "<div style=\"width: 100%; font-size: 20px; font-weight: bold; text-align: center;\">\n";
print "Python Test Page";
print "\n</div>\n";
print "</body>\n</html>\n";

パーミッションを設定します。

root@www:~# chmod 705 /var/www/html/index.py

Webブラウザを起動し、「http://ubuntuserver.jp/index.py」にアクセスし、動作確認をします。

Webブラウザを起動

Perlモジュールのインストール

CPANを起動します。

root@www:~# perl -MCPAN -e shell

「y」を入力してEnterキー押下します。

Would you like to configure as much as possible automatically? [yes] y

チェックを入れて「OK」をクリックするとインストールログが表示されます。

モジュール名 説明
全て
Jcode 日本語文字コードの変換
Getopt::Long コマンドライン引数のオプションを処理する
Archive::Tar tarファイルの展開と作成(v5.10以降)
Cwd カレントディレクトリのパスを取得する
File::Basename ファイルのベース名とディレクトリ名の取得
File::Copy ファイルの移動とコピー
File::Path 複数階層のディレクトリの作成と削除
File::Spec ファイル名に対する移植性のある処理
File::Temp 一時ファイルの作成
File::Find ファイルの検索
FindBin スクリプトが存在するディレクトリのパスの取得
Encode 日本語などのマルチバイト文字列を適切に処理する
utf8 ソースコード内の文字列を内部文字列に変換
Carp モジュールの呼び出し元の観点で例外を発生させる
lib モジュールの検索パスの追加
Time::Piece 日付・時刻を扱うための標準モジュール(v5.10からコアモジュール)
Time::Local localtime,gmtimeの逆返還
base クラスの継承
Data::Dumper 変数の内容を出力する
Benchmark ベンチマーク(性能比較)を行う
JSON JSONデータの解析
MIME::Base64 Base64形式へのエンコード
MIME::QuotedPrint quoted-printable形式へのエンコード
Digest::MD5 MD5値を求める
Digest::SHA 各種SHA値を求める (v5.10以降)
Storable データのシリアライズ化
Scalar::Util スカラ値に関するユーティリティ
List::Util 配列に対するさまざまな操作
Hash::Uti ハッシュのキーの制限
Sys::Hostname ホスト名の取得
Net::FTP FTPクライアント
Net::Ping リモートホストの生存確認
Exporter 関数のエクスポート
CPAN CPANからモジュールをインストールする
Pod::Usage PODドキュメントの出力
Errno システムのエラー番号をあらわす定数
POSIX POSIXで定義された関数
Math::BigInt 大きな数の計算
Math::BigFloat 大きな数の計算
Mojolicious Webフレームワーク
Mojolicious::Plugin::AutoRoute ルートを自動的に生成
MIME::Lite メールを簡単に送信
Class::Accessor::Fast アクセサの作成
Object::Simple デフォルト値つきのアクセサの作成
DDP データをく出力する
DBIx::Custom データベース簡単操作
MySQL::Diff MySQLの差分を表示
DateTime 日付の汎用的な処理
PDL 統計解析
Imager 画像編集
Text::CSV::Encoded 日本語を含んだCSVファイルを取り扱う
Text::Diff テキストの差分確認
XML::Simple シンプルなXMLパーサ
Validator::Custom HTMLフォームの検証
Data::Page ページング処理の支援
Data::Page::Navigation ページのナビゲーションの作成
Module::Starter モジュールの雛形を作成する
Devel::NYTProf 使いやすいプロファイラ
perltidy ソースコードの整形
Net::Ping::External pingコマンドの実行
IO::ScalarArray 標準入力の自動試験
IO::Capture 標準出力、標準エラー出力の自動試験
FFI::Raw ダイナミックライブラリ内の関数を呼び出す
Math::Trig さまざまな三角関数
FFI::Platypus 機能豊富なFFIモジュール
Time::Moment 日付・時刻の高速な処理
CGI CGIモジュール
CGI::Carp HTTPD(またはその他)にエラーログを書込む
DBI 各種データベースをアクセスする
strict 文法チェックを厳しくする
LWP::UserAgent Web 上のデータに アクセスする
LWP::Protocol::https LWP::UserAgentでSSLに アクセスする
Net::SSLeay LWP::UserAgentでSSLに アクセスする
Net::SSL LWP::UserAgentでSSLに アクセスする
Crypt::SSLeay LWP::UserAgentでSSLに アクセスする
Encode::Guess 文字コードを判別
Image::ExifTool Exifの情報を取得する
Archive::Tar tar 展開、作成、ファイルの追加
Image::Magick ImageMagick のコマンドラインツール
Archive::Zip zip 展開、作成、ファイルの追加

モジュールをインストールします。

Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v2.11)
Enter 'h' for help.

cpan[1]>

ImageMagikのインストール

ImageMagickをインストールします。

root@www:~# apt-get -y install imagemagick

perl ImageMagickをインストールします。

root@www:~# apt-get -y install libimage-magick-perl

関連のライブラリをインストールします。

root@www:~# apt-get -y install libmagickwand-dev libmagickcore libmagickcore-dev libmagickwand-dev
root@www:~# apt-get -y install pkg-config
root@www:~# apt-get -y install php-dev

サーバー証明書の取得

Certbotをインストールします。

root@www:~# apt -y install certbot

証明書を取得します。

ドキュメントルート:/var/www/html/

メールアドレス:webmasterubuntuserver.jp

Webサーバー名:ubuntuserver.jp

root@www:~# certbot certonly --webroot -w /var/www/html/ -m webmaster@ubuntuserver.jp  -d centserver.jp --agree-tos

証明書自動更新を設定します。

root@www:~# vi /etc/cron.d/letsencrypt

下記を追記します。

00 16 * * 2 root /usr/bin/certbot renew --post-hook "service httpd restart"

SSLの設定

SSLを設定します。

root@www:~# vi /etc/apache2/sites-available/default-ssl.conf

3行目:管理者アドレス変更します。

ServerAdmin webmaster@ubuntuserver.jp

32行目:取得した証明書を指定します。

SSLCertificateFile /etc/letsencrypt/live/ubuntuserver.jp/cert.pem

33行目:取得した鍵ファイルを指定します。

SSLCertificateKeyFile /etc/letsencrypt/live/ubuntuserver.jp/privkey.pem

SSLを有効にします。

root@www:~# a2ensite default-ssl
root@www:~# a2enmod ssl

再起動します。

root@www:~# systemctl restart apache2

Free Monitoring Test Toolsで「https://ubuntuserver.jp」の作動確認をします。

コメント

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