반응형

라즈베리파이에  LAMP (Linux, Apache, MySQL, PHP)를 설치하는 방법을 다루고 있습니다.


버전에 따라 설치되는 MySQL 버전 차이가 있어서 라즈비안 최신 버전을 기준으로 작성했습니다.

라즈비안 2018년 6월 27일버전으로 진행했습니다.


앞서 작성한 다음 포스팅을 참고하여 진행했습니다. 라즈비안이나 우분투나 데비안 계열이라 패키지명이 거의 동일하기 때문입니다.


Ubuntu 18.04에 LAMP ( Apache2, MySQL , PHP 7) 설치하는 방법

http://webnautes.tistory.com/1185




마지막 업데이트 2018. 9. 6




설치를 진행하기 전에 다음처럼 기존에 설치되어 있던 패키지를 업그레이드 해줍니다.


$ sudo apt update && sudo apt upgrade




1. Apache2 웹서버 설치

웹서버(Web Server)는 웹브라우저 같은 클라이언트의 요청을 받아서 웹문서나 이미지 등을 전송해주는 역활을 합니다.


1-1. Apache2 웹서버 패키지를 설치합니다.


$ sudo apt install apache2




1-2. 정상적으로 설치되었는지 확인하기 위해 PC에서 웹 브라우저를 실행하고 라즈베리파이의 아이피 주소를 입력합니다.

다음과 같은 웹페이지가 보이면 정상적으로 설치된 것입니다.





2. MySQL 서버 설치


MySQL은 관계형 데이터베이스 관리 시스템(relational database management system, RDBMS)입니다.

다중 사용자가 다수의 데이터베이스에 접근할 수 있도록 서버를 구성합니다.


2-1. MySQL 서버 패키지를 설치합니다.


$ sudo apt install mysql-server




2-2. MySQL 5.7은 설치 작업 중에 root 패스워드를 물어보지 않기 때문에 다음처럼 설정해야 합니다.  


pi@raspberrypi:~ $ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
     SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):  엔터를 누릅니다.
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y    
New password:   사용할 root 패스워드를 입력합니다.
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y     데이터베이스를 볼수만 있는 계정을 제거합니다.
... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y  원격으로 데이터베이스 서버에 접속하는 것을 막습니다.
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y   테스트용 데이터베이스를 제거합니다.
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!




2-3. 예전에 사용하던 다음 방법으로는 로그인이 되지 않습니다.

현재 설치된 mysql 패키지가 디폴트로 auth_socket 플러그인을 사용하기 때문이라네요.


pi@raspberrypi:~ $ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'




2-4. 우선 다음처럼 데이터베이스 서버에 접속합니다.


pi@raspberrypi:~ $ sudo mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>




사용할 데이터베이스를 생성합니다.


MariaDB [(none)]> create database db DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.01 sec)





2-5. 데이터베이스를 사용할 사용자를 생성합니다.


create user 사용자이름 identified by '패스워드';



MariaDB [(none)]> create user webnautes identified by '123456780';
Query OK, 0 rows affected (0.00 sec)




2-6. 앞에서 생성했던 데이터베이스 db를 새로 생성한 사용자가 사용하도록 권한을 부여합니다.  


grant all privileges on 데이터베이스이름.* to '사용자이름@localhost' identified by '패스워드'


MariaDB [(none)]> grant all privileges on db.* to 'webnautes'@'localhost' identified by '123456780';
Query OK, 0 rows affected (0.00 sec)




2-7. 잘 동작하는지 여부를 다음처럼 확인했습니다.


quit를 입력하여 데이터베이스 서버에서 로그아웃합니다.


MariaDB [(none)]> quit
Bye
pi@raspberrypi:~ $



새로 생성한 사용자(webnautes)로 로그인합니다.

pi@raspberrypi:~ $ mysql -u webnautes -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.1.23-MariaDB-9+deb9u1 Raspbian 9.0

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

앞에서 생성해두었던 데이터베이스 db가 보입니다.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| db                 |
| information_schema |
+--------------------+
2 rows in set (0.00 sec)

