2011. 8. 3. 14:45

VMWare+Ubuntu+APM(Apache,PHP,MYSQL)+RSYNC 로 웹 서버 환경 구축 하기

제목처럼,

  • Ubuntu Desktop
  • Apache
  • PHP
  • MySQL
  • RSYNC

기반의 php 웹 서버 환경 구축 가이드를 공유합니다.

서버는 VMWare상의 Ubuntu로 가정합니다.
(물론, VMWare가 아닌 물리 서버도 가능합니다.)
그리고, 개발 PC는 Windows이며, FTP 대신 RSYNC로 웹 소스를 Deploy 합니다.
(물론, Windows 말고 Linux 기반에서도 RSYNC로 Deploy 가능합니다.)

Ubuntu Desktop 설치

http://www.ubuntu.com/download/ubuntu/download
에서 Ubuntu를 다운로드 합니다. (기준 버전 : 11.04)
해당 ISO를 가지고 VMWare 상으로 설치합니다.
물론, Ubuntu Server를 사용해도 아래 Package들만 설치되면 무방하리라 봅니다.

Apache2 설치

아래와 같이 Terminal을 실행합니다.
(이후의 모든 과정은 이러한 Terminal에서 실행합니다.)



~$ sudo apt-get update
Ign http://us.archive.ubuntu.com natty InRelease                   
Ign
http://extras.ubuntu.com natty InRelease                       
Ign
http://security.ubuntu.com natty-security InRelease
...

와 같이 package 정보를 업데이트합니다.

~$ sudo apt-get install apache2
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
  apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1
  libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
Suggested packages:
  apache2-doc apache2-suexec apache2-suexec-custom
The following NEW packages will be installed:
  apache2 apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common
  libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap
0 upgraded, 9 newly installed, 0 to remove and 201 not upgraded.
Need to get 3,104 kB of archives.
After this operation, 10.5 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
...

와 같이 apache2 package를 설치합니다.

설정 파일은 /etc/apache2/apache2.conf 이며,
www root 경로는 /var/www가 기본으로 지정되어 있습니다.

MYSQL 설치

~$ sudo apt-get install mysql-server mysql-client
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed
...
Need to get 22.6 MB of archives.
After this operation, 54.6 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
...


를 진행합니다.

아래 부분도 진행합니다.

~$ sudo apt-get install libapache2-mod-auth-mysql
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  libapache2-mod-auth-mysql
0 upgraded, 1 newly installed, 0 to remove and 201 not upgraded.
Need to get 25.9 kB of archives.
After this operation, 115 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu/ natty/main libapache2-mod-auth-mysql i386 4.3.9-13ubuntu1 [25.9 kB]
Fetched 25.9 kB in 1s (14.5 kB/s)                   
Selecting previously deselected package libapache2-mod-auth-mysql.
(Reading database ... 132812 files and directories currently installed.)
Unpacking libapache2-mod-auth-mysql (from .../libapache2-mod-auth-mysql_4.3.9-13ubuntu1_i386.deb) ...
Setting up libapache2-mod-auth-mysql (4.3.9-13ubuntu1) ...


PHP 설치

~$ sudo apt-get install php5-mysql
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
...
0 upgraded, 5 newly installed, 1 to remove and 201 not upgraded.
Need to get 6,397 kB of archives.
After this operation, 17.1 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
...
Creating config file /etc/php5/cli/php.ini with new version
update-alternatives: using /usr/bin/php5 to provide /usr/bin/php (php) in auto mode.
Setting up php5-mysql (5.3.5-1ubuntu7.2) ...


~$ sudo apt-get install php5-gd php-pear
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
...
After this operation, 20.1 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
...
update-alternatives: using /usr/bin/phpize5 to provide /usr/bin/phpize (phpize) in auto mode.
Setting up php5-gd (5.3.5-1ubuntu7.2) ...


~$ sudo apt-get install php5-curl
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following extra packages will be installed:
  libcurl3
...
Do you want to continue [Y/n]? y


~$ sudo apt-get install php5
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  php5
...
Unpacking php5 (from .../php5_5.3.5-1ubuntu7.2_all.deb) ...
Setting up php5 (5.3.5-1ubuntu7.2) ...


와 같이 PHP와 관련된 다른 package도 설치합니다.

