Category Archives: php5

Install and Configure Apache Web Server and PHP5 in FreeBSD

Apache WebServer is well-known as robust and stable web server. Currently, most of web server use Apache WebServer. Apache 2.2 is simple yet having lots of modules available. Accompanied by PHP5 scripting language, you can have a good web server and ready to run any web applications.

In this article we will discuss about how to install and configure Apache and PHP5 for FreeBSD. For this, I use FreeBSD 8.3 amd64, but you can have any FreeBSD version and platform as you please. The Apache WebServer we use would be 2.2 as provided on FreeBSD ports collection.

You might also install MySQL before

Apache Web Server

Installing the port

Installing Apache2.2 is as easy as installing any ports. Do the following commands:

cd /usr/ports/www/apache22
make install

If you are going to do fresh installation, you will be prompted by installation options menu. The configuration I choose are list here:

  1. THREADS (Enable threads support in APR)
  2. MYSQL (Enable MYSQL support for apr-dbd
  3. PGSQL (Enable PostgreSQL support for apr-dbd)
  4. SQLITE (Enable SQLite support for apr-dbd)
  5. IPV6 (Enable IPv6 support)
  6. BDB (Enable BerkelyDB dbm)
  7. AUTH_BASIC (mod_auth_basic)
  8. AUTH_DIGEST (mod_auth_digest)
  9. AUTHN_ANON (mod_authn_anon)
  10. AUTHN_DBM (mod_authn_dbm)
  11. AUTHN_DEFAULT (mod_authn_default)
  12. AUTHN_FILE (mod_authn_file)
  13. AUTHZ_DBM (mod_authz_dbm)
  14. AUTHZ_DEFAULT (mod_authz_default)
  15. AUTHZ_GROUPFILE (mod_authz_groupfile)
  16. AUTHZ_HOST (mod_auth_host)
  17. AUTHZ_OWNER (mod_auth_owner)
  18. AUTHZ_USER (mod_auth_user)
  19. CACHE (mod_cache)
  20. DISK_CACHE (mod_disk_cache)
  21. FILE_CACHE (mod_file_cache)
  22. DAV (mod_dav)
  23. DAV_FS (mod_dav_fs)
  24. LDAP (Enable mod_ldap)
  25. ACTIONS (mod_actions)
  26. ALIAS (mod_alias)
  27. ASIS (mod_asis)
  28. AUTOINDEX (mod_autoindex)
  29. CERN_META (mod_cern_meta)
  30. CGI (mod_cgi)
  31. CHARSET_LITE (mod_charset_lite)
  32. DEFLATE (mod_deflate)
  33. DIR (mod_dir)
  34. DUMPIO (mod_dumpio)
  35. ENV (mod_env)
  36. EXPIRES (mod_expires)
  37. HEADERS (mod_headers)
  38. IMAGEMAP (mod_imagemap)
  39. INCLUDE (mod_include)
  40. INFO (mod_info)
  41. LOG_CONFIG (mod_log_config)
  42. LOGIO (mod_logio)
  43. MIME (mod_mime)
  44. MIME_MAGIC (mod_mime_magic)
  45. NEGOTIATION (mod_negotiation)
  46. REWRITE (mod_rewrite)
  47. SETENVIF (mod_setenvif)
  48. SPELING (mod_speling)
  49. STATUS (mod_status)
  50. UNIQUE_ID (mod_unique_id)
  51. USERDIR (mod_userdir)
  52. USERTRACK (mod_usertrack)
  53. VHOST_ALIAS (mod_alias)
  54. FILTER (mod_filter)
  55. VERSION (mod_version)
  56. SSL (mod_ssl)
  57. REQTIMOUT (mod_mod_reqtimeout)

Once the installation finish, you will have a new script at your /usr/local/etc/rc.d named apache22. You can use /usr/local/etc/rc.d/apache22 or apachectl to start the server. But before that, you need to add an enable line for Apache to your  /etc/rc.conf file:

apache22_enable="YES"

Configuring Apache’s httpd.conf

The httpd.conf contains most all of the important configuration settings. Everything can be done here from adding virtual hosts, to setting the log files, to setting .htm files to be parsed for php. You can open the httpd.conf file for editting with

ee /usr/local/etc/apache22/httpd.conf

The first thing you will need to change in the file to get your server going is the ServerName to the hostname you want to use for the server. If you do not have one you can use the IP address, or localhost. This will need to be followed by the port number.

ServerName www.yourdomain.com:80

Which in my case I use:

ServerName cluster0.xathrya.id:80

Currently in Apache 2.2.x the default directory is /usr/local/www/apache22 instead of the old default. You can change to the standard directory of /usr/local/www/data if you want. Just replace any /usr/local/www/apache22 with /usr/local/www/data on the file and then move the folder like this if you do not have a previous version of apache installed that has created the directory already.

mv /usr/local/www/apache22 /usr/local/www/

You can replace it easily with sarep from the ports with this command.

sarep "/usr/local/www/apache22" "/usr/local/www" httpd.conf

This is enough to get the server going so that you can check that it will run, so do that now.

Starting Apache

Apache is controlled with apachectl, some examples of its usage are

apachectl start
apachectl restart
apachectl graceful
apachectl stop

The graceful option has the same result as restart, it just does it in a nice way as opposed to restart forcefully restarting the server.

Before restarting or starting Apache it is best to run the configtest to check for errors in the httpd.conf

apachectl configtest

If this comes back OK then you are good to go. When starting Apache it will not tell you if the start was successful, the easiest way to check this is to restart it. If apache failed to start up previously it will tell you that Apache is not running when you perform the restart.

Once your apache  won’t start for unknown reason, you can check the logs and find the error messages. The log is located at /var/log/messages and you can check by

tail /var/log/messages
tail /var/log/httpd-error.log

To check if you can get to the server just point your web browser to the machine and you should get a message telling you that Apache has been successfully installed.

Turning on the defaults

Many of the default settings are now included in a seperate file and turned off default. To use them unhash this part of your httpd.conf file.

# Various default settings
Include etc/apache22/extra/httpd-default.conf

Common Errors

The most common error when setting up Apache is the “cannot determine local host name” error. This error is caused by the hostname resolving to a different IP than the one it has. To check what your current hostname is use hostname. Then use nslookup on the hostname to get the IP and compare it to the IP that your machine is actually using with ifconfig. For example:

# hostname
	server.mydomain.com

# nslookup server.mydomain.com
	Non-authoritative answer:
	Name:   server.mydomain.com
	Address: 10.1.1.30

# ifconfig
	inet 192.168.0.5 netmask 0xffffff00

We can see here that the IP of the hostname does not match the real IP of the machine. A quick fix for this is to just add the hostname to your /etc/hosts file.

# ee /etc/hosts
	192.168.0.5	server.mydomain.com.

This will set the hostname to the IP assigned to your machine. Make sure you do not forget to put a . on the end when adding this line!

Another very common error is this one

[warn] (2)No such file or directory: Failed to enable 
the 'httpready' Accept Filter

It is caused by not having the accf_http kernel module loaded. Loading it is explained above.

Password Protecting Directories

Directories are set to password protected in the the httpd.conf file also. See the tutorial on password protecting directories with htaccess in Apache

Encrypting Traffic with SSL

The data moving between the user and your server well be plain text unless you use encrypt it. See the tutorial on setting up SSL with Apache 2.

PHP5

Installing PHP5 is as easy as installing any ports. Do the following commands:

cd /usr/ports/lang/php5
make install

You can configure as you like. As a reference here is my configuration:

  1. CLI (Build CLI version)
  2. CGI (Build CGI version)
  3. APACHE (Build Apache module)
  4. SUHOSIN (Enable Suhosin protection system)
  5. IPV6 (Enable ipv6 support)
  6. LINKTHR (Link thread lib (for threaded extensions))

Make sure APACHE is ticked!

Now install PHP5 extensions by

cd /usr/ports/lang/php5-extensions
make install

Again, configuration is yours. Here is my list:

  1. BCMATH (bc style precision math functions)
  2. BZ2 (bzip2 library support)
  3. CTYPE (ctype functions)
  4. CURL (CURL support)
  5. DOM (DOM support)
  6. FILTER (input filter support)
  7. GD (GD library support)
  8. GMP (GNU MP support)
  9. HASH (HASH Message Digest Framework)
  10. ICONV (iconv support)
  11. JSON (JavaScript Object Serialization support)
  12. LDAP (OpenLDAP support)
  13. MYSQL (MySQL database support)
  14. PDO (PHP Data Objects Interface (PDO))
  15. PDO_SQLITE (PDO sqlite driver)
  16. PGSQL (PostgreSQL database support)
  17. PHAR (phar support)
  18. POSIX (POSIX-like functions)
  19. SESSION (session support)
  20. SIMPLEXML (simplexml support)
  21. SQLITE (sqlite support)
  22. SQLITE3 (sqlite3 support)
  23. TOKENIZER (tokenizer support)
  24. WDDX (WDDX support (implies XML))
  25. XML (XML support)
  26. XMLREADER (XMLReader support)
  27. XMLWRITER (XMLWriter support)
  28. ZIP (ZIP support)
  29. ZLIP (ZLIB support)

Make configuratio file for PHP, invoke this command:

cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

Then edit Apache’s configuration file on /usr/local/etc/apache22/httpd.conf and add these:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .php

In DirectoryIndex add php extention so it would be something like this:

DirectoryIndex index.html index.php

Edit the Languageconfiguration file (/usr/local/etc/apache22/extra/httpd-languages.conf) and add the following lines:

AddDefaultCharset On

Now restart apache (or start if you don’t start it yet)

/usr/local/etc/rc.d/apache22 restart

The post Install and Configure Apache Web Server and PHP5 in FreeBSD appeared first on Xathrya.ID.