Amazon Web Services

부록 : php 실습환경 구축

본 수업에서는 Apache, PHP, MySQL이 동작하는 환경을 구축하는 방법을 알아봅니다.

최신상태로 업데이트를 합니다. 

sudo apt-get update;

apache를 설치합니다. 

sudo apt-get install apache2

php를 설치합니다. 

sudo apt-get install php5
만약 RDS나 DynamoDB와 같은 서비스형 데이터베이스를 사용하실 예정이면  아래 과정은 하지 않으셔도 됩니다. 

MySQL 데이터베이스 서버를 설치합니다. 

sudo apt-get install mysql-server

MySQL 데이터베이스 클라이언트를 설치합니다. 

sudo apt-get install mysql-client

데이터베이스에 정보를 입력합니다. 

데이터베이스 생성

CREATE DATABASE o2 CHARACTER SET utf8 COLLATE utf8_general_ci;

데이터베이스 선택

use o2;

테이블 생성

CREATE TABLE `topic` (
`id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `description` text NOT NULL,
  `author` varchar(30) NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

생성된 테이블 확인

show tables;

데이터 삽입

INSERT INTO `topic` (`id`, `title`, `description`, `author`, `created`) VALUES(1, 'About JavaScript', '<h3>Desctiption</h3>\r\n<p>JavaScript  is a dynamic computer programming language. It is most commonly used as part of web browsers, whose implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed.</p>\r\n<p>\r\nDespite some naming, syntactic, and standard library similarities, JavaScript and Java are otherwise unrelated and have very different semantics. The syntax of JavaScript is actually derived from C, while the semantics and design are influenced by the Self and Scheme programming languages.\r\n</p>\r\n<h3>See Also</h3>\r\n<ul>\r\n  <li><a href="http://en.wikipedia.org/wiki/Dynamic_HTML">Dynamic HTML and Ajax (programming)</a></li>\r\n  <li><a href="http://en.wikipedia.org/wiki/Web_interoperability">Web interoperability</a></li>\r\n  <li><a href="http://en.wikipedia.org/wiki/Web_accessibility">Web accessibility</a></li>\r\n</ul>\r\n', 'egoing', '2015-03-31 12:14:00');
INSERT INTO `topic` (`id`, `title`, `description`, `author`, `created`) VALUES(2, 'Variable and Constant', '<h3>Desciption</h3>\r\n\r\nIn computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity or information referred to as a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer source code can be bound to a value during run time, and the value of the variable may thus change during the course of program execution.\r\n\r\n<h3>See Also</h3>\r\n<ul>\r\n<li>Non-local variable</li>\r\n<li>Variable interpolation</li>\r\n</ul>\r\n', 'k8805', '2015-05-14 10:04:00');
INSERT INTO `topic` (`id`, `title`, `description`, `author`, `created`) VALUES(3, 'Opeartor', '<h2>Operator</h2>\r\n<h3>Description</h3>\r\n<p>Programming languages typically support a set of operators: constructs which behave generally like functions, but which differ syntactically or semantically from usual functions</p>\r\n<p>Common simple examples include arithmetic (addition with +, comparison with >) and logical operations (such as AND or &&). </p>\r\n', 'egoing', '2015-06-18 05:00:00');
INSERT INTO `topic` (`id`, `title`, `description`, `author`, `created`) VALUES(4, 'Conditional', '<h3>Description</h3>\r\n<p>In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition.</p>\r\n<p>In imperative programming languages, the term "conditional statement" is usually used, whereas in functional programming, the terms "conditional expression" or "conditional construct" are preferred, because these terms all have distinct meanings.</p>\r\n<h3>See Also</h3>\r\n<ul>\r\n<li><a href="http://en.wikipedia.org/wiki/Branch_(computer_science)" title="Branch (computer science)">Branch (computer science)</a></li>\r\n<li><a href="http://en.wikipedia.org/wiki/Conditional_compilation" title="Conditional compilation">Conditional compilation</a></li>\r\n<li><a href="http://en.wikipedia.org/wiki/Dynamic_dispatch" title="Dynamic dispatch">Dynamic dispatch</a> for another way to make execution choices</li>\r\n<li><a href="http://en.wikipedia.org/wiki/McCarthy_Formalism" title="McCarthy Formalism">McCarthy Formalism</a> for history and historical references</li>\r\n<li><a href="http://en.wikipedia.org/wiki/Named_condition" title="Named condition" class="mw-redirect">Named condition</a></li>\r\n<li><a href="http://en.wikipedia.org/wiki/Test_(Unix)" title="Test (Unix)">Test (Unix)</a></li>\r\n<li><a href="http://en.wikipedia.org/wiki/Yoda_conditions" title="Yoda conditions">Yoda conditions</a></li>\r\n</ul>', 'c2', '2015-07-25 00:00:00');

php와 mysql 연동

sudo apt-get install php5-mysql;
sudo service apache2 restart;

샘플 애플리케이션

<?php
$conn = mysqli_connect('localhost', 'root', '', 'o2');
$result = mysqli_query($conn, 'SELECT * FROM topic WHERE id=1');
$row = mysqli_fetch_assoc($result);
var_dump($row['title']);
?>

 

댓글

댓글 본문
  1. 당당
    2023.06.15
  2. labis98
    20220211 항상 좋은 강의 감사합니다.
  3. 의기천추
    2022.01.28 AWS Ubuntu 20.04 LTS버젼 설치는 위에 대로 하면 됩니다.
    아파치 + PHP는 apt install apache2 php로 한방에 설치할수 있음
    mysql root 패스워드 설정이 다른데..
    mysql -u root 로 패스워드 없이 들어간뒤
    mysql> use mysql;
    mysql> alter user 'root'@'localhost' identified with mysql_native_password by '원하는 패스워드';
    mysql> flush privileges;
    mysql> exit;
    이제 mysq -u root -p 로 패스워드 치고 들어갈수 있음

    $conn = mysqli_connect('localhost', 'root', '패스워드', 'o2');
    결국 NULL 이 나오는 이유는 mysql connect 실패였음..
  4. 김노아
    자꾸 NULL이 나오네요.. ㅠㅠ
    어떻게해야할까요..
  5. Woori
    PHP 7.0.33-0 ubuntu0.16.04.7
    환경에 PHP7.x 에 설치하신분들중
    php.ini 파일을
    short_open_tag = On
    으로 바꾸었는데도 안보이시는 분들은
    sudo apt install php libapache2-mod-php
    설치해주시면 작동합니다.
  6. Leonard Choo
    코드 내용이 강의 영상하고 맞지 않습니다!
    강의 영상에서는 "o2" 라는 DB를 만들고 선택하는 데 여기 코드에서는 opentutorials를 선택하네요.
    공부하시는 분들 주의하시기 바랍니다.
  7. 공부다시해보자
    php7을 설치해서 뭔가 안맞는것인지 페이지에서 index.php내 명령어들을 그대로 보여주기만 하는군요
  8. Thomas
    index.php 실행하면 NULL 은 자꾸 왜 나올까요..(14.04 LTS 버전 & php5)
    몇번 반복했는데 안됩니다..

    --> db 접속 패스워드가 누락되서 그랬내요 ㅋㅋ
  9. 유 진영
    감사합니다
  10. 다비도프
    ec2 운영체제 선택시 ubuntu 최신버전이 아닌 Ubuntu Server 14.04 LTS 버전을 설치하시면 php5설치하셔서 사용하실 수 있습니다.
  11. phphtmldb
    sudo apt-get install php로 하시면 됩니다. EC2에서는 php5는 더이상 지원하지 않는것 같네요
    대화보기
    • sihan
      sudo apt-get install php7.0
      대화보기
      • Jane
        php 설치 하면
        sudo apt-get install php5
        Reading package lists... Done
        Building dependency tree
        Reading state information... Done
        Package php5 is not available, but is referred to by another package.
        This may mean that the package is missing, has been obsoleted, or
        is only available from another source

        E: Package 'php5' has no installation candidate
        이렇게 뜨고 설치가 안되네요 어떻게 해야 되죠?
      • 정연출
        버그 리포트 : 댓글 삭제가 안 됩니다.
      버전 관리
      egoing
      현재 버전
      선택 버전
      graphittie 자세히 보기