FreeBSD Server Administration

« November 2004 | Main | March 2005 »

January 21, 2005

Install Awstats on FreeBSD

Warning: If you are using Awstats 6.2 or earlier version, your server is at great risk, update it now! More information.


I have separated the log files for all virtual hosts, now it's time to install a log analyzer. I used to use offline software, but as the website growing, a faster and more convenient way is a online program.

1. Install Awstats from ports
# cd /usr/ports/www/awstats
# make install
It seems that "make install" only did a download.

2. Config Awstats
Find the file "config.pl", it's under /usr/ports/www/awstats/work/awstats-6.1/tools on my server, run
# ./config.pl
It suggests that the config script should run from /usr/local/awstats, then I copied the whole directory to /usr/local/etc/awstats/
# cp -Rv /usr/ports/www/awstats/work/awstats-6.1/ /usr/local/etc/awstats
(Note: the directory is different for different Awstats versions.)

# chmod +x /usr/local/etc/awstats
(Give Apache permission)

# /usr/local/etc/awstats/tools/configure.pl
This script will ask you a few questions to config Apache, follow the instructions and a config file for this domain will be created. It's better to save the output for reference, many useful information.

3. Modify generated Awstats config file
The config file is generated under /etc/awstats/, named like awstats.www.domain.com.conf
It's necessary to modify this file, the important parameters are:
LogFile="/var/log/domain.com-access_log"
DirData="/var/log/awstats"
(You need to create this directory)

4. Restart Apache
# /usr/local/sbin/apachectl restart

5. Generate stats
This step can be done from the web if you enable AllowToUpdateStatsFromBrowser in the file awstats.www.domain.com.conf.
# /usr/local/etc/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.domain.com
If everything goes well, it will dispaly something like:

Update for config "/etc/awstats/awstats.www.domain.com.conf"
With data in log file "/var/log/domain.com-access_log"...
Phase 1 : First bypass old records, searching new record...
Direct access after last parsed record (after line 74)
Jumped lines in file: 74
Found 74 already parsed records.
Parsed lines in file: 0
Found 0 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 3 new qualified records.

6. Test the result
Visit http://www.domain.com/awstats/awstats.pl?config=www.domain.com
Get "permission error"? Check if Apache can read the directory /usr/local/etc/awstats/.

7. Config other domains
Fortunately, you don't have to repeat the config process for all virutal hosts, copy the previous config file and modify the parameters as step 3.

8. Add to cron job
# crontab -e
Insert the following line, Awstats will update at 00:01 everyday.
01 0 * * * /usr/local/etc/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.domain.com
02 0 * * * /usr/local/etc/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.otherdomain.com
(more info about cron job)

Awstats has a command to update all domains, but I wasn't able to run it.
# /usr/local/etc/awstats/tools/awstats_updateall.pl now

Awstats also supports update from the web, I prefer this way - generate the stats only when I need it, add the following line in the file awstats.www.domain.com.conf.

AllowToUpdateStatsFromBrowser=1

9. Protect the Awstats Report
You might want to protect the awstats report, refer to "how to protect a directory under Apache". Note that you must modify Apache configuration file.


Category : Apache

Posted by FreeBSD Newbie at 07:22 AM | Comments (2)

Separate Log Files for Virutal Host

To have dedicated log file for each virtual host, add the following directive to VirtualHost directive in Apache config file: (FreeBSD)

ErrorLog /var/log/error_log_file_name
CustomLog /var/log/log_filename combined

The complete VirtualHost entry looks like:
<VirtualHost 1.2.3.4>
ServerAlias www.domain.com domain.com
ServerAdmin admin@domain.com
DocumentRoot /home/domain/www
ServerName www.domain.com
ErrorLog /var/log/error_log_file_name
CustomLog /var/log/log_filename combined
</VirtualHost>

Make sure to check the config file before restarting Apache:
# /usr/local/sbin/httpd -t


Category : Apache

Posted by FreeBSD Newbie at 01:45 AM | Comments (0)

January 15, 2005

register_globals

In my last post, I wrote: "PHP doesn't need any configuration if installed with pkg_add", well, this is not accurate, I found several php scripts worked weird, that reminded me of the famous parameter register_globals. It's off by default, pkg_add doesn't create php.ini, if you want to enable register_globals, you have to create a php.ini and modify the configuration, then restart Apache.

# cp /usr/local/etc/php.ini-recommended /usr/local/etc/php.ini

In PHP documentation, there is no configuration guide specially for FreeBSD, there are two related sections: OpenBSD and Unix installation, they both use different place for php.ini.

Update: I deleted the comments of this post accidentally and lost some useful information.

The easiest way to know where to put php.ini is creating a php file like below and check the output, the key for php.ini is "Configuration File (php.ini) Path"

<?php phpinfo(); ?>


Category : PHP

Posted by FreeBSD Newbie at 09:52 PM | Comments (1)

January 14, 2005

Complete Steps to Add a Website

To add a new website on FreeBSD, here is the complete procedure, that's what CPanel does when creating a new account. All commands are executed by root account.