데이터베이스 db를 사용하도록 하고

MariaDB [(none)]>  use db;
Database changed


다음 명령을 복사해서 Person 테이블을 생성합니다.

create table Person(

    id bigint(20) unsigned not null auto_increment,

    name varchar(255) not null,

    address varchar(255) not null,

    primary key (id)

)  DEFAULT CHARACTER SET utf8;


MariaDB [db]> create table Person(
   ->     id bigint(20) unsigned not null auto_increment,
   ->     name varchar(255) not null,
   ->     address varchar(255) not null,
   ->     primary key (id)
   -> )  DEFAULT CHARACTER SET utf8;
Query OK, 0 rows affected (4.76 sec)


Person 테이블이 생성되었습니다.
MariaDB [db]> show tables;
+--------------+
| Tables_in_db |
+--------------+
| Person       |
+--------------+
1 row in set (0.00 sec)


Insert를 해봅니다.

MariaDB [db]> insert into Person(name, address) values('test', 'test');
Query OK, 1 row affected (0.07 sec)

정상적으로 입력이 되었습니다.

MariaDB [db]> select * from Person;
+----+------+---------+
| id | name | address |
+----+------+---------+
|  1 | test | test    |
+----+------+---------+
1 row in set (0.00 sec)

Person 테이블을 삭제하고 종료합니다.

MariaDB [db]> drop tables Person;
Query OK, 0 rows affected (2.34 sec)

MariaDB [db]> show tables;
Empty set (0.00 sec)

MariaDB [db]> exit
Bye
pi@raspberrypi:~ $




3. PHP 설치

대부분의 웹서버는 PHP 같은 서버 사이드 스크립트 언어(Server-side scripting)를 지원합니다.


단순한 HTML 문서에서는 할 수 없는 동적 웹페이지 생성이나 데이터베이스로부터 조회하거나 수정하는 등의 일을 할 수 있습니다. '



3-1. php 패키지를 설치합니다.


$ sudo apt install php php-mysql




3-2. PHP가 정상적으로 설치되었는지 확인하기 위해  /var/www/html경로에 info.php 파일을 편집기로 열어서


$ sudo nano /var/www/html/info.php



다음 내용을 입력합니다.  Ctrl + O를 누른 후, 엔터를 누르면 저장이 됩니다.

Ctrl + X를 눌러서 빠져나옵니다.


<?php phpinfo(); ?>




3-3. 웹 브라우저를 실행하고  라즈베리파이_주소/info.php를 입력합니다.

문제 없으면 아래 캡쳐화면처럼 설치된 PHP 정보를 보여줍니다.





4. phpMyAdmin 설치(옵션)

phpMyAdmin은 MySQL 데이터베이스를 관리할 수 있는 웹 인터페이스 입니다.



4-1. phpmyadmin 패키지를 설치합니다.


$ sudo apt install phpmyadmin




4-2. phpMyAdmin을 실행하기 위한 설정을 자동으로 할 웹서버를 선택합니다.

스페이스바를 눌러서  apache2를 선택하고 엔터를 누르면 설치가 계속 진행됩니다.





4-3. phpMyAdmin을 위한 설정을 데이터베이스에 적용하기 위해 Yes를 선택하고 엔터를 누릅니다.





4-4. MySQL에 등록할 phpMyAdmin을 위한 암호를 입력해줍니다. 엔터를 눌러서 랜덤으로 생성되게 해도 무방합니다.




4-5. 이제 웹 브라우저에서 라즈베리파이_주소/phpmyadmin로 접속합니다.

MySQL 계정 정보를 입력하고 실행을 클릭합니다.





4-6. 다음과 같은 화면을 볼 수 있습니다.

데이터베이스 및 테이블 생성, SQL 질의등 다양한 작업을 웹기반으로 할 수 있습니다.





4-7. 사용할 수 있도록 권한 부여 받은 데이터베이스 db에 대한 작업외에는 다른 일을 할 수 없도록 되어 있습니다.

추가로 데이터베이스를 생성하거나 기존 데이터베이스를 삭제할 수 없습니다.





문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts