LoginSignup
0
0

More than 1 year has passed since last update.

【簡単】MacでWebサーバーを構築し、hello worldを表示する。

Last updated at Posted at 2022-11-20

対象:Macが手元にある。Terminalで基本的なコマンドが理解できているかた。

下準備①Apach起動

最近のMacにはWebサーバー(ミドルウェア)が初めから入っている場合が多い。
Terminalで以下のコマンドを押下してみる。

webサーバーの起動
sudo /usr/sbin/apachectl start

*sudoで管理者権限で実行。PCログインのPasswordを求められる。
ブラウザのURLにlocalhostと入力しEnterを押下すると、It Works!の画面が出ることを確認。
もし、上記のコマンドが使えなければApacheがMacに入っていない可能性があるため、ここで終了または、要インストール。

下準備② 設定ファイルの確認

設定ファイルの中身をcatコマンドで確認
sudo cat /etc/apache2/httpd.conf

Finder(GUI)から、
Macintosh HD>etc/apache2/ で見ても良い。
*1 apache2は2022時点。今後versionが上がる可能性がある。
*2 Finderではetcは隠しファイルである。
 (Shift+Command+.で隠しファイル表示)

設定ファイルのDocumentRootの文字列を検索。
通常は
DocumentRoot "/Library/WebServer/Documents"
と記載されており、htmlファイルをどこに入れれば反映されるのかが分かる。

htmlファイルの用意

viエディタで以下のファイルを作成

hello.html
sudo vi /Library/WebServer/Documents/hello.html

Insert-modeでhello worldと記述、保存。

または、
Finder(GUI)から、Macintosh > Library > WebServer > Documents
を開き、以下のファイルを作成。

hello.html
hello world

hello worldを確認

ブラウザのURLにlocalhost/hello.htmlと入力しEnterを押下すると、
hello worldの画面が出ることを確認。
確認ができたらApachは止めておく。

webサーバーの停止
sudo /usr/sbin/apachectl stop

以上

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