1. Add a new user (test_user), the new user will have ftp permission automatically.
# adduser

2. Add the new domain to bind. (test_domain.com)
2.1 Insert a zone entry into /etc/namedb/named.conf:

zone "test_domain.com" {
type master;
file "test_domain.com.db";
};

2.2 Create a new zone file test_domain.com.db under /etc/namedb/. It looks like:

$TTL 14400
@ 14440 IN SOA ns1.yourdns.com. admin.test_domain.com. (
2004102903
14400
7200
3600000
86400
)

test_domain.com. 14400 IN NS ns1.yourdns.com.
test_domain.com. 14400 IN NS ns2.yourdns.com.
test_domain.com. 14400 IN A 1.2.3.4

localhost.test_domain.com. 14400 IN A 127.0.0.1
test_domain.com. 14400 IN MX 0 test_domain.com.
mail 14400 IN CNAME test_domain.com.
www 14400 IN CNAME test_domain.com.
ftp 14400 IN A 1.2.3.4

2.3 Restart bind
# /etc/rc.d/named restart

3. Add a virtual host entry to Apache config file /usr/local/etc/apache/httpd.conf, it looks like this:

# If IP 1.2.3.4 hosts multi-sites, must have the NameVirtualHost line
NameVirtualHost 1.2.3.4
<VirtualHost 1.2.3.4>
ServerAlias www.test_domain.com test_domain.com
ServerAdmin admin@test_domain.com
DocumentRoot /home/test_user/www
ServerName www.test_domain.com
</VirtualHost>

Restart Apache
# /usr/local/sbin/apachectl restart

4. Add database and database user for test_user.
Refer to last post.

5. Add email address.
Pending.


Category : FreeBSD General

Posted by FreeBSD Newbie at 02:42 AM | Comments (0)

January 13, 2005

Create MySQL User

Using grant to create new mysql user, the following two commands will create a new user named "user_name" with password "somepassword", the new user has all the permission on database "database_name".

mysql> grant usage on database_name.* to user_name@localhost IDENTIFIED BY 'somepassword' WITH GRANT OPTION;

mysql> grant all on database_name.* to user_name@localhost;

Test the new account
# mysql -u user_name -p


Category : MySQL

Posted by FreeBSD Newbie at 08:02 AM | Comments (0)

Mod_rewrite

mod_rewrite is enabled by default after installation, make sure the following two lines are present in Apache configuration file (/usr/local/etc/apache/httpd.conf).
LoadModule rewrite_module libexec/apache/mod_rewrite.so
AddModule mod_rewrite.c

A quick command to check this:
# cat /usr/local/etc/apache/httpd.conf | grep mod_rewrite

If you want to use url rewrite in .htaccess file, you must modify httpd.conf.
Find
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
Replace with
<Directory />
Options All
AllowOverride All
</Directory>


Category : Apache

Posted by FreeBSD Newbie at 04:38 AM | Comments (0)

First Website Is Running

Finally, I solved the DNS problem, the following steps after that are much easier.

Some tips for DNS troubleshooting
1. Get a working example from another server if possible, it doesn't matter the server is running FreeBSD or Linux, it's fine as long as the Bind is the same version (well, roughly). I didn't do this at the beginning because the Linux Bind configuration and zone files looks quite different, but I have no other options after tried two days and still no luck.

2. There is a default line in the bind configuration file, remove or comment it if you want to use the same server as DNS server.
listen-on { 127.0.0.1; };

3. Don't directly copy the text from web pages, the different Windows/Unix format could cause troubles.

4. Read the handbook carefully and don't miss any step in it :-).


PHP/MySQL
I installed php 5 and mysql 5 at first, but php didn't work, I didn't look into the problem, although it should be easy, also considering lower versions have better support and documentations, so I installed php 4 and mysql 4 instead with pkg_add.

PHP doesn't need any configuration (done by pkg_add) and is ready after installation.

Browsing ftp://ftp.freebsd.org/pub/FreeBSD/ports/i386/packages-5.3-release/Latest/ to check available packages, for example, check php 4 related packages:
ncftp > ls php4*
By the way, ncftp is a great ftp client program with bookmark support.


