Sunday, July 8, 2007

Very basic FAMP (Freebsd, Apache, MySQL, PHP)

As you may already know, I installed FreeBSD 6.2 last night. One of the primary uses of my machine is a web server complete with PHP and MySQL.

Installation is pretty straight-forward.

Apache 2.2

cd /usr/ports/www/apache22
# If you don't know where it is just do a make search name=apache22
make install clean
mkdir -p /var/www/htdocs
chgrp -R www /var/www/htdocs
chown -R www /var/www/htdocs
# Let anybody in the group add new documents
chmod g+w -R /var/www/htdocs
# Add myself to the www group
pw user mod my_user -G www
# Let's configure the damn thing
vi /usr/local/etc/apache22/httpd.conf
# Changed DocumentRoot to /var/www/htdocs
# Changed ServerName to http://my_private_adress:80
# Changed ServerAdmin to my email address
# Changed AllowOverride All (as
# anybody can modify their directories with .htaccess)
# Now let's start it
/usr/local/sbin/apachectl start
# And add it to /etc/rc.conf to start automatically on every
# system boot.

echo 'apache22_enable="YES"' >> /etc/rc.conf

MySQL 5.1

cd /usr/ports/databases/mysql51-server
make install clean
# Let us start the database installer
mysql_install_db --user=mysql
chown -R mysql /var/db/mysql/
chgrp -R mysql /var/db/mysql/
/usr/local/bin/mysqld_safe -user=mysql &
# Start it every time the system starts
echo ‘mysql_enable=”YES”‘ >> /etc/ rc.conf
# Change the root password
mysqladmin -u root password newpassword
# Copy the config file
cp /usr/local/share/mysql/my-small.cnf /var/db/mysql/my.cnf
# Add another user (it's generally not a good idea to work with
# root)

mysql -u root -pnewpassword
mysql> GRANT ALL PRIVILEGES ON *.* to 'user'@'localhost' IDENTIFIED BY 'another_password'
mysql> quit

PHP 5.2

cd /usr/ports/lang/php5
make install clean
# You have to choose the option for Apache 2.2
cd /usr/ports/lang/php5-extentions
make config
# Choose Mysql, GD, Zlib, PDF, Mbstring and any other extension you would like
make install clean
# Apache configuration
# After all the LoadModule lines add:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php
# Uncomment the Include etc/apache22/extra/httpd-userdir.conf line
# You now have a per-user web account in /home/user/public_html
# Restart apache
apachectl restart

All is done. Have fun. By the way, you can copy stuff from the outside with scp (you did remember to let ssh connection didn't you?)


Stephen Reese said...

You have some great BSD tutorial on your blog. Keep up the great work!

Anonymous said...

First of all - THANKS !
That's exactly what needed, simple step-by-step guide without long explanation for noobs.

I just want to add a little bit:
(Installed yesterday Apache+MySQL+PHP on FreeBSD 7.0-release.)

1. Apache:
After ”apachectl start”
got error ”(2)No such file or directory: Failed to enable the ‘httpready’ Accept Filter

Problem resolved as:
Add string [accf_http_load=”YES”] to ”/boot/loader.conf”
and start manually 'kldload accf_http'

If someone want to host multiple sites on server, it's good idea to uncomment string in httpd.conf:
# Virtual hosts
Include etc/apache22/extra/httpd-vhosts.conf

2. MySQL 5.1
mysqladmin -u root password newpassword

should be:
mysqladmin -u root password ”newpassword”
where ”newpassword” - new MySQL root password.

GRANT ALL PRIVILEGES ON *.* to 'user'@'localhost' IDENTIFIED BY 'another_password'

should be:
GRANT ALL PRIVILEGES ON *.* to 'user'@'localhost' IDENTIFIED BY 'another_password';

(;) - on the end.

3. PHP
cd /usr/ports/lang/php5-extentions

should be:
cd /usr/ports/lang/php5-extensions