Selasa, 06 November 2012

Install Web Server di Linux (Apache & PHP)

Ketika ditanya server apa yang paling sering digunakan, jawaban akan tertuju kepada web server. Web server memberikan layanan kepada user berupa halaman web yang bisa di request melalui protokol http.
Pada kesempatan ini, saya tidak akan terlalu mendalam kepada definisi dari web server secara teori, karena untuk teori bisa di search untuk lebih lengkapnya. Saat ini saya akan lebih fokus pada instalasi web server Apache dan PHP pada server berbasis Linux.
Sebenarnya cara paling mudah untuk instalasi web server adalah menggunakan repository dari distro masing – masing.  Tapi saat ini saya akan membahas instalasi web server dengan melakukan compile secara manual dari source code. Dengan melakukan compile dari source akan memudahkan administrasi dan efesiensi  terhadap webserver karena kita dapat memilih modul – modul apa saja yang akan diinstall pada webserver kita bahkan lokasi file konfigurasinya bisa kita tentukan.
Cara install Apache dan PHP adalah sebagai berikut :

download apache dari situs resminya, versi terbaru 2.2.19  dengan perintah :
1sudo su
2wget http://apache.the.net.id//httpd/httpd-2.2.19.tar.gz
ekstrak paket instalasi apache
1tar -xvzf httpd-2.2.19.tar.gz
2cd httpd-2.2.19
selanjutnya lakukan perintah configure untuk memilih modul – modul apa saja yang akan diinstal bersama dengan apache
1./configure --enable-so --enable-authn-dbm --enable-authn-dbd --enable-auth-digest --enable-isapi --enable-cache --enable-disk-cache --enable-mem-cache --enable-ssl --enable-cgi --with-ssl --enable-cgid --enable-dav
selanjutnya build paket apache dan modul yang dan lakukan instalasi
1make
2make install
instalasi apache selesai, secara default lokasi instalasi apache yang tadi diinstall akan berada pada direktori /usr/local/apache2. Untuk mengetahui apache telah terinstall, service apache harus diaktifkan dengan perintah :
1/usr/local/apache2/bin/apachectl start
kemudian akses melalui browser ke http://localhost. Jika muncul pesan It’s Work berarti apache sudah berjalan pada server.
Untuk instalasi php, pastikan service websever apache tidak sedang dalam keadaan up. Untuk mematikan sementara webserver apache, dilakukan dengan perintah :
1/usr/local/apache2/bin/apachectl stop
Selanjutnya untuk melakukan instalasi php dengan cara sebagai berikut :
download php dari situs resminya atau mirror yang paling dekat, kemudian ekstrak file instalasi php
1cd /usr/src
2wget http://id.php.net/distributions/php-5.3.6.tar.gz
3tar -zxvf php-5.3.6.tar.gz
4cd php-5.3.6
konfigurasi instalasi php untuk mengaktifkan modul – modul yang akan diinstall bersama php
1./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-config-file-path=/usr/local/php/etc/ --with-libdir=lib64 --with-mysql --with-zlib --with-gd --enable-mbstring --enable-exif --enable-gd-native-ttf --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-xmlrpc --without-pdo-sqlite --without-sqlite --disable-posix --disable-ipv6 --with-curl --enable-fastcgi
Selanjutnya compile source php dan lakukan instalasi dengan perintah :
1make
2make install
php tersebut diinstall pada lokasi sesuai dengan prefix yang dipakai pada konfigurasi, yaitu pada direktori /usr/local/php. Langkah selanjutnya adalah melakukan configurasi php.  File konfigurasi default sudah tersedia pada source php yang di download tadi, jadi kita tinggal memindahkan file konfigurasi tersebut dengan perintah :
1cp /usr/src/php-5.3.6/php.ini-production /usr/local/php/etc/php.ini
Selanjutnya lakukan penyesuaian pada config apache sehingga bisa membaca sintaks php dengan teks editor kesayangan anda, untuk membuat atau mengedit file konfigurasi dengan vim menggunakan perintah :
1vim /usr/local/apache2/conf/httpd.conf
Pada konfigurasi tersebut yang perlu dipastikan adalah terdapat baris yang berisi ” LoadModule php5_module modules/libphp5.so” kemudian perlu ditambahkan pada konfigurasi agar bisa menghandle sintaks php. Conoth  konfigurasi apache minimal adalah sebagai berikut :
001#
002# This is the main Apache HTTP server configuration file. It contains the
003 
004# configuration directives that give the server its instructions.
005 
006# See <URL:http://httpd.apache.org/docs/2.2> for detailed information.
007 
008# In particular, see
009 
011 
012# for a discussion of each configuration directive.
013 
014#
015 
016# Do NOT simply read the instructions in here without understanding
017 
018# what they do. They're here only as hints or reminders. If you are unsure
019 
020# consult the online docs. You have been warned.
021 
022#
023 
024# Configuration and logfile names: If the filenames you specify for many
025 
026# of the server's control files begin with "/" (or "drive:/" for Win32), the
027 
028# server will use that explicit path. If the filenames do *not* begin
029 
030# with "/", the value of ServerRoot is prepended -- so "logs/foo_log"
031 
032# with ServerRoot set to "/usr/local/apache2" will be interpreted by the
033 
034# server as "/usr/local/apache2/logs/foo_log".
035 
036#
037 
038# ServerRoot: The top of the directory tree under which the server's
039 
040# configuration, error, and log files are kept.
041 
042#
043 
044# Do not add a slash at the end of the directory path. If you point
045 
046# ServerRoot at a non-local disk, be sure to point the LockFile directive
047 
048# at a local disk. If you wish to share the same ServerRoot for multiple
049 
050# httpd daemons, you will need to change at least LockFile and PidFile.
051 
052#
053 
054ServerRoot "/usr/local/apache2"
055 
056#
057 
058# Listen: Allows you to bind Apache to specific IP addresses and/or
059 
060# ports, instead of the default. See also the <VirtualHost>
061 
062# directive.
063 
064#
065 
066# Change this to Listen on specific IP addresses as shown below to
067 
068# prevent Apache from glomming onto all bound IP addresses.
069 
070#
071 
072#Listen 12.34.56.78:80
073 
074Listen 80
075 
076#
077 
078# Dynamic Shared Object (DSO) Support
079 
080#
081 
082# To be able to use the functionality of a module which was built as a DSO you
083 
084# have to place corresponding `LoadModule' lines at this location so the
085 
086# directives contained in it are actually available _before_ they are used.
087 
088# Statically compiled modules (those listed by `httpd -l') do not need
089 
090# to be loaded here.
091 
092#
093 
094# Example:
095 
096# LoadModule foo_module modules/mod_foo.so
097 
098LoadModule php5_module modules/libphp5.so
099 
100#
101 
102<IfModule !mpm_netware_module>
103 
104<IfModule !mpm_winnt_module>
105 
106#
107 
108# If you wish httpd to run as a different user or group, you must run
109 
110# httpd as root initially and it will switch.
111 
112#
113 
114# User/Group: The name (or #number) of the user/group to run httpd as.
115 
116# It is usually good practice to create a dedicated user and group for
117 
118# running httpd, as with most system services.
119 
120#
121 
122User daemon
123 
124Group daemon
125 
126</IfModule>
127 
128</IfModule>
129 
130# 'Main' server configuration
131 
132#
133 
134# The directives in this section set up the values used by the 'main'
135 
136# server, which responds to any requests that aren't handled by a
137 
138# <VirtualHost> definition. These values also provide defaults for
139 
140# any <VirtualHost> containers you may define later in the file.
141 
142#
143 
144# All of these directives may appear inside <VirtualHost> containers,
145 
146# in which case these default settings will be overridden for the
147 
148# virtual host being defined.
149 
150#
151 
152#
153 
154# ServerAdmin: Your address, where problems with the server should be
155 
156# e-mailed. This address appears on some server-generated pages, such
157 
158# as error documents. e.g. admin@your-domain.com
159 
160#
161 
162ServerAdmin you@example.com
163 
164#
165 
166# ServerName gives the name and port that the server uses to identify itself.
167 
168# This can often be determined automatically, but we recommend you specify
169 
170# it explicitly to prevent problems during startup.
171 
172#
173 
174# If your host doesn't have a registered DNS name, enter its IP address here.
175 
176#
177 
178#ServerName www.example.com:80
179 
180#
181 
182# DocumentRoot: The directory out of which you will serve your
183 
184# documents. By default, all requests are taken from this directory, but
185 
186# symbolic links and aliases may be used to point to other locations.
187 
188#
189 
190DocumentRoot "/usr/local/apache2/htdocs"
191 
192#
193 
194# Each directory to which Apache has access can be configured with respect
195 
196# to which services and features are allowed and/or disabled in that
197 
198# directory (and its subdirectories).
199 
200#
201 
202# First, we configure the "default" to be a very restrictive set of
203 
204# features.
205 
206#
207 
208<Directory />
209 
210Options FollowSymLinks
211 
212AllowOverride None
213 
214Order deny,allow
215 
216Deny from all
217 
218</Directory>
219 
220#
221 
222# Note that from this point forward you must specifically allow
223 
224# particular features to be enabled - so if something's not working as
225 
226# you might expect, make sure that you have specifically enabled it
227 
228# below.
229 
230#
231 
232#
233 
234# This should be changed to whatever you set DocumentRoot to.
235 
236#
237 
238<Directory "/usr/local/apache2/htdocs">
239 
240#
241 
242# Possible values for the Options directive are "None", "All",
243 
244# or any combination of:
245 
246# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
247 
248#
249 
250# Note that "MultiViews" must be named *explicitly* --- "Options All"
251 
252# doesn't give it to you.
253 
254#
255 
256# The Options directive is both complicated and important. Please see
257 
259 
260# for more information.
261 
262#
263 
264Options Indexes FollowSymLinks
265 
266#
267 
268# AllowOverride controls what directives may be placed in .htaccess files.
269 
270# It can be "All", "None", or any combination of the keywords:
271 
272# Options FileInfo AuthConfig Limit
273 
274#
275 
276AllowOverride None
277 
278#
279 
280# Controls who can get stuff from this server.
281 
282#
283 
284Order allow,deny
285 
286Allow from all
287 
288</Directory>
289 
290#
291 
292# DirectoryIndex: sets the file that Apache will serve if a directory
293 
294# is requested.
295 
296#
297 
298<IfModule dir_module>
299 
300DirectoryIndex index.html
301 
302</IfModule>
303 
304#
305 
306# The following lines prevent .htaccess and .htpasswd files from being
307 
308# viewed by Web clients.
309 
310#
311 
312<FilesMatch "^\.ht">
313 
314Order allow,deny
315 
316Deny from all
317 
318Satisfy All
319 
320</FilesMatch>
321 
322#
323 
324# ErrorLog: The location of the error log file.
325 
326# If you do not specify an ErrorLog directive within a <VirtualHost>
327 
328# container, error messages relating to that virtual host will be
329 
330# logged here. If you *do* define an error logfile for a <VirtualHost>
331 
332# container, that host's errors will be logged there and not here.
333 
334#
335 
336ErrorLog "logs/error_log"
337 
338#
339 
340# LogLevel: Control the number of messages logged to the error_log.
341 
342# Possible values include: debug, info, notice, warn, error, crit,
343 
344# alert, emerg.
345 
346#
347 
348LogLevel warn
349 
350<IfModule log_config_module>
351 
352#
353 
354# The following directives define some format nicknames for use with
355 
356# a CustomLog directive (see below).
357 
358#
359 
360LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
361 
362LogFormat "%h %l %u %t \"%r\" %>s %b" common
363 
364<IfModule logio_module>
365 
366# You need to enable mod_logio.c to use %I and %O
367 
368LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
369 
370</IfModule>
371 
372#
373 
374# The location and format of the access logfile (Common Logfile Format).
375 
376# If you do not define any access logfiles within a <VirtualHost>
377 
378# container, they will be logged here. Contrariwise, if you *do*
379 
380# define per-<VirtualHost> access logfiles, transactions will be
381 
382# logged therein and *not* in this file.
383 
384#
385 
386CustomLog "logs/access_log" common
387 
388#
389 
390# If you prefer a logfile with access, agent, and referer information
391 
392# (Combined Logfile Format) you can use the following directive.
393 
394#
395 
396#CustomLog "logs/access_log" combined
397 
398</IfModule>
399 
400<IfModule alias_module>
401 
402#
403 
404# Redirect: Allows you to tell clients about documents that used to
405 
406# exist in your server's namespace, but do not anymore. The client
407 
408# will make a new request for the document at its new location.
409 
410# Example:
411 
412# Redirect permanent /foo http://www.example.com/bar
413 
414#
415 
416# Alias: Maps web paths into filesystem paths and is used to
417 
418# access content that does not live under the DocumentRoot.
419 
420# Example:
421 
422# Alias /webpath /full/filesystem/path
423 
424#
425 
426# If you include a trailing / on /webpath then the server will
427 
428# require it to be present in the URL. You will also likely
429 
430# need to provide a <Directory> section to allow access to
431 
432# the filesystem path.
433 
434#
435 
436# ScriptAlias: This controls which directories contain server scripts.
437 
438# ScriptAliases are essentially the same as Aliases, except that
439 
440# documents in the target directory are treated as applications and
441 
442# run by the server when requested rather than as documents sent to the
443 
444# client. The same rules about trailing "/" apply to ScriptAlias
445 
446# directives as to Alias.
447 
448#
449 
450ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
451 
452</IfModule>
453 
454<IfModule cgid_module>
455 
456#
457 
458# ScriptSock: On threaded servers, designate the path to the UNIX
459 
460# socket used to communicate with the CGI daemon of mod_cgid.
461 
462#
463 
464#Scriptsock logs/cgisock
465 
466</IfModule>
467 
468#
469 
470# "/usr/local/apache2/cgi-bin" should be changed to whatever your ScriptAliased
471 
472# CGI directory exists, if you have that configured.
473 
474#
475 
476<Directory "/usr/local/apache2/cgi-bin">
477 
478AllowOverride None
479 
480Options None
481 
482Order allow,deny
483 
484Allow from all
485 
486</Directory>
487 
488#
489 
490# DefaultType: the default MIME type the server will use for a document
491 
492# if it cannot otherwise determine one, such as from filename extensions.
493 
494# If your server contains mostly text or HTML documents, "text/plain" is
495 
496# a good value. If most of your content is binary, such as applications
497 
498# or images, you may want to use "application/octet-stream" instead to
499 
500# keep browsers from trying to display binary files as though they are
501 
502# text.
503 
504#
505 
506DefaultType text/plain
507 
508<IfModule mime_module>
509 
510#
511 
512# TypesConfig points to the file containing the list of mappings from
513 
514# filename extension to MIME-type.
515 
516#
517 
518TypesConfig conf/mime.types
519 
520#
521 
522# AddType allows you to add to or override the MIME configuration
523 
524# file specified in TypesConfig for specific file types.
525 
526#
527 
528#AddType application/x-gzip .tgz
529 
530#
531 
532# AddEncoding allows you to have certain browsers uncompress
533 
534# information on the fly. Note: Not all browsers support this.
535 
536#
537 
538#AddEncoding x-compress .Z
539 
540#AddEncoding x-gzip .gz .tgz
541 
542#
543 
544# If the AddEncoding directives above are commented-out, then you
545 
546# probably should define those extensions to indicate media types:
547 
548#
549 
550AddType application/x-compress .Z
551 
552AddType application/x-gzip .gz .tgz
553 
554#
555 
556# AddHandler allows you to map certain file extensions to "handlers":
557 
558# actions unrelated to filetype. These can be either built into the server
559 
560# or added with the Action directive (see below)
561 
562#
563 
564# To use CGI scripts outside of ScriptAliased directories:
565 
566# (You will also need to add "ExecCGI" to the "Options" directive.)
567 
568#
569 
570#AddHandler cgi-script .cgi
571 
572# For type maps (negotiated resources):
573 
574#AddHandler type-map var
575 
576#
577 
578# Filters allow you to process content before it is sent to the client.
579 
580#
581 
582# To parse .shtml files for server-side includes (SSI):
583 
584# (You will also need to add "Includes" to the "Options" directive.)
585 
586#
587 
588#AddType text/html .shtml
589 
590#AddOutputFilter INCLUDES .shtml
591 
592</IfModule>
593 
594#
595 
596# The mod_mime_magic module allows the server to use various hints from the
597 
598# contents of the file itself to determine its type. The MIMEMagicFile
599 
600# directive tells the module where the hint definitions are located.
601 
602#
603 
604#MIMEMagicFile conf/magic
605 
606#
607 
608# Customizable error responses come in three flavors:
609 
610# 1) plain text 2) local redirects 3) external redirects
611 
612#
613 
614# Some examples:
615 
616#ErrorDocument 500 "The server made a boo boo."
617 
618#ErrorDocument 404 /missing.html
619 
620#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
621 
623 
624#
625 
626#
627 
628# EnableMMAP and EnableSendfile: On systems that support it,
629 
630# memory-mapping or the sendfile syscall is used to deliver
631 
632# files. This usually improves server performance, but must
633 
634# be turned off when serving from networked-mounted
635 
636# filesystems or if support for these functions is otherwise
637 
638# broken on your system.
639 
640#
641 
642#EnableMMAP off
643 
644#EnableSendfile off
645 
646# Supplemental configuration
647 
648#
649 
650# The configuration files in the conf/extra/ directory can be
651 
652# included to add extra features or to modify the default configuration of
653 
654# the server, or you may simply copy their contents here and change as
655 
656# necessary.
657 
658# Server-pool management (MPM specific)
659 
660#Include conf/extra/httpd-mpm.conf
661 
662# Multi-language error messages
663 
664#Include conf/extra/httpd-multilang-errordoc.conf
665 
666# Fancy directory listings
667 
668#Include conf/extra/httpd-autoindex.conf
669 
670# Language settings
671 
672#Include conf/extra/httpd-languages.conf
673 
674# User home directories
675 
676#Include conf/extra/httpd-userdir.conf
677 
678# Real-time info on requests and configuration
679 
680#Include conf/extra/httpd-info.conf
681 
682# Virtual hosts
683 
684#Include conf/extra/httpd-vhosts.conf
685 
686# Local access to the Apache HTTP Server Manual
687 
688#Include conf/extra/httpd-manual.conf
689 
690# Distributed authoring and versioning (WebDAV)
691 
692#Include conf/extra/httpd-dav.conf
693 
694# Various default settings
695 
696#Include conf/extra/httpd-default.conf
697 
698# Secure (SSL/TLS) connections
699 
700#Include conf/extra/httpd-ssl.conf
701 
702#
703 
704# Note: The following must must be present to support
705 
706# starting without SSL on platforms with no /dev/random equivalent
707 
708# but a statically compiled-in mod_ssl.
709 
710#
711 
712<IfModule ssl_module>
713 
714SSLRandomSeed startup builtin
715 
716SSLRandomSeed connect builtin
717 
718</IfModule>
719 
720<IfModule dir_module>
721 
722DirectoryIndex index.php
723 
724</IfModule>
725 
726<IfModule mime_module>
727 
728AddType text/html php
729 
730AddHandler php5-script php
731 
732AddType application/x-httpd-php-source .phps
733 
734</IfModule>
Setelah konfigurasi apache, restart service apache untuk mejalankan webserver.

1/usr/local/apache2/bin/apachectl restart
Saat ini, jika instalasi berhasil, maka kita sudah mempunyai webserver yang bisa mengeksekusi php. Untuk lebih memastikan, buat halaman web yang menampilkan sistem php yang sedang berjalan pada saat ini.

1vim /usr/local/apache2/htdocs/info.php
isi dari file info.php tersebut

1<?php
2      phpinfo();
3?>

Kemudian akses via web browser arahkan ke http://localhost/info.php, jika muncul tampilan berisi informasi versi dan fitur php yang sedang berjalan, maka anda sukses melakukan instalasi webserver.

http://okagartria.wordpress.com/2011/08/10/install-web-server-di-linux-apache-php/

1 komentar: