Project

General

Profile

Support #385

Updated by Daniel Curtis over 10 years ago

Here is a procedure to install a FAMP, FreeBSD *F*reeBSD with Apache, MariaDB *A*pache, *M*ariaDB and PHP, *P*HP, server. The following setup runs Apache 2.2, 2.4, MariaDB 5.5, and PHP 5 on FreeBSD 9.2. If any version of the packages needs to be changed, replace the versions in the commands accordingly. 

 h2. Pre-Installation Tasks 

 * Before installation of the components, make sure download the compressed snapshot of the ports tree is up to date collection, using the following command: 
 <pre> 
 portsnap fetch 
 </pre> 

 * Now extract the snapshot into @/usr/ports@ using the following command: 
 <pre> 
 portsnap extract 
 </pre> 

 h2. Apache 2.2 – Installation and Configuration 

 * Install Apache 2.2 
 <pre> 
 cd /usr/ports/www/apache22 
 make install 
 </pre> 

 *NOTE*: While running @make install@ the installer asks to check the boxes to install various libraries and support packages. Check the appropriate boxes as per requirements. This is where SuExec and LDAP will be enabled, make sure to enable it. 

 *NOTE*: I use puppet to manage apache configurations and modules, so the above command is used to compile in the above modules. 

 * Clean up the source packages: 
 <pre> 
 make clean 
 </pre> 

 * Edit the apache configuration file, i.e. @/usr/local/etc/apache22/httpd.conf@, and make the following changes: 
 <pre> 
 ServerRoot "/usr/local" 
 ServerAdmin you@your.address 
 ServerName www.example.com:80 
 DocumentRoot "/usr/local/www/apache22/data" 
 Listen :80 
 </pre> 

 * Edit the @/etc/hosts@ file and add the following line: 
 > <ip-address>                    <hostname>.<domain> 
 #* eg: 
 > 192.168.1.1                 hostname.example.org 

 * (Optional) Create a file named /boot/loader.conf or edit it if it is already present and add the following line: 
 > accf_http_load="YES" 

 * Add the following line to @/etc/rc.conf@: 
 > echo 'apache22_enable="YES"' >> /etc/rc.conf 

 * Test the apache server installation using the following command: 
 <pre> 
 /usr/local/sbin/apachectl start 
 </pre> 

 h2. MariaDB – Installation and Configuration 

 Install MariaDB 5.5 
 <pre> 
 cd /usr/ports/databases/mariadb55-server/ 
 make install 
 make clean 
 </pre> 

 * Add the following line to the file @/etc/rc.conf@: 
 <pre> 
 echo 'mysql_enable="YES"' >> /etc/rc.conf 
 </pre> 

 * Start MariaDB 
 <pre> 
 service mysql-server start 
 </pre> 

 * Set password for mysql using the following command 
 <pre> 
 rehash 
 mysqladmin -uroot password 
 </pre>  

 h3. Configure MariaDB 

 * Use the following command: 
 <pre> 
 cp /usr/local/share/mysql/my-small.cnf /etc/my.cnf 
 </pre> 

 Restart mysql using the following commands: 
 <pre> 
 service mysql-server restart 
 </pre> 

 h2. PHP – Installation and Configuration 

 * Install PHP5 and other supporting packages: 
 <pre> 
 cd /usr/ports/lang/php5 
 make config install 
 make install clean 
 </pre> 

 NOTE: I decided compile the FPM module into PHP to replace the old FastCGI method. Make sure to do the same while running @make config@. 

 * Copy the PHP configuration file using the following command 
 <pre> 
 cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini 
 </pre> 

 * Configure the mysql module for PHP: 
 <pre> 
 cd /usr/ports/databases/php5-mysql 
 make config 
 </pre> 

 *NOTE*: Make sure to enable the *mysql* extension 

 * Install the mysql module for PHP: 
 <pre> 
 make install 
 make clean 
 </pre> 

 * Install the php5-session package 
 <pre> 
 cd /usr/ports/www/php5-session 
 make install clean 
 </pre> 

 * Configure the php5-extensions package 
 <pre> 
 cd /usr/ports/lang/php5-extensions 
 make config 
 </pre> 
 *NOTE*: Enable all the modules that will apply to your PHP needs. 

 * Install the php5-extensions package 
 <pre> 
 make install clean 
 </pre> 

 * Install and configuring apache module for PHP: 
 <pre> 
 cd /usr/ports/www/mod_php5 
 make install 
 make clean 
 </pre> 

 * Edit @/usr/local/etc/apache22/httpd.conf@ file and add the following lines: 
 <pre> 
 LoadModule php5_module          libexec/apache22/libphp5.so 

 AddType application/x-httpd-php .php 
 AddType application/x-httpd-php-source .phps 
 </pre> 

 * You should also search for the line that reads: 
 > DirectoryIndex index.html 
 *# and change it to read: 
 > DirectoryIndex index.php index.html 

 * Once completed, a simple call to the apachectl command for a graceful restart is needed to load the PHP module: 
 <pre> 
 apachectl graceful 
 </pre> 

 For future upgrades of PHP, the make config command will not be required; the selected OPTIONS are saved automatically by the FreeBSD Ports framework. 

 The PHP support in FreeBSD is extremely modular so the base install is very limited. It is very easy to add support using the _lang/php5-extensions_ port. This port provides a menu driven interface to PHP extension installation. Alternatively, individual extensions can be installed using the appropriate port. 

 * Now restart the apache server by using the following command: 
 <pre> 
 /usr/local/sbin/apachectl restart 
 </pre> 

 h2. Apache 2.2    Modules – Installation and Configuration 

 There are many modules that apache can use, the following are just a few that I use in my web server baseline. For more modules, consult the FreeBSD handbook. 

 * *-(Optional) # *(Optional) Install PHP FastCGI as a PHP replacement-* replacement* 
 -Install Install @www/mod_fcgid@ from ports. In httpd.conf use:- use: 
 <pre> 
 cd /usr/ports/www/mod_fcgid 
 make install clean 
 </pre> 
 *# -Now #* Now replace the PHP module used by Apache with the FastCGI module:- module: 
 <pre> 
 LoadModule fcgid_module libexec/apache22/mod_fcgid.so 

 <IfModule mod_fcgid.c> 
   AddHandler fcgid-script .fcgi 
 </IfModule> 
 </pre> 

 -If If all went well you should be able to restart Apache and be in business. A @phpinfo();@ should execute and provide details. Any problems the quickest way to check PHP is to just execute @php -v@ at a shell prompt. If it doesn't segfault it will print out a short descriptive output text. 

 I believe this is better than the usual script based approach you will  
 locate on the web. It starts/spawns PHP as a long running process when  
 Apache starts instead of starting a new CGI each time PHP script is  
 executed. The mod_fcgid is configurable.- configurable. 


 # *Install the Phusion Passenger module* 
 <pre> 
 cd /usr/ports/www/rubygem-passenger 
 make install clean 
 passenger-install-apache2-module 
 </pre> 
 #* Then add the module in @/usr/local/etc/apache22/httpd.conf@: 
 <pre> 
 LoadModule passenger_module /usr/ports/www/rubygem-passenger/work/passenger-4.0.41/buildout/apache2/mod_passenger.so 
 <IfModule mod_passenger.c> 
   PassengerRoot /usr/ports/www/rubygem-passenger/work/passenger-4.0.41 
   PassengerDefaultRuby /usr/local/bin/ruby19 
 </IfModule> 
 </pre> 
 #* And now ruby web applications can be used by using the following template: 
 <pre> 
 <VirtualHost *:80> 
    ServerName www.yourhost.com 
    # !!! Be sure to point DocumentRoot to 'public'! 
    DocumentRoot /somewhere/public     
    <Directory /somewhere/public> 
       # This relaxes Apache security settings. 
       AllowOverride all 
       # MultiViews must be turned off. 
       Options -MultiViews 
    </Directory> 
 </VirtualHost> 
 </pre> 

 # *Install the Perl module* 
 <pre> 
 cd /usr/ports/www/mod_perl2 
 make install clean 
 </pre> 
 #* Add the following to @/usr/local/apache22/httpd.conf@: 
 <pre> 
 LoadModule perl_module /usr/local/libexec/apache22/mod_perl.so 
 </pre> 

 h2. Resources 

 * http://fosskb.wordpress.com/2014/04/12/famp-installing-apache2-4-mariadb-php-on-freebsd-10/ 
 * http://www5.us.freebsd.org/doc/handbook/network-apache.html 
 * http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html 
 * http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html

Back