Computer System/Raspberry Pi

[Raspberry PI] 라즈베리 파이를 웹 서버로 활용 1. 아파치 설치 (MySQL, php)

해리팍 2016. 3. 16. 14:55
반응형

안녕하세요.


간만에 라즈베리 파이 관련 포스팅을 하는 것 같습니다.


이번 시간에는 라즈베리 파이에 아파치를 설치하여 라즈베리 파이를 웹 서버로 활용하는 방법에 대해 알아보겠습니다.


* 본 내용은 라즈베리 파이 2, Ubuntu Mate에서 진행되었습니다.



1. 아파치 설치


다음의 명령어를 이용하여 아파치를 설치합니다.



$ sudo apt-get install apache2



아파치 설치가 제대로 되었는지 확인해 보려면,


라즈베리 파이의 웹 브라우저에서 http://localhost 로 이동해 보시면 됩니다.






'It works!' 라는 문구를 확인하셨으면 제대로 설치가 됐다고 보시면 됩니다.



2. MySQL 설치


추 후에 DB를 사용하기 위해 아파치와 함께 사용할 MySQL을 미리 설치해둡니다.


다음의 명령어를 이용해 설치하시면 됩니다.



$ sudo apt-get install mysql-server mysql-clientsudo apt-get install mysql-server mysql-client



3. php 설치


요즘은 웹 페이지를 만들 때 html 보다 활용도가 높은 php를 많이 사용합니다.


php를 함께 설치합니다.


다음의 명령어를 이용해 php를 설치합니다.



$ sudo apt-get install php5 php5-common libapache2-mod-php5



4. 아파치 설정


(1) 웹 서버의 홈 디렉토리 생성


먼저 웹 서버의 홈 디렉토리를 생성해 주어야 합니다.


저는 홈 디렉토리 ~/ (/home/parkch0708)에 'www'란 디렉토리를 웹 서버의 홈 디렉토리로 설정 하려고 합니다.


다음의 명령어로 해당 디렉토리를 생성해 주고, 권한을 바꿔줍니다.



$ mkdir ~/www

$ chmod 755 ~/www



(2) 아파치 기본 설정 변경


데비안 계열에 설치한 아파치의 기본 설정 파일의 경로는 다음과 같습니다.


/etc/apache2/apache2.conf


해당 파일을 루트 권한으로 열어 내용 수정을 해줍니다.



$ sudo vi /etc/apache2/apache2.conf



파일의 중간 부분을 보면 디렉토리와 관련된 내용이 있습니다.

해당 내용을 다음과 같이 수정합니다.


# Sets the default security model of the Apache2 HTTPD server. It does

# not allow access to the root filesystem outside of /usr/share and /var/www.

# The former is used by web applications packaged in Debian,

# the latter may be used for local directories served by the web server. If

# your system is serving content from a sub-directory in /srv you must allow

# access here, or in any related virtual host.

<Directory />

        Options FollowSymLinks

        AllowOverride None

</Directory>


<Directory /usr/share>

        AllowOverride None

        Require all granted

</Directory>


<Directory /home/parkch0708/www>

        Options Indexes FollowSymLinks MultiViews

        AllowOverride None

        Order deny,allow

        allow from all

</Directory>


#<Directory /srv/>

#       Options Indexes FollowSymLinks

#       AllowOverride None

#       Require all granted

#</Directory>



굵게 표시한 부분이 수정해주어야 하는 부분입니다.


초기 웹 서버의 홈 디렉토리 설정은 '/var/www'로 되어있습니다.


저는 웹 서버의 홈 디렉토리가 'home/parkch0708/www' 이므로 해당 경로로 디렉토리를 수정해주었습니다.



/etc/apache2/sites-enabled/000-default.conf 파일도 마찬가지로 루트 권한으로 열어 수정해줍니다.



sudo vi /etc/apache2/sites-enabled/000-default.conf



<VirtualHost *:80>

        # The ServerName directive sets the request scheme, hostname and port that

        # the server uses to identify itself. This is used when creating

        # redirection URLs. In the context of virtual hosts, the ServerName

        # specifies what hostname must appear in the request's Host: header to

        # match this virtual host. For the default virtual host (this file) this

        # value is not decisive as it is used as a last resort host regardless.

        # However, you must set it for any further virtual host explicitly.

        #ServerName www.example.com


        ServerAdmin webmaster@localhost

        DocumentRoot /home/parkch0708/www


        <Directory />

                Options FollowSymLinks

                AllowOverride None

        </Directory>

        <Directory /home/parkch0708/www>

                Options Indexes FollowSymLinks MultiViews

                AllowOverride None

                Order deny,allow

                allow from all

        </Directory>


        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,

        # error, crit, alert, emerg.

        # It is also possible to configure the loglevel for particular

        # modules, e.g.

        #LogLevel info ssl:warn



굵게 표시한 부분을 수정해 주셔야 합니다.



(3) 아파치 재시작


변경된 설정을 적용 하기 위해 아파치 서비스를 재시작 합니다.



$ sudo /etc/init.d/apache2 restart



(4) 테스트용 html 파일 만들기


웹 서버가 정상적으로 작동 하는지 확인을 하기 위해 테스트용 html 파일을 만들어 줍니다.


아파치_웹_서버_디렉토리에 index.html 파일을 생성해 줍니다.



$ vi ~/www/index.html



<html>

<head>

<title>R-Pi Apache Test</title>

</head>


<body>


Hello World!


</body>


</html>



파일을 저장하고, 권한 설정을 해줍니다.



$ chmod 777 ~/www/index.html



(5) 동작 확인


라즈베리파이의 웹브라우저에서 'http://localhost' 를 입력해보셔도 되고,


라즈베리파이와 같은 네트워크에 있는 다른 컴퓨터의 웹 브라우져에서 'http://라즈베리파이의_IP' 를 입력해 주셔도 됩니다.


웹 브라우저에서 테스트 용으로 만든 html 파일을 정상적으로 읽어올 수 있으면 성공 한겁니다.






라즈베리 파이를 웹 서버로 활용하기 위한 아파치 설치가 끝났습니다.


다음 포스팅 부터는 아파치와 함께 사용할 수 있는 것들 (SSL, MySQL 등)에 대해 다뤄보도록 하겠습니다.

반응형