Apache : 1.3.41 -> 2.2, 2.4 

Mysql : 4.0.27 -> 4.1 -> 5.6 

Php : 4.x -> 5.x


경험상 다양한 버전을 가지고 여러가지 컴파일 방법을 연습해둬야 한다. 

이유는 ? 

1. 운영중인 사이트가 오래된 버전을 사용하다가 (A:1.3.41, M:4.0.27, P:4.x) -> 상위버전으로 올리고 싶다고해서 무턱대고 올리면 

잘 돌아가던 애플리케이션이 php 버전문제로 안될 수 있기 때문이다.

또한 최신버전의 php 버전을 사용하다가보면 현재 사용중인 애플리케이션이 동작하지 않을 수 있으므로 마이그레이션을 해야한다. 


LAMP 


o APM 을 사용하는 방식 : static, DSO 방식 두가지를 사용한다. 


o APM 을 설치하는 방법 

1. yum 을 이용해서 설치하는 방법 

- DSO 방식으로 설치한다.

- 생략  


2. 소스를 이용해서 설치하는 방법 


o APM 설치순서 

MySQL install -> Apache install -> Php install -> mod_security -> phpmyadmin 다양한 모듈들을 설치 


필요한 패키지 : ncurses-devel



구글 -> ncurses 예제 검색 

ncurses 프로그래밍

http://www.joinc.co.kr/modules/moniwiki/wiki.php/article/ncurses_%C7%C1%B7%CE%B1%D7%B7%A1%B9%D6



======= mysql 소스설치의 옵션들 ======= 

!!! 참고로 이외의 여러가지 다양한 옵션이 존재하는걸 볼 수 있다. !!!

!!! gcc <-- pgcc 로 컴파일하면 gcc 

!!! --with-raid        Enable RAID Support

!!! --without-server   Only build the client.

!!! mysql5 에서는 mysqli 라는 확장된 mysql이 존재하므로 이를 설치해야 한다. 


--prefix : mysql 이 설치될 디렉토리 

--localstatedir : mysql DB가 저장될 디렉토리 

--with-mysqld-user : mysql 데몬 사용자 

--with-charset : 언어설정 euc_kr 로 지정 

--enable-thread-safe-client  :  apache compile시 기본적으로 thread mode로 설치가 되기 때문에 위의 옵션을 주지 않을 경우 연동이 되지 않고, 

                                php configure 시에 mysql-client 에러가 발생한다.

=================================================================


APM.vol1.egg


APM.vol2.egg


################

## mysql 설치 ##

################


==== 체크사항 ====

mysql 소스로 설치할 수 있는가 ?

mysql 4.x and 5.x 를 소스로 설치할 수 있는가 ? 

mysql 을 yum 으로 설치할 수 있는가 ? 

mysql 운영중에 홈페이지가 에러가 발생이되면(DB가 깨진 것이다.) 복구할 수 있는가 ?

mysql DB를 다른쪽 mysql 서버로 옮길 수 있는가 ?

mysql DB를 백업(덤프) 받을 수 있는가 ?

mysql 의 접속이 많을때 부하분산을 할 수 있는가 ?

mysql replication 으로 운영할 수 있는가 ?

 :

 :     <-- 나머지 부분은 정리해서 알려줄 것!!!


1. 패키지 확인 작업 

# rpm -qa |grep mysql   <-- 설치된 mysql 패키지가 있는지 확인한다. 

# yum grouplist 

# yum groupremove "MySQL Database"

# rpm -e `rpm -qa | grep mysql` --nodeps

# userdel -r mysql



2. 환경설정

# vi /usr/include/pthread.h 

...

  /* Linuxthreads */ <-- CentOS 5 에서는 반드시 추가해야 한다. CentOS 4에서는 들어있으므로 추가하지 않아도 된다. 

...



# tar xzf mysql-4.0.27.tar.gz 

# cd mysql-4.0.27



# ./configure \

--prefix=/usr/local/mysql \

--localstatedir=/usr/local/mysql/data \

