How to setup Apache, Php, Mysql and PhpMyAdmin on FreeBSD 9
22-06-2014

How to setup Apache, Php, Mysql and PhpMyAdmin on FreeBSD 9
I'm starting this off with a fresh FreeBSD 9.0 install if you don't know how to install FreeBSD have a look at this tutorial I made on installing FreeBSD here. First thing to do is make sure your ports tree is up to date, make sure you are root and type in the command below.
portsnap fetch extract
Now it's time to install php run the command below to install php.
cd /usr/ports/lang/php5 make install clean
There will be a few dialog box's that pop up asking what we want to compile php with, you can select/unselect anything you do or don't want, next we'll install apache, run the commands below.
cd /usr/ports/www/apache22 make install clean
In the next dialog box you don't need to change anything, so select/unselect anything you want or don't want, if you're unsure just press enter. It's very easy to change it in the future, the default will work with most common things but it is a good idea to try and make it lighter in the future by removing parts you don't need. A good way of testing what you need is to comment out modules in the httpd.conf file and restarting Apache to check if everything still works or not it's good to have a play about with it but do that after this tutorial after we have everything setup. Next install the php apache module with the commands below.
cd /usr/ports/www/mod_php5 make install clean
Now it's time to do some configuration the first part we want to configure is part of FreeBSD, type in the command below.
vi /etc/hosts
If your hosts file is the default from the install it should look like mine in the image below.
A bored coder
If you're setting this up on a server where is says .my.domain change that to your domain name for the parts that start with ::1 and 127.0.0.1 so it looks like this example "localhost.aboredcoder.com". Now it's time to configure Apache, run the command below.
vi /usr/local/etc/apache22/httpd.conf
The first part of this file you need to change is where it says Listen 12.34.56.78:80 change that to your IP :80 the :80 stands for the port you want Apache to listen on if you want to only run this locally for programming or something just set it to listen to 127.0.0.1:80 this will make Apache only visible to the PC you're on. After that line where it says Listen 80 you can remove that or just hash it out like I have done in the image below.
A bored coder
The next part we need to change is the part where is says "ServerAdmin [email protected]" just change the [email protected] to your admin email address.The next thing we need to change is the part where it says "ServerName www.example.com" change this to your domain name if you are running this locally only you can just leave it as it is, or set it to localhost. Now the next part we need to change is where it says "DirectoryIndex index.html" add index.php in there like in the image below.
A bored coder
Now go down close to the bottom of the file and look for where is says;
Include etc/apache22/extra/httpd-languages.conf
Remove the # like I have in the image below.
A bored coder
Now go right to the bottom of the file and add in the following.
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
It should look like mine dose in the image below, exit and save the httpd.conf.
A bored coder
Now put in the command below to use one of the default php.ini files.
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
Everything should work now, we can test everything is working type "service apache22 onestart" and then make a index.php file so we can test it is all working so type in the command below.
vi /usr/local/www/apache22/data/index.php
In that file add
<?php echo phpinfo(); ?>
Save that file and then in a browser go to the ip of the server you're setting up or if you are on the machine you're setting up go to localhost and if should look something like mine dose in the image below.
A bored coder
Congratulations you've setup a web server now we'll go onto the mysql setup so first we need to install it type in the commands below to install mysql.
cd /usr/ports/databases/mysql55-server make BATCH=yes install clean
Now start mysql by typing "service mysql-server onestart" Now we can run the mysql secure installation script it's script to setup mysql fast and secure so type in the command below.
mysql_secure_installation
This script is really strait forward just follow it through and then mysql is setup. Now it's time to install php extensions so run the command below. In the dialogs pick whatever extension you need.
cd /usr/ports/lang/php5-extensions make install clean
Right now it is time to install phpMyAdmin we're close to done phpMyAdmin is the last part of this tutorial so run the command below to install phpMyAdmin.
cd /usr/ports/databases/phpmyadmin make BATCH=yes install clean
Now we need to configure phpMyAdmin there is not too much configuration to do so we're almost done. Type in the command below.
vi /usr/local/www/phpMyAdmin/config.inc.php
I n this file add what I put below this but make sure you change the blowfish part for better security just type some random letters, or generate a random string here
$cfg['blowfish_secret']='lYSRWfJ39TOgo3GkgHo7vHcO'; $i=1; $cfg['Servers'][$i]['connect_type']='socket'; $cfg['Servers'][$i]['auth_type']='cookie'; $cfg['Servers'][$i]['extension']='mysqli';
Make sure it's in the php tags like in the image below.
A bored coder
Now for the last part we have to add phpMyAdmin into apache so you can access it run the command below to edit the httpd.conf again.
vi /usr/local/etc/apache22/httpd.conf
Now go right to the bottom of the file and add in the following.
#phpmyadmin
alias /phpmyadmin "/usr/local/www/phpMyAdmin" <Directory "/usr/local/www/phpMyAdmin"> Order deny,allow Allow from all </Directory>
Here's a image of it in my configuration, you should probably comment this out when you're not using it also you should change the alias part to something like /database but use something that other people wont think of just so it is a bit harder for other people to find.
A bored coder
Now we just need to add mysql and apache to startup so run the command below.
vi /etc/rc.conf
In there add the following 2 lines.
mysql_enable="YES" apache22_enable="YES"
A bored coder
Now we're done to show php files on the server you can put them in "/usr/local/www/apache22/data" that's Apache's default directory you can change this in the httpd.conf file if you want to, if you get stuck on anything post it in the comments below and I'll try to help you.

Name:
*

Email:
*

Website:





Page generated in 0.0033 seconds.