VMWare Networking 설정

2010/10/04 - [Research/Ubuntu] - VMWare에서 ubuntu 설치후 SSH 연결하기
를 참고하여 VMWare에서의 Ubuntu Network 설정 과정을 따라하면 됩니다.

위 Link의 방법이 어렵다면,


와 같이 Network Connections를 실행하고,


에서 Address, Netmask, Gateway, DNS server를 다음 정보로 채웁니다.

  • Address
    ; Virtual Network Editor -> VMnet8 (NAT) -> DHCP Settings -> Start IP Address ~ End IP Address 맘에 드는것 하나
  • Netmask
    ; Virtual Network Editor에 있는 Subnet mask
  • Gateway
    ; Vitrual Network Editor -> VMnet8 (NAT) -> NAT Settings에 있는 Gateway IP
  • DNS Server
    ; 그냥 아는 DNS 서버 IP

와 같이 UI 상으로 설정 가능합니다.

그리고, Virtual Network Editor->VMnet8 (NAT) -> NAT Settings에 아래와 같이 Port Forwarding 시키십시요.
물론, Virtual Machine IP Address는 위 Start IP Address ~ End IP Address 중 선택한 IP로 지정해야 합니다.
(즉, VMWare를 설치한 IP가 들어가는 것이 아닙니다.)

그럼, 아래와 같이 테스트 됩니다.

VMWare를 실행한 PC(Windows)에서, 127.0.0.1로 테스트한 화면입니다.
물론, VMWare를 실행한 PC의 IP 주소로 다른 PC에서 접근 가능합니다.

SSH 설치

~$ sudo apt-get install ssh
Reading package lists... Done
Building dependency tree      
Reading state information... Done
...
Need to get 318 kB of archives.
After this operation, 954 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
...
ssh start/running, process 23151
Setting up ssh (1:5.8p1-1ubuntu3) ...
Setting up ssh-import-id (2.4-0ubuntu1) ...


xinetd 설치 & RSYNC 설정

RSYNC는 Ubuntu Desktop에 기본으로 설치되어 있습니다.
[주의] 본 부분은 Ubuntu에 기본 포함되었기 때문에, 아래 Section(Windows에서 RSYNC로 Apache 서버에 소스 Deploy하기)으로 바로 넘어가기 바랍니다. 만일, 해당 부분에서 오류가 발생한다면, 본 Section 내용대로 RSYNC 설치를 진행하면 됩니다.

xinetd를 설치하여 RSYNC를 daemon으로 돌리도록 합니다.

~$ sudo apt-get install xinetd
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following NEW packages will be installed:
  xinetd
...R
Setting up xinetd (1:2.3.14-7ubuntu4) ...
xinetd start/running, process 23582

그다음, rsync 설정을 진행합니다.

~$ sudo gedit /etc/xinetd.d/rsync
service rsync
{
 disable = no
 socket_type = stream
 wait = no
 user = root
 server = /usr/bin/rsync
 server_args = --daemon
 log_on_failure += USERID
}
와 같이 파일을 저장합니다. (앞부분엔 Tab 키 입니다.)

~$ sudo gedit /etc/rsyncd.conf
[www]
path = /var/www
comment = apache web server
uid = nobody
gid = nobody
use chroot = yes
read only = no
hosts allow = all host
max connections = 3
timeout = 600

~$ sudo service xinetd restart
xinetd start/running, process 23582


Windows에서 RSYNC로 Apache 서버에 소스 Deploy하기

RSYNC로 Deploy하면, FTP 보다 좀더 편리하게 서버에 업로드 할 수 있습니다.
즉, .bat만 실행하면 서버로 파일들을 전송하게 됩니다.
RSYNC에는 수많은 옵션이 제공되니, 각자 연습을 하시기 바랍니다.

일단, /var/www가 현재 Apache의 root로 되어 있는데, RSYNC로 업로드하는데 권한 문제가 야기될 수 있습니다.
그래서, /var/www 대신 /home/계정ID/public_html로 이동하여 관리하도록 합니다.
참고로 계정ID는 greenfish로 가정하고 진행합니다.

~$ sudo gedit /etc/apache2/sites-available/default
하고 아래와 같이 변경합니다.

DocumentRoot /var/www
->
DocumentRoot /home/greenfish/public_html