--with-mysqld-user=mysql \

--enable-thread-safe-client \

--with-charset=euc_kr  


3. 컴파일 / 설치

# make && make install



5. 세팅 

- 실행파일 링크를 걸어준다. 

# ln -s /usr/local/mysql/bin/* /usr/bin               

# ln -s /usr/local/mysql/libexec/mysqld /usr/sbin     



- 설정파일 복사한다.

# cd support-files

# install -m 600 my-large.cnf /etc/my.cnf  


- 데몬실행 스크립트 복사한다.

# install -m 700 mysql.server /etc/init.d/mysqld  


- 부팅시 활성화 등록을 한다.

# chkconfig --add mysqld  

# chkconfig --list mysqld

mysqld         0:off 1:off 2:on 3:on 4:on 5:on 6:off



- DB 초기화 작업 

- RedHat 계열의 사용자/그룹 정보 확인 

- cat /usr/share/doc/setup-2.5.58/uidgid  

# mysql_install_db  



# useradd -s /bin/false -d /usr/local/mysql/data -M -r -u 27 mysql  

# chown -R mysql.mysql /usr/local/mysql/data            


- mysql 데몬을 시작한다.

# /etc/init.d/mysqld start   


- mysql 포트(3306)를 확인한다.

# netstat -nltp | grep 3306 

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      12704/mysqld        



- root 비밀번호를 설정한다.

# mysqladmin -u root password qwer1234


- startup 파일를 생성한다. (root 로 자동으로 로그인하기 위한 설정)

# vi ~/.my.cnf   



-- /root/.my.cnf --

[client]

host = localhost

user = root

password = qwer1234

-- /root/.my.cnf --



- 접속이 되면 DB설치 성공!!!  

- 자동로그인 기능에 의해 접속 (~/.my.cnf 에 설정되어 있다)

# mysql mysql    

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1 to server version: 4.0.27-log


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



mysql> select user();

+----------------+

| user()         |

+----------------+

| root@localhost |

+----------------+

1 row in set (0.00 sec)


mysql> select database();

+------------+

| database() |

+------------+

| mysql      |

+------------+

1 row in set (0.00 sec)


- 비밀번호가 없는 레코드를 모두 삭제한다.

mysql> select host,user,password from user;          

+-----------------------+------+------------------+

| host                  | user | password         |

+-----------------------+------+------------------+

| localhost             | root | 57c0dba50319edd5 |

| localhost.localdomain | root |                  |    <-- 삭제

| localhost             |      |                  |    <-- 삭제

| localhost.localdomain |      |                  |    <-- 삭제

+-----------------------+------+------------------+

4 rows in set (0.00 sec)



mysql> delete from user where password = '';

Query OK, 3 rows affected (0.00 sec)


mysql> select host,user,password from user;

+-----------+------+------------------+

| host      | user | password         |

+-----------+------+------------------+

| localhost | root | 57c0dba50319edd5 |

+-----------+------+------------------+

1 row in set (0.00 sec)


mysql> select host,user,db from db;          

+------+------+---------+

| host | user | db      |

+------+------+---------+

| %    |      | test    |   <-- 삭제

| %    |      | test\_% |   <-- 삭제

+------+------+---------+

2 rows in set (0.00 sec)



mysql> delete from db;                       <-- db 테이블의 임시 자료를 모두 삭제 

Query OK, 2 rows affected (0.00 sec)


mysql> select host,user,db from db;

Empty set (0.00 sec)


mysql> flush privileges;    <-- user,db 테이블의 세팅되어 있는 권한으로 적용 

Query OK, 0 rows affected (0.00 sec)



# netstat -ant  <-- mysql 포트 확인

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address               Foreign Address             State      

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN        <-- mysql 포트대기

  :

  :

  :



# vi /etc/my.cnf

...

skip-networking    <-- 외부에서 접속할 필요가 없을땐 보안상 외부 접속을 차단하면 된다.

...



# /etc/init.d/mysqld restart

