How to install a LAMP Server
From NetBSD Wiki
LAMP is a an acronym for a combined set of software to run a Webserver containig following Software Products: Apache, Mysql Perl, Python or PHP. The "L" stands for Linux, therefore there is also an acronym named WAMP representing the Windows Operating System. This also means that the title of this article is misleading. The approach is to install the same combined set of software, but using NetBSD as the Operating System instead of Linux.
We will install all components using pkgsrc, building all packages from source. An installation using existing binaries provided by ftp.netbsd.org is not possible.
Contents |
Requirements
The pkgsrc source tree will need a minimum of 200 MB disk space. Pkgsrc is the main package management framework for NetBSD. It will provide all necessary software you need.
Preparing pkgsrc
Creating the pkgsrc directory
This is a matter of taste, but most people create the directory to /usr/pkgsrc/
# mkdir /usr/pkgsrc
Change owner of pkgsrc to a user, so you can update later it with user rights.
# chown john pkgsrc
Obtaining the current pkgsrc source tree
For being able to use the pkgsrc framework, you have to get the pkgsrc tree first. The recommended way is to download a current tar package of the tree via ftp, then extract it and install csup, a leightweight cvsup protocol client written in C, to keep in sync.
Downloading the tar file
The latest compressed pkgsrc tar package is always available on the NetBSD ftp servers and its mirrors.
$ cd $ ftp -a ftp://ftp.netbsd.org/pub/NetBSD/packages/current-src/pkgsrc.tar.gz
and extract into /usr
$ tar xvfz pkgsrc-current.tar.gz -C /usr
Installing csup
We will keep our tree in sync with csup, therefore we have to install it first.
# cd /usr/pkgsrc/net/csup
and the three magic words
# make install clean
Configuring csup
csup needs one configuration file, the supfile, which contains the information to sync what from which server to where. Please create a file called pkgsrc-supfile in your home directory which contains:
*default tag=. *default release=cvs *default delete use-rel-suffix *default umask=002 *default host=cvsup.se.netbsd.org *default base=/home/john *default prefix=/usr netbsd-pkgsrc
base points to your home directory and prefix to the directory, where the repo goes.
To get and to keep your pkgsrc tree in sync, just run:
$ csup pkgsrc-supfile
If you wish to update regularly, say once a day, use cron to do that for you.
Creating WRKOBJDIR
To keep pkgsrc clean and your work directories out of pkgsrc, define WRKOBJDIR in /etc/mk.conf and add:
WRKOBJDIR=/usr/work
and then create that directory.
# mkdir /usr/work
Creating DISTDIR
We also want our distfiles to be stored, outside of the pkgsrc directory. Therefore we add the DISTDIR variable to /etc/mk.conf
DISTDIR=/usr/distfiles
and create it with:
# mkdir /usr/distfiles
To install packages, we need to become root.
Installing the Apache webserver
You can now begin installing software. It's pretty simple.
The new Apache 2.2 server comes with two different threading models from which prefork is installed by default. It is not recommended to use the Worker model, if you wish to use Apache and PHP. As that is the case, we will install a default Apache 2.2 server.
# cd /usr/pkgsrc/www/apache22 # make install clean clean-depends
This will install the Apache 2.2 server and all it's dependencies. The package currently depends on 10 other packages like perl, gmake and libtool to name a few. All dependencies are build before the Apache webserver is build, otherwise it wouldn't be dependencies.
If your build was successful, you should now edit the Apache configuration file /usr/pkg/etc/httpd/httpd.conf to fit your needs. At least set the Listen Attribute and your Servername. Please ensure that if your machines hostname does not globally resolve, to put it into your /etc/hosts file, otherwise Apache will refuse to start.
If you wish to start the Apache webserver at boottime, please copy the rc.d example script from /usr/pkg/share/examples/rc.d/apache to /etc/rc.d and then add apache=yes to your /etc/rc.conf file.
# cp /usr/pkg/share/examples/rc.d/apache /etc/rc.d
If you want to copy the rc.d scripts automatically with pkgsrc, you can use:
PKG_RCD_SCRIPTS=YES
in your /etc/mk.conf
You could now start, stop and restart the Apache Webserver using apachectl or using boot script /etc/rc.d/apache
To start the Server enter:
# apachectl start
or
# /etc/rc.d/apache start
To stop the server, substitute start with stop. If you're running a production server, pay attention to the apachectl graceful option.
Installing MySQL
You can skip this part, if you don't want to install a MySQL Server. To install the MySQL Server enter:
# cd /usr/pkgsrc/databases/mysql5-server # make install clean clean-depends
This will install the mysql server and all it's dependencies, like the mysql client.
Configuring the MySQL server
Please copy the example start script to /etc/rc.d
# cp /usr/pkg/share/examples/rc.d/mysqld /etc/rc.d
and add mysqld=yes to your /etc/rc.conf
You can now start, stop and restart the MySQL server using
# /etc/rc.d/mysqld start
to start and respectively stop and restart.
The default mysql server database root password is empty. For security reasons, you should set your root password as soon as possible.
You can pass most of the Options to the Server via the file /etc/my.cnf. If you want the Server to listen only on localhost, for instance, create /etc/my.cnf and add
[mysqld] port=3306 bind-address=127.0.0.1
and restart your mysql server. To check, if your mysql server is really listening only on localhost, use sockstat.
# sockstat -l
For much more Options, consider reading the MySQL Documentation.
Installing the PHP Module for Apache
# cd /usr/pkgsrc/www/ap-php # make install clean
This will install by default the latest Version of PHP 5.x and the PHP5 Module for Apache 2.2
Configuring PHP
You should now add the Module and the PHP Handlers to your Apache Configuration File /usr/pkg/etc/httpd/httpd.conf
Add following lines:
LoadModule php5_module /usr/pkg/lib/httpd/mod_php5.so
and
AddType application/x-httpd-php .php
and if you wish
DirectoryIndex index.html index.php
Installing the MySQL module for PHP
This step is important and enables you to make mysql database connections from your php skript.
cd /usr/pkgsrc/databases/php-mysql/ make install clean
Now edit /usr/pkg/etc/php.ini and add the line
extension=mysql.so
You need this to enable mysql functions in your php module.
Now restart your Apache webserver.
To test, if PHP is working, create a small file called test.php in your document root directory, which is by default /usr/pkg/share/httpd/htdocs, containing only one line with the function phpinfo().
<?php phpinfo(); ?>
if you use php5 and wish to use short tags like <? phpinfo() ?>, then edit your /usr/pkg/etc/php.ini file and change option short_open_tag = Off to On to make this line working. In PHP5 short_open_tag is off by default.
Open your browser and point it to this url:
http://127.0.0.1/test.php
You should now see a website with information regarding your PHP installation and a table named mysql, in the middle of the document, with mysql informations.
That's it. You can now install software like a phpMyAdmin, or a Wiki. Have Fun.
