본문 바로가기

Linux

리눅스 웹서버 구축 (Apache, Php, Mysql)



웹서버 구축 = APM설치 (Apache Php Mysql)



1. Apache = httpd


[root@localhost ~]# rpm -qa | grep httpd (--> 있으면 rpm -e --nodeps로 지워주기)
[root@localhost ~]# mkdir /usr/local/httpd
[root@localhost ~]# cd /usr/local/httpd
[root@localhost httpd]# wget http://pkgs.fedoraproject.org/repo/pkgs/httpd/httpd-2.2.11.tar.gz/03e0a99a5de0f3f568a0087fb9993af9/httpd-2.2.11.tar.gz
[root@localhost httpd]# tar zxvf httpd-2.2.11.tar.gz 
[root@localhost httpd]# cd httpd-2.2.11


[root@localhost httpd-2.2.11]# ./configure \
--prefix=/usr/local/httpd \
--enable-module=so \   --> DSO모드 활성화
--enable-mods-share=all --> 모든 모듈을 DSO모드로 지정

**아파치 설치 방식에는 DSO 방식과 Static 방식이 있는데, DSO모드는 xinetd랑 동일하다고 생각하면 된다. 
즉, 사용자의 요청이 있을 때만 모듈을 로드하는 방식.

[root@localhost httpd-2.2.11]# make && make install


[root@localhost httpd-2.2.11]# cd /usr/local/httpd/bin
[root@localhost bin]# ./apachectl start


[root@localhost bin]# ps -ef | grep httpd
root     22990     1  0 16:34 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
daemon   22991 22990  0 16:34 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
daemon   22992 22990  0 16:34 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
daemon   22993 22990  0 16:34 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
daemon   22994 22990  0 16:34 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
daemon   22995 22990  0 16:34 ?        00:00:00 /usr/local/httpd/bin/httpd -k start
root     23012  4832  0 16:35 pts/5    00:00:00 grep httpd

확인해봤을때 저렇게 6개가 뜨면 됨.

더 확인하기 위해서는 http://localhost 나 http://127.0.0.1로 접속했을 때 It works! 가 뜨면 된것임.
(http://내아이피주소 해도 됨, 다른 컴퓨터아이피주소 쳤을때 그 컴퓨터에 아파치가 설치되서 실행되고 있으면 접속가능
하지만 아니면 접속이 불가능하다고 나온다)


자동실행 설정은 필요하지 않아서 설명ㄴㄴㄴㄴㄴ





2. Mysql


[root@localhost ~]# mkdir -p /usr/local/mysql
[root@localhost ~]# cd /usr/local/mysql/
[root@localhost mysql]# wget http://pkgs.fedoraproject.org/repo/pkgs/mysql/mysql-5.0.45.tar.gz/a2a1c5a82bb22b45ab76a8ecab94e10d/mysql-5.0.45.tar.gz
[root@localhost mysql]# tar zxvf mysql-5.0.45.tar.gz
[root@localhost mysql]# cd mysql-5.0.45


[root@localhost mysql-5.0.45]# vi INSTALL-SOURCE 를 읽어보면 설명이 잘 나와있음. 요렇게..

     shell> groupadd mysql
     shell> useradd -g mysql mysql
     shell> gunzip < mysql-VERSION.tar.gz | tar -xvf -
     shell> cd mysql-VERSION
     shell> ./configure --prefix=/usr/local/mysql
     shell> make
     shell> make install
     shell> cp support-files/my-medium.cnf /etc/my.cnf
     shell> cd /usr/local/mysql
     shell> bin/mysql_install_db --user=mysql
     shell> chown -R root  .
     shell> chown -R mysql var
     shell> chgrp -R mysql .
     shell> bin/mysqld_safe --user=mysql &


그럼 쭉 따라해봅시다.

[root@localhost mysql-5.0.45]# groupadd mysql
[root@localhost mysql-5.0.45]# useradd -g mysql mysql
--> mysql 데몬을 위해서 mysql 유저와 mysql 그룹이 필요

./configure --help 치면 옵션들이 쭈욱 나옴, 우리가 쓸 옵션들은 
 --prefix=PREFIX
 --with-charset=CHARSET   --> 기본문자셋을 utf8fh
 --localstatedir=  --> 로그파일과 db디렉토리가 저장되는 곳 설정, 설정을 따로 안할경우 PREFIX/var <<여기에 저장됨.

[root@localhost mysql-5.0.45]# ./configure \
> --prefix=/usr/local/mysql \
> --with-charset=utf8 \
> --localstatedir=/usr/local/mysql/data 

[root@localhost mysql-5.0.45]# make && make install
이거 좀 오래걸리니까 당황하지 말긔


[root@server103 bin]# ./mysql_install_db --user=mysql
-->mysql 처음 설치하는 사람은 이 명령어를 꼭 실행해야함.
    MySQL data 디렉토리를 생성하고, root로 접속되어 있을경우는 뒤에 --user=mysql를 꼭 붙여주어야 함. 
    만약 mysql로 접속해 작업하고 있다면 생략해도 괜츈타. 


[root@server103 bin]# chown -R root /usr/local/mysql/
[root@server103 bin]# chown -R mysql /usr/local/mysql/data 
-->아까 ./configure할때 설정 안해놨으면 data->var로 바꾸면 됨.
-->mysql의 하위 디렉토리 및 파일의 소유주를 root로 변경하고,
    데이터베이스가 생성될 /data 디렉토리의 소유주는 mysql로 바꿔주기


아까 ./mysql_install_db --user=mysql할 때 나오던 설명에 보면 이런게 있다.
You can start the MySQL daemon with:
cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &
--> 즉, mysql을 실행하기 위해서는 mysql 데몬이 생성되어 있어야 한다!!! 

[root@server103 bin]# ./mysqld_safe --user=mysql &

프로세스가 잘 돌아가고 있는지 확인
[root@server103 bin]# ps -ef | grep mysql
root     20781  3125  0 09:28 pts/1    00:00:00 /bin/sh ./mysqld_safe --user=mysql
mysql    20799 20781  0 09:28 pts/1    00:00:00 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --pid-file=/usr/local/mysql/data/server103.pid --skip-external-locking

잘돌아가고 있는걸 확인했으니 이제 mysql을 실행!
[root@server103 bin]# ./mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.30 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

↑↑↑↑↑
이렇게 뜨면 성공! (나오려면 exit나 quit)



역시 자동실행설정을 필요하지 않으므로 생략.





3. php


[root@server103 ~]# mkdir /usr/local/php
[root@server103 ~]# cd /usr/local/php
[root@server103 php]# wget http://pkgs.fedoraproject.org/repo/pkgs/php/php-.2.0.tar.gz/52d7e8b3d8d7573e75c97340f131f988/php-5.2.0.tar.gz
[root@server103 php]# tar zxvf php-5.2.0.tar.gz 
[root@server103 php]# cd php-5.2.0


[root@server103 php-5.2.0]#./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/httpd/bin/apxs \         --> Build shares Apache 2.0 Handler module. 
--with-mysql=/usr/local/mysql \         --> Include mysql support. DIR is the mysql base directory
--with-config-file-path=/usr/local/httpd/conf \ --> Set the path in which to look for php.ini [PREFIX/lib]
--with-exec-dir=/usr/local/httpd/bin

[root@server103 php-5.2.0]# make && make install

[root@server103 php-5.2.0]# php -v
확인차 php -v 하면 됨 (version number 체크하기 위한 명령어: 버전이 퓽퓽뜨면 잘 설치가 되었다는것임)
(**거꾸로 갈라면 make install clean, make clean --> 나중에 뭐 잘못되서 다 지우는것보단 이걸 사용해서 go back**)


php환경설정을 하기 위해서 /usr/local/httpd/conf 가서 httpd.conf 열기
[root@server103 conf]# vi /usr/local/httpd/conf/httpd.conf

추가할 내용:
AddType application/x-httpd-php .php .htm .html 
AddType application/x-httpd-php-source .phps 
--> .php .html .htm 을 만났을때 안에 php소스가 있으면 x-httpd-php로 읽고, .phps안에 php소스는 x-httpd-php-source로 읽어라 라는걸 추가해주

는거임

추가할 위치:
나는 line 306줄 근처에 추가 (AddType이라고 쭉 써있는데 그 밑에다가 추가해주면됨)
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php .html .htm  <<<추가
AddType application/x-httpd-php-source .phps <<<추가

저장하고 나오기.
(이때 이미 아파치가 실행되고 있으면 /usr/local/httpd/bin/apachectl restart 해주기)


이제 테스트를 해보기 위해서 WEB 문서를 /usr/local/httpd/htdocs 에다가 생성해야함. 
[root@server103 conf]# vi /usr/local/httpd/htdocs/phptest.php
(나는 phptest.php로 했지만 이거는 맘대로 해도됨, 대신 .php로 끝나야 한다는것!)

<?php
        phpinfo();
?>

이걸적어주고 저장하고 나오기


웹브라우저를 키고 http://localhost/phptest.php 나 http://(내아이피주소)/phptest.php를 했을때
이런 화면이 나오면 성공!

따단~


+++
 /usr/local/httpd/htdocs/phptest.php 대신
/usr/local/httpd/htdocs 에 있는 index.html 여기에다가 내용을 적어주고 
웹브라우저 키고 http://localhost 나 아이피주소만 쳐도 테스트 화면이 나올 수 있다.