Installing MySQL + Apache + PHP on FreeBSD (the same LAMP web server, but on FreeBSD)

Practice



Task: the well-known LAMP stack (whose first letter stands for Linux) needs to be set up on FreeBSD. A sort of FAMP, if you will )))


Let's get started.
Installing MySQL + Apache + PHP on FreeBSD (the same LAMP web server, but on FreeBSD)

MySQL

First, let's install the MySQL server.

$ cd /usr/ports/databases/mysql51-server
$ sudo make install clean


MySQL doesn't ask you for any configuration during the build - it just installs. Let's be patient - building a database server takes a while.

After the installation is done, let's also install the scripts:

$ cd /usr/ports/databases/mysql51-scripts
$ sudo make install clean


Add the following line to the /etc/rc.conf file:

mysql_enable="YES"


And start the MySQL server:

$ sudo /usr/local/etc/rc.d/mysql-server start


Now let's run the security script, where we set the root password (by default the password is empty) and answer "yes" to the remaining questions.

$ sudo mysql_secure_installation


Apache

Next up - installing the Apache web server.

$ cd /usr/ports/www/apache22
$ sudo make install clean


And here we're already offered a configurator to choose a number of options. Most of them are "mods" - mod_.

I won't give the full list here - it can be a bit different for everyone. I'll just say that, in addition to the default options, I recommend enabling:

  • MySQL: for MySQL support in apr-dbd
  • mem_cache: if your webmasters use mem_cache
  • ldap: if you plan to use the mod_lpad module for the LDAP directory


And be sure to check that mod_rewrite (rewrite) is enabled - one of the tastiest bits of Apache: the Rewrite Engine - and also check that SSL is enabled, just in case you end up using a secure channel for some sites.

Apache also takes a while to install.

Now let's add the following line to the /usr/local/etc/apache22/httpd.conf file:

ServerName "MyWebServer"


where instead of MyWebServer you specify the name of this server (whatever you want the server to be called in Apache's messages and the pages it generates).

After the installation, add the following to the /etc/rc.conf file:

apache22_enable="YES"
apache22_httpd_accept="YES"


And let's start the server (still with a bare configuration):

$ sudo /usr/local/etc/rc.d/apache22 start


Installing PHP 5.3.x

Now let's install the latest available version of PHP (on FreeBSD, new versions appear fairly promptly).

$ cd /usr/ports/lang/php5
$ sudo make install clean


Note. The "php5" directory contains the latest version - PHP 5.3; the "php52" directory contains the latest version of the 5.2 branch - 5.2.17.

In the configurator, we select the "Apache" option so that the PHP installer automatically adds its module for loading into Apache, and continue the installation.


Installing PHP extensions

PHP alone isn't enough - most sites use its additional modules (no need to look far - our own site uses them). For example, even iconv, gd, or imagick... the list goes on.

So, let's install the PHP extensions.

$ cd /usr/ports/lang/php5-extensions
$ sudo make install clean


Here's a list of what I recommend enabling. Besides this, of course, you can add whatever you need on your server (on my servers the list is longer):

  • BZ2
  • CTYPE
  • CURL
  • DOM
  • FILTER
  • GD
  • GETTEXT
  • HASH
  • ICONV
  • IMAP
  • JSON
  • MBSTRING
  • MCRYPT
  • MYSQL
  • MYSQLI
  • OPENSSL
  • PDO
  • PDO_SQLITE
  • POSIX
  • SESSION
  • SIMPLEXML
  • SQLITE
  • SQLITE3
  • TOKENIZER
  • XML
  • XMLREADER
  • XMLWRITER
  • ZIP
  • ZLIB


Webmasters and developers will certainly ask for whatever's missing for PHP, but the suggested list is a sort of universal starter kit - not complete, but no longer empty either.

That's it, click OK and install.

It installs even longer than MySQL, constantly pestering the admin with questions about the configuration of accompanying packages (from dependencies). So don't rush off for a coffee or tea break - it'll still put your brain through the wringer.


Finishing Apache's configuration for PHP

Even though we enabled the "Apache" option when building PHP, we still need to tweak the httpd.conf config file a little, located at:

/usr/local/etc/apache2/httpd.conf

Now let's find the section:


...


and just before the closing "" we insert the following lines:

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


Also, at the end of the configuration file, add the directive:

NameVirtualHost *:80


After which come the site descriptions, like:


ServerName mysite.ru
ServerAlias www.mysite.ru
DirectoryIndex index.php
DocumentRoot /www/mysite.int


(this, of course, is just an example)


And let's restart Apache:

$ sudo /usr/local/etc/rc.d/apache2 restart


SSL for Apache on FreeBSD

Applies to: FreeBSD 6.x+

See also

  • [[b5820]]
  • [[b11050]]
  • [[b11062]]
  • [[b5894]]
  • [[b5895]]
  • [[b889]]
  • [[b9]]
  • [[b6486]]

See also

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)