# netstat -nltp  <-- mysql 포트 3306 이 없는 것을 확인할 수 있다.

# ls -l /tmp/mysql.sock  <-- 이때는 unix domain socket 으로 통신을 한다.


# mysql   <-- /tmp/mysql.sock 파일을 통해서 접속하는 것이다.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1 to server version: 4.0.27-log


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


mysql> 



#################

## apache 설치 ##

#################


아파치 문서 : http://httpd.apache.org/docs/


1. 패키지 확인

# rpm -qa | grep httpd

httpd-2.2.3-82.el5.centos   <-- httpd 패키지가 설치되어 있다면 

# rpm -e httpd              <-- 삭제한다.


# yum grouplist                     <-- httpd 가 설치가 되어 있으면 available 부분을 확인 

# yum -y groupremove "web server"   <-- httpd 를 삭제 



2. 압축을 해제한다.

# tar xzf apache_1.3.41.tar.gz 

# cd apache_1.3.41

# ./configure --help   <-- 1.3.41 의 모듈을 확인 



  :

  :

--enable-module=NAME   enable  a particular Module named 'NAME'

--disable-module=NAME  disable a particular Module named 'NAME'

                        [access=yes      actions=yes     alias=yes      ]     <-- yes : 모듈 활성화 

                        [asis=yes        auth_anon=no    auth_dbm=no    ]     <-- no  : 모듈 비활성화 

                        [auth_db=no      auth_digest=no  auth=yes       ]

                        [autoindex=yes   cern_meta=no    cgi=yes        ]

                        [digest=no       dir=yes         env=yes        ]

                        [example=no      expires=no      headers=no     ]

                        [imap=yes        include=yes     info=no        ]

                        [log_agent=no    log_config=yes  log_forensic=no]

                        [log_referer=no  mime_magic=no   mime=yes       ]

                        [mmap_static=no  negotiation=yes proxy=no       ]

                        [rewrite=no      setenvif=yes    so=no          ]      <-- so : DSO 방식일때 반드시 모듈을 활성화시키고 컴파일해야 한다. 

                        [speling=no      status=yes      unique_id=no   ]

                        [userdir=yes     usertrack=no    vhost_alias=no ]      <-- vhost_alias : DSO 방식일때 반드시 모듈을 활성화시키고 컴파일해야 한다. 

  :

  :


- DSO 방식으로 compile

- configure 옵션은 config.status 파일에 저장된다.

# ./configure  \                   

--prefix=/usr/local/apache \

--enable-shared=max \

--enable-rule=SHARED_CORE \

--enable-module=so \

--enable-module=vhost_alias


# make

# make install



  :

  :

+--------------------------------------------------------+    <-- 설치가 완료되면 box 가 나온다.

| You now have successfully built and installed the          |

| Apache 1.3 HTTP server. To verify that Apache actually    |

| works correctly you now should first check the              |

| (initially created or preserved) configuration files           |

|                                                                  |

|   /usr/local/apache/conf/httpd.conf                      l

|                                                                   |

| and then you should be able to immediately fire up         |

| Apache the first time by running:                              |

|                                                                   |

|   /usr/local/apache/bin/apachectl start                  l

|                                                                  |

| Thanks for using Apache.       The Apache Group         |

|                                http://www.apache.org/       |

+--------------------------------------------------------+



# vi ~/.bash_profile



-- /root/.bash_profile --

  :

  :

APACHE_HOME=/usr/local/apache                          <-- 추가

PATH=$PATH:$MYSQL_HOME/bin:$APACHE_HOME/bin:$HOME/bin  <-- 수정

  :

  :

-- /root/.bash_profile --



# . ~/.bash_profile



# ln -s /usr/local/apache/bin/* /usr/bin



# apachectl start

/usr/bin/apachectl start: httpd started

# netstat -nltp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2982/httpd              <-- 웹서버 포트 대기 

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1651/sshd           



# ln -s /usr/local/apache/bin/apachectl /etc/init.d/httpd  


# ls /etc/rc3.d/*mysql*

/etc/rc3.d/S90mysqld