I installed the following packages with pkg_add:
# pkg_add -r php4
# pkg_add -r php4-mysql
# pkg_add -r mysql40-server
(Don't install mysql41-server, php4-mysql requires mysql40)

Start/Stop Mysql
# /usr/local/etc/rc.d/mysql-server.sh start[stop/restart]

Mysql will start at boot time by default, because any executable script accepting a 'start' command-line argument found in /usr/local/etc/rc.d will be executed when the system starts up.


POP3 Server
I installed qpopper as pop3 server, the installation is very easy with pkg_add, uncomment the following line in file /etc/inetd.conf to enable qpopper
pop3 stream tcp nowait root /usr/local/libexec/qpopper popper

Qpopper is controlled by inetd (super server), enable inetd start at boot time, insert the following line in /etc/rc.conf.
inetd_enable="YES"

Any configuration changes with qpopper require inetd to restart, it will reload the config file. Restart inetd with:
# /etc/rc.d/inetd restart
or send a HUP (hang up) to inetd process, it will also restart and reload the new config information:
# killall -HUP inetd
or
# kill -s HUP `cat /var/run/inetd.pid`
Note: ` is backticks not forward ticks.

I can connect the pop3 server with mail client, but didn't send or receive email successfully, should be some problems of sendmail configuration, next step.


Category : FreeBSD General

Posted by FreeBSD Newbie at 12:36 AM | Comments (0)

January 07, 2005

DNS Setup

Unfortunately I stuck at the DNS setup, I have read the handbook many times, but still don't have a clear idea about it.

Here is what I have done yesterday:
1. Running bind 9 (doc).
Create a zone file according to the example in the handbook, but later I found it's for bind 8, someone told me bind 9 is quite different. I got a bind 9 zone sample from the Internet, it didn't work either. named-checkzone checking got the following error message:

mydomain.com:1: unknown RR type 'mydomain.com.'
mydomain.com:19: using RFC 1035 TTL semantics
mydomain.com:20: ignoring out-of-zone data (mydomain.com)
dns_rdata_fromtext: mydomain.com:29: near '#': extra input text
mydomain.com:30: ignoring out-of-zone data (mydomain.com)
dns_rdata_fromtext: mydomain.com:30: near '#': extra input text
zone myaccount/IN: loading master file mydomain.com: unknown class/type

Useful commands
Start/stop/restart bind, this also applies to most other common services.
# /etc/r0.d/named start[stop/restart/status]

Check zone file
# /usr/sbin/named-checkzone account_name zonefile

Check latest error message
# tail /var/log/messages


2. Running Apache (doc)
Create virtual host entry in Apache configuration file. The website can be accessed by IP address.

Apache configuration file is at /usr/local/etc/apache/httpd.conf on FreeBSD.

Apache is one of the few services which don't run from inetd according to the handbook.
# /usr/local/sbin/apachectl start[stop/restart]

Restart Apache without aborting current connections, this is very interesting to me, never heard of it.
# /usr/local/sbin/apachectl graceful

A nice admin in the IRC room logined my server to help me locate the problem, but failed to fix it. Although I didn't make the website up and running, I learnt many things during the process.

I'm too lazy to work on it today, anyway I'm not in rush.


Category : DNS

Posted by FreeBSD Newbie at 03:48 AM | Comments (0)

January 05, 2005

I Love FreeBSD!

My server is completely new, only the OS was installed. I installed some program and learned a little basics of FreeBSD yesterday, it's fun :-).

1. Installing application from source
ports is a genius program. With it, compiling and installing an application is just one command, and all applications work the same way, you don't have to read README file to know how to install each program, type "man ports" for more information. CVSup does similar work but can ensure you get the latest version.

2. Installing pre-compiled application
The commands pkg_add, pkg_info, pkg_version are for binary application. It will fetch the application from FTP automatically and install on your system. For example, type "pkg_add -r apache" to install Apache, that's all you need to do!
Please note: some programs are not named as you expect, for example, "pkg_add -r mysql" doesn't work, I list the available applications in database with ftp and found it's named something like "mysql_server_5.0".


Installation on FreeBSD is extremely easy compared to Red Hat, check out the post to see how I installed ncftp on Red Hat, I tried the same process on FreeBSD and it didn't work, with pkg_add, "pkg_add -r ncftp" does everything for you.

I installed wget, apache, mysql, php and other basic applications yesterday, also changed my shell to bash. My next step is DNS setup and running apache/mysql.

I'm very glad that I chose FreeBSD, it's better than I could imagine. I will learn it systematically after I make my websites up and running.


Category : FreeBSD General

Posted by FreeBSD Newbie at 01:46 PM | Comments (0)

Server Is Ready

I got my server information yesterday, haven't worked on it much. The first impression - I'm lost. Most Linux commands don't work, which are default on my VPS, such as wget, command suggestion (pressing Tab), etc.

The OS is FreeBSD, LayeredTech doesn't support Red Hat, don't know how much different they are.

I'm going to play around in a FreeBSD IRC, get the idea what's the first step :-).

By the way, my VPS works great now.


Category : General

Posted by FreeBSD Newbie at 01:04 AM | Comments (0)

January 02, 2005

New Unmanaged Server

I ordered a 100% self-managed server from layeredtech yesterday, here is the server configuration:

• AMD XP2400
• 80GB IDE Hard Drive
• 1GB DDR RAM
• Bandwidth: 1000GB
• IP Addresses: 8 (5 usable)
• Private VLAN
• Basic Resource Monitoring
• FreeBSD, OpenBSD, Linux, Windows*
• 100% Self Managed and Dedicated

No control panel, no backup plan, I think a self-managed server will force me to learn how things work, it's better than fully managed server from this point of view.

I plan to blog everything I do on the new server. Now waiting for my login details.


Category : General

Posted by FreeBSD Newbie at 02:31 AM | Comments (0)