IT News

Ubuntu 22.04 에서 Apache, MySQL, and PHP 설치하기

skyLove1982 2022. 5. 8. 01:47
반응형

1. 아파치(Apache) 설치

$ sudo apt update && sudo apt -y upgrade

 - 아파치 설치

$ sudo apt install -y apache2

 

웹브라우저에서 http://localhost/ 로 접속하면

아래의 이미지와 같은 페이지를 확인 할 수가 있습니다.

 

우분투에 아파치를 설치한 후에 localhost 로 접속한 모습

 

 

2. 데이터베이스(DB) 설치

 

- mysql 을 설치할 경우에

$ sudo apt install -y mysql-server

 

 - mariadb(마리아DB)를 설치할 경우에

 

$ sudo apt install -y mariadb-server mariadb-client

 

 - 디비 암호 및 원격접속 등 보안관련 설정

$ sudo mysql_secure_installation

위의 명령어를 실행해서 디비 root(루트) 암호를 설정하세요.

실행하시면 아래 화면처럼 설정을 물어보게 되는데요.

아래의 내용처럼 대부분 Y 를 누르시면 보안에 도움이 됩니다.

 

MySQL Server 인 경우에

 Would you like to setup VALIDATE PASSWORD component?
    Press y|Y for Yes, any other key for No: n

    Please set the password for root here.
    New password: EXAMPLE_PASSWORD
    Re-enter new password: EXAMPLE_PASSWORD

    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

    Remove test database and access to it? (Press y|Y for Yes, any other key for No): y

    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

MariaDB Server 인 경우에

Enter current password for root (enter for none): ENTER

    Set root password? [Y/n]: Y

    New password: EXAMPLE_PASSWORD

    Re-enter new password: EXAMPLE_PASSWORD

    Remove anonymous users? [Y/n] Y

    Disallow root login remotely? [Y/n] Y

    Remove test database and access to it? [Y/n] Y

    Reload privilege tables now? [Y/n] Y

 

DB에 root 로 패스워드를 사용하여 접속하세요. 

sudo mysql -u root -p

 

디비 생성하기

MySQL Server 인 경우에

mysql> CREATE database test_database;

MariaDB Server 인 경우에

MariaDB [(none)]> CREATE database test_database;

 

데이터베이스 보기

MySQL Server 인 경우에

mysql> SHOW DATABASES;

MariaDB Server 인 경우에

MariaDB [(none)]> SHOW DATABASES;

 

데이터베이스 보기

MySQL Server 인 경우에

mysql> CREATE USER 'test_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'EXAMPLE_PASSWORD';
       GRANT ALL PRIVILEGES ON test_database.* TO 'test_user'@'localhost';
       FLUSH PRIVILEGES;
       EXIT;

MariaDB Server 인 경우에

MariaDB [(none)]> CREATE USER 'test_user'@'localhost' IDENTIFIED BY 'EXAMPLE_PASSWORD';
                  GRANT ALL PRIVILEGES ON test_database.* TO 'test_user'@'localhost';
                  FLUSH PRIVILEGES;
                  EXIT;

 

3.PHP 설치하기

$ sudo apt install -y php
$ sudo apt install -y php-{common,mysql,xml,xmlrpc,curl,gd,imagick,cli,dev,imap,mbstring,opcache,soap,zip,intl}

 

아파치 서버 재시작

$ sudo systemctl restart apache2

 

디비 접속 테스트 예시

<?php

$conn = new mysqli('localhost', 'test_user', 'EXAMPLE_PASSWORD', 'test_database');

if ($conn->connect_error) {
    die("Database connection failed: " . $conn->connect_error);
}

echo "Database connection was successful";

?>

 

#apache #php #mysql #아파치 #우분투

반응형