<Directory /var/www/>
->
<Directory /home/greenfish/public_html/>

기존의 /var/www/index.html을 /home/greenfish/public_html로 복사합니다.

또, 아래을 실행해서 read 권한으로 변경합니다.
~$ chmod 755 public_html

그리고, 아래와 같이 Apache 서버를 재시작 합니다.
~$ sudo service apache2 restart

http://www.rsync.net/resources/howto/windows_rsync.html
에서 cwRsync here를 통해 Windows RSYNC 설치본을 받을 수 있습니다.

실제로 RSYNC를 직접 설치할 필요는 없으며,
임의로 설치한 PC에서, 설치 경로(C:\Program Files\cwRsync\bin)의 파일을 복사하여 사용해도 무방합니다.
(해당 바이너리를 공유하고 싶지만, 저작권 문제가 걸릴까봐 비워둡니다. 알아서 bin 경로를 수집하시기 바랍니다.)

많일, D:\Tool\rsync에 cwRsync의 bin 내용이 있다면, (즉, D:\Tool\rsync\rsync.exe 존재)
아래와 같이 Test_CopyTo.bat를 만듭니다.

SET RSYNC_PATH=D:\Tool\rsync
SET CYGWIN=nontsec
SET HOME=D:\Tool\rsync\home
SET PATH=%PATH%;%RSYNC_PATH%
rsync -avz -e ssh --stats /cygdrive/d/_work/webserver/public_html 127.0.0.1:/home/greenfish
pause

위 .bat는 다음과 같은 역할을 합니다. (색갈있는 부분이 시스템별로 변동될 수 있는 내용입니다.)

  • D:\_work/webserver/public_html 경로의 내용을 127.0.0.1 서버의 /home/greenfish/public_html
    로 전송한다.

와 같습니다.
/cygdrive/d는 윈도우 경로의 D:\와 같으며, 아러한 Format 규칙을 지켜야 합니다.
rsync Src Dest 와 같은 순서인데, Src의 내용을 Dest에 복사하는 경우로 생각하면 됩니다.
즉, 위 .bat는 Upload에 해당되며, Src Dest 순서를 변경하면 Download 논리가 됩니다.

127.0.0.1:/home/greenfish/public_html로 선언하지 않는 이유는, rsync에서 Dest의 경로를 그대로 받지 않고, Src의 마지막 경로 조각을 강제로 붙여서 전송하였기 때문입니다. 이유는 모르겠습니다. ;-;

위 .bat를 실행하면 사용자 계정 Password를 입력받고, 복사가 진행됩니다. (Upload)
만일, 다른 계정으로 하고 싶다면, foobar_id@127.0.0.1:/home/.... 와 같이 USER@HOST 와 같이 전달하면 됩니다.

그리고 --delete 를 추가하면 Src에 없는 파일인 경우 삭제하여 동기화를 달성해 줍니다.

D:\...>SET RSYNC_PATH=D:\Tool\rsync

D:\...>SET CYGWIN=nontsec

D:\...>SET HOME=D:\Tool\rsync\home

D:\...>SET PATH=...;D:\Tool\rsync

D:\...>rsync -avz -e ssh --stats /cygdrive/d/_work/webserver/public_html 127.0.0.1:/home/greenfish
greenfish@127.0.0.1's password:
sending incremental file list
public_html/
public_html/a.txt

Number of files: 3
Number of files transferred: 1
Total file size: 180 bytes
Total transferred file size: 3 bytes
Literal data: 3 bytes
Matched data: 0 bytes
File list size: 92
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 152
Total bytes received: 35

sent 152 bytes  received 35 bytes  34.00 bytes/sec
total size is 180  speedup is 0.96


만일, Src 경로에 아래와 같이 a.txt가 있었다고 가정합니다.


그럼 위 .bat를 실행한 이후에는,


와 같이 a.txt가 업로드된 것을 확인할 수 있습니다.

그럼 아래와 같이 a.txt를 열어볼 수 있습니다.

'Web > PHP' 카테고리의 다른 글

Ubuntu+PHP+mongoDB 구축시 유의 사항  (2) 2011.09.06
ubuntu+php에서 mongoDB 설치하기  (0) 2011.09.02
PHP Shell  (0) 2011.08.03