# vi /etc/init.d/mysqld



-- /etc/init.d/mysqld --

  :

  :

# Comments to support chkconfig on RedHat Linux

# chkconfig: 2345 90 20

# description: A very fast and reliable SQL database engine.

  :

  :

(확인)

-- /etc/init.d/mysqld --


# vi /etc/init.d/httpd



-- /etc/init.d/httpd --

  :

  :

# chkconfig: 2345 91 19               <-- 부팅시 활성화 세팅    !!! 시작은 mysql 보다 늦게 중지는 mysql 보다 빠르게 !!!

# description: Apache web server      <-- 부팅시 활성화 세팅 

  :

  :

-- /etc/init.d/httpd --



# chkconfig --add httpd

# chkconfig --list httpd

httpd           0:off 1:off 2:on 3:on 4:on 5:on 6:off



##############

## php 설치 ##

##############


o APM 의 관계 도식도 

 

                            +--> $PREFIX --conf/httpd.conf 

                            |             |       - LoadModule php4_module  libexec/libphp4.so

                            |             |       - AddModule mod_php4.c

                            |             `-libexec/libphp4.so

                            |

                            |                 +--> --with-mysql 

                            |                 |

+-----+          +-----+    |      +-----+     |       +-----+

|  C  | ------->|  S  | ---+--->|  P  | --------> |      |

|  L  |           |  E  |    |      |  H  |             |  M  |

|  I   |           |  R  |    |      |  P  |             |  Y  |

|  E  |           |  V  |    |      | 해  |             |  S  |

|  N  |           |  E  |    |      | 석  |             |  Q  |

|  T  | <-------|  R  | <--+---- | 기  | <-------- |  L  |

+-----+          +-----+    |      +-----+            +-----+

                            |

                            +-- --with-apxs=/usr/local/apache/bin/apxs


0. rpm 파일 삭제

# rpm -qa | grep php   <--  php 가 설치되어 있으면 삭제 

# rpm -e php-cli php-common



1. 환경설정

# tar xzf php-4.4.9.tar.gz

# cd php-4.4.9

# ./configure \                                <-- configure 옵션은 config.status 파일에 저장되고 phpinfo() 함수에서도 확인이 가능하다.

--prefix=/usr/local/php \

--with-apxs=/usr/local/apache/bin/apxs \

--with-mysql


2. 컴파일

# make


3. 보안점검   <-- 생략 

# make test    


4. 설치

# make install



5.  세팅 

# cp php.ini-dist /usr/local/php/lib/php.ini      <-- 설정파일 복사


# vi ~/.bash_profile



-- /root/.bash_profile --

  :

  :

PHP_HOME=/usr/local/php                                              <-- 추가

PATH=$PATH:$MYSQL_HOME/bin:$APACHE_HOME/bin:$PHP_HOME/bin:$HOME/bin  <-- 수정

  :

  :

-- /root/.bash_profile --



# . ~/.bash_profile 



--- APM 설치 끝 ---

ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf

ln -s /etc/my.cnf /etc/mysql.conf

ln -s /usr/local/php/lib/php.ini /etc/php.conf

# ls -l /etc/{httpd,mysql,php}.conf

lrwxrwxrwx 1 root root 33 Nov  1 08:37 /etc/httpd.conf -> /usr/local/apache/conf/httpd.conf

lrwxrwxrwx 1 root root 11 Nov  1 08:38 /etc/mysql.conf -> /etc/my.cnf

lrwxrwxrwx 1 root root 26 Nov  1 08:38 /etc/php.conf -> /usr/local/php/lib/php.ini



# vi /usr/local/apache/conf/httpd.conf


  :

  :

<IfModule mod_dir.c>  <-- 400번 라인 부근에 위치

    DirectoryIndex index.html  index.php index.php3 index.htm  <-- index.php index.php3 index.htm 도 추가 

</IfModule>

  :

  :

AddType application/x-tar .tgz  <-- 800번 라인 부근에 위치

AddType application/x-httpd-php .php .php3 .html .htm  <-- 추가 

# AddType application/x-httpd-php-source .phps         <-- 소스코드로 인식 (보안상 현재는 주석처리)




# apachectl configtest    <--  SELinux 가 설정되어 있으면 아래 에러가 발생

Syntax error on line 223 of /usr/local/apache/conf/httpd.conf:

Cannot load /usr/local/apache/libexec/libphp4.so into server: /usr/local/apache/libexec/libphp4.so: cannot restore segment prot after reloc: Permission denied


# setup   <-- SELinux 를 중지



# apachectl configtest

Syntax OK


# apachectl restart

/usr/bin/apachectl restart: httpd restarted


# cd /usr/local/apache/htdocs 

# rm -rf *                           <-- Default 파일 모두 삭제

# touch index.html

# echo '<? phpinfo(); ?>' > p.html   <-- phpinfo 파일 생성 



http://192.168.56.101/p.html         <-- 브라우저에서 확인 



php -m

[PHP Modules]

ctype

mysql

overload

pcre

posix

session

standard

tokenizer

xml


[Zend Modules]



###################

## php 실무 TIP  ##

###################


1. 어떤 소스를 가져와서 실행할려고 했더니 아래와 같은 에러메세지가 발생되었다.

- Call to undefined function: socket_create() 이라는 메세지는 현재 socket_create() 함수가 실행되는 라이브러리가 php 파서를 

  인식을 못하기 때문이다. 

  해결방안 : php 를 socket_create() 함수가 인식되게 다시 컴파일해야 한다.


Fatal error: Call to undefined function: socket_create() 


o php 파일에 포함되어 있는 라이브러리

- php를 컴파일할때 옵션으로 enable 시켜준다.


o php 파일에 포함되지 않은 라이브러리 

- 외부의 파일을 가져와서 먼저 설치한 후에 php 를 컴파일할때 enable 시켜준다.


# cd /usr/local/apache/htdocs

# vi index.html

-- index.html -- 

<?php

error_reporting(E_ALL);


/* Allow the script to hang around waiting for connections. */

set_time_limit(0);


/* Turn on implicit output flushing so we see what we are getting as it comes in. */

ob_implicit_flush();


$address = '0.0.0.0';

$port = 10000;


if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) == false) {

    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";

}


if (socket_bind($sock, $address, $port) == false) {

    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

}


if (socket_listen($sock, 5) == false) {

    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

}


if (($msgsock = socket_accept($sock)) === false) {

        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

        break;

}

/* Send instructions. */

$msg = "\nWelcome to the PHP Test Server. \n" .  "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";


socket_write($msgsock, $msg, strlen($msg));

socket_close($msgsock);


socket_close($sock);

?>

-- index.html -- 



브라우저에서 접근시 : http://192.168.56.101   <-- 정상적으로 실행이 안된다.

Fatal error: Call to undefined function: socket_create()  에러가 발생한다.



-- 해결방안 --

!!! 기존 소스 디렉토리로 이동해서 socket 함수를 쓸 수 있도록 옵션을 추가해서 다시 재설치 한다. !!!


# cd <php source directory>

# ./configure  \

--prefix=/usr/local/php \

--with-apxs=/usr/local/apache/bin/apxs \

--with-mysql \

--enable-sockets                               <-- 추가한다. 

# make

# make install 



# apachectl restart


브라우저에서 접근시 : http://192.168.56.101    <-- 정상적으로 실행이 된다.


# netstat -nat

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Pro

  :

  :

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      32179/h/httpd

tcp        0      0 0.0.0.0:10000               0.0.0.0:*                   LISTEN      32386/httpd          <-- 10000 포트가 열렸다.

  :

  :



!!! php 에서 기능들을 확인하는 옵션 


# ./configure --help | grep with


  :

  :

  --with-mysql[=DIR]      Include MySQL support. DIR is the MySQL base directory.

  :

  :


# ./configure --help | grep enable


  :

  :

  --enable-sockets        Enable sockets support

  :

  :