Archive for January, 2007
Installing Apache, MySQL, and PHP on Linux
This tutorial is designed to guide you through the initial steps of setting up Apache, MySQL, and PHP on Linux. The Linux distribution being utilized for this tutorial is Fedora Core 1, however the steps should be very similar across most distributions. This tutorial makes the assumption that you have the required development tools loaded for compiling programs from source, these tools are beyond the scope of this document and will not be covered here. Also, it assumes you can use the vi text editor for basic editing tasks.
Apache, MySQL, and PHP have become one of the most utilized combinations for developing content driven websites. They are robust, flexible, provide a decent level of security, and they are available for many different platforms. That being said, lets get to building a web server.
The first thing you need to do is obtain the sourceballs for each package, we will be compiling each package from scratch here, and, while there are also binary packages available for some distributions, I find your end results are usually better when building each package for your machine. Make sure you get the source files.
Here are the links and the package versions available at the time this tutorial was written
Apache
URL : http://httpd.apache.org/download.cgi
Current Version - 2.0.48
MySQL
URL : http://www.mysql.com/downloads/mysql-4.0.html
Current Version - 4.0.16
PHP
URL : http://www.php.net/downloads.php
Current Version - 4.3.4
Ok, so you’ve got the files now what ?, well now the fun begins..
Installation -
The first thing we need to do is extract the sourceballs so we can work with the files included in them. Beginning now we will be working as root, so open a terminal window, change to the directory in which you saved your downloaded files and become root by issuing the su command, enter the root password and you should be good to go.
To extract the sourceballs type the following commands;
#tar -zxf httpd-2.0.48.tar.gz (enter)
#tar -zxf mysql-4.0.16.tar.gz (enter)
#tar -zxf php-4.3.4.tar.gz (enter)
The commands above will extract the sourceballs into their own separate directories. Now lets move on to compiling the source into usable programs. We’ll start with Apache.
Compiling Apache -
Change into the directory created when you untarred the sourceball as follows;
#cd httpd-2.0.48 (enter)
Follow this command by typing;
#./configure –prefix=/usr/local/apache2 –enable-mods-shared=most (enter)
This tells Apache to install in the /usr/local/apache2 directory, and to build most of the available loadable modules. There are a ton of options with Apache, but these should work for the most part. Once the configure is done and the system returns the prompt to you, issue the following command;
#make
This will take a few minutes, once the prompt comes back again issue the following command;
#make install
Wait for a few minutes and viola !, Apache is installed with the exception of a few minor changes we still need to make. They are as follows..
Issue the following command;
#vi /usr/local/apache2/conf/httpd.conf
Check to make sure the following line is present in the file at the bottom of the LoadModule list, if it is not there add it;
LoadModule php4_module modules/libphp4.so
Find the DirectoryIndex line and edit it so it looks like the following;
DirectoryIndex index.html index.html.var index.php
Find the AddType application section and add the following line;
AddType application/x-httpd-php .php
Thats it, save the file and we are done with Apache. Now, on to MySQL !
Compiling MySQL -
Change into the MySQL source directory as follows;
#cd mysql-4.0.16 (enter)
Follow this command by typing;
#./configure –prefix=/usr/local/mysql –localstatedir=/usr/local/mysql/data –disable-maintainer-mode –with-mysqld-user=mysql –enable-large-files-without-debug (enter)
Sit back and wait for a while while configure does its thing, once the system returns the prompt to you issue the following command;
#make (enter)
Unless you have a very fast machine this will take some time, so spend time with your family, grab a beer, go for a walk, or whatever you’re into. When you get back, assuming the system has returned the prompt to you issue the following command;
#make install (enter)
Cool !, MySQL is installed, there are only a couple things left to do to get it working, first we need to create a group for MySQL as follows;
#/usr/sbin/groupadd mysql (enter)
Then we create a user called mysql which belongs to the mysql group;
#/usr/sbin/useradd -g mysql mysql (enter)
Now we install the database files as follows;
#./scripts/mysql_install_db (enter)
Then we make a couple minor ownership changes;
# chown -R root:mysql /usr/local/mysql (enter)
# chown -R mysql:mysql /usr/local/mysql/data (enter)
Last but not least, we use vi to add a line the ld.so.conf file as follows;
#vi /etc/ld.so.conf
And we add the following line;
/usr/local/mysql/lib/mysql
Thats it, MySQL is installed, you can run it by issuing the following command;
#/usr/local/mysql/bin/mysqld_safe –user=mysql &
And as long as we’re here we might as well set a root password for MySQL as follows;
#/usr/local/mysql/bin/mysqladmin -u root password new_password
Where new_password is the password you want to use.
Ok, so far so good, on to PHP !
Compiling PHP -
Change into the PHP source directory as follows;
#cd php-4.3.4 (enter)
Follow this command by typing;
#./configure –prefix=/usr/local/php –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql=/usr/local/mysql (enter)
Once the prompt comes back to you issue the following command;
#make (enter)
Hang out for awhile, and then yep, you guessed it, once you have the prompt back;
#make install (enter)
Once the install finishes and you have the prompt back issue the following command;
#cp php.ini-recommended /usr/local/php/lib/php.ini (enter)
Then edit that file;
#vi /usr/local/php/lib/php.ini (enter)
And change the following;
Find the doc_root section and enter the correct path for the directory which serves your web content, such as;
doc_root= “/usr/local/apache2/htdocs/”
(this is default for apache2)
Then find the file_uploads section and change it to reflect the following;
file_uploads=Off
(for security reasons)
Thats if for PHP, now lets see if it all works..
Testing -
Assuming your MySQL process is still running from earlier, lets start Apache by issuing the following command;
#/usr/local/apache2/bin/apachectl start (enter)
This starts the Apache web server, now change into the following directory;
#cd /usr/local/apache2/htdocs (enter)
And using vi create a file called test.php;
#vi test.php
Add the following line to the file;
Save the file, then fire up your browser and point it to localhost/test.php. You should see a listing of all kinds of cool info about Apache, PHP, etc. If you do then your set !, if you don’t, then take a look at your logs for Apache and MySql, and remember Google is your friend. But hopefully you do, and now you have a fully functioning setup.
Ok, one last step and we’ll be done, you have everything running now, but you had to start Apache and MySql manually, that’s something you don’t want to have to remember to do everytime you reboot your machine, so lets fix it.
Starting Apache and MySQL Automatically -
Lets start with MySQL, as root make your working directory that of the MySQL source directory you worked with earlier, something similar to;
#cd /home/xxxx/mysql-4.0.16
Then, copy the file mysql.server to your /etc/init.d directory as follows;
#cp support-files/mysql.server /etc/init.d/mysql
Ok, lets create some links in the startup folders for run levels 3 and 5.
#cd /etc/rc3.d
#ln -s ../init.d/mysql S85mysql
#ln -s ../init.d/mysql K85mysql
#cd /etc/rc5.d
#ln -s ../init.d/mysql S85mysql
#ln -s ../init.d/mysql K85mysql
#cd ../init.d
#chmod 755 mysql
Thats it for MySQL, it should start automatically now when you reboot your machine. Now lets do the same for Apache, still as root make your working directory that of the Apache binaries as follows;
#cd /usr/local/apache2/bin
Then, copy the file called apachectl as follows;
#cp apachectl /etc/init.d/httpd
Now, for some more links;
#cd /etc/rc3.d
#ln -s ../init.d/httpd S85httpd
#ln -s ../init.d/httpd K85httpd
#cd /etc/rc5.d
#ln -s ../init.d/httpd S85httpd
#ln -s ../init.d/httpd K85httpd
And thats it for Apache !, it should start automatically along with MySQL the next time you boot your machine.
That brings us to the end of this tutorial, hopefully you found it helpful, and Good Luck !
No comments
Finding/locating files with find command
UNIX/Linux system administrator can use nifty find utility to gain lots of useful information. This will help to monitor and enhance the security of system.
(A) Finding all set user id files
# find / -perm +u=s
OR
# find / -perm +4000
See also, shell script to find all programs and scripts with setuid set on.
(B) Finding all set group id files
# find / -perm +g=s
OR
# find / -perm +2000
See also, shell script to find all programs and scripts with setgid bit set on.
(C)Finding all large directories – For example find all directories taking 40k (kilobytes) blocks of space. This is useful to find out which directories on system taking lot of space.
# find / -type d -size +40k
/var/lib/dpkg/info
/var/log/ksymoops
/usr/share/doc/HOWTO/en-html
/usr/share/man/man3
(D) Finding all large files on a Linux
# find / -type f -size +10000k
/var/log/kern.log
/sys/devices/pci0000:00/0000:00:02.0/resource0
/sys/devices/pci0000:00/0000:00:00.0/resource0
/opt/03Jun05/firefox-1.0.4-source.tar.bz2
However my favorite hack to above command is as follows:
find / -type f -size +10000k -exec ls -lh {} \; | awk ‘{ print $8 “: ” $5 }’
/var/log/kern.log: 22M
/sys/devices/pci0000:00/0000:00:02.0/resource0: 128M
Above command will find all files block size greater than 10000k and print filename followed by file size. Now this is more informative than the normal output .
No commentsInstalling zend optimizer
Zend optimizer is a plugin to php which allows a server to read zend encoded files as well as serve as a platform to install zend modules such as eaccelerator. The installation is incredibly simple thanks to zend’s script. The current 10a version works with php 4.4.1 as well as the latest version of eaccelerator. Currently the cPanel scripts do NOT install a version of zend compatible with php 4.4.1 so a manual install must be done.
I have a local copy of the files:
cd /usr/local/src
wget http://www.eth0.us/files/ZendOptimizer-2.5.10a-linux-glibc21-i386.tar.gz
tar -zxf ZendOptimizer-2.5.10a-linux-glibc21-i386.tar.gz
cd ZendOptimizer-2.5.10a-linux-glibc21-i386
./install
Now just hit enter a few times until it is done and you are done! It is as easy as that.
Article from eth0.us
No commentsOptimizing host.conf and sysctl.conf
#!/bin/sh
cp /etc/host.conf /etc/host.back
echo “# Lookup names via DNS first then fall back to /etc/hosts.” > /etc/host.conf
echo “order bind,hosts” >> /etc/host.conf
echo “# We have machines with multiple IP addresses.” >> /etc/host.conf
echo “multi on” >> /etc/host.conf
echo “# Check for IP address spoofing.” >> /etc/host.conf
echo “nospoof on” >> /etc/host.conf
cp /etc/sysctl.conf /etc/sysctl.conf.old
echo “# Max File Handlers” >> /etc/sysctl.conf
echo “fs.file-max = 8192″ >> /etc/sysctl.conf
echo “# Disable CTR+ALT+DEL Restart Keys” >> /etc/sysctl.conf
echo “kernel.ctrl-alt-del = 1″ >> /etc/sysctl.conf
echo “# Enable TCP SYN cookie protection” >> /etc/sysctl.conf
echo “net.ipv4.tcp_syncookies = 1″ >> /etc/sysctl.conf
echo “# Disable ICMP Redirect Acceptance” >> /etc/sysctl.conf
echo “net.ipv4.conf.all.accept_redirects = 0″ >> /etc/sysctl.conf
echo “# Enable bad error message protection” >> /etc/sysctl.conf
echo “net.ipv4.icmp_ignore_bogus_error_responses = 1″ >> /etc/sysctl.conf
echo “# Decrease time between keepalives” >> /etc/sysctl.conf
echo “net.ipv4.tcp_keepalive_time = 1200″ >> /etc/sysctl.conf
echo “# Turn off timestamps” >> /etc/sysctl.conf
echo “net.ipv4.tcp_timestamps = 0″ >> /etc/sysctl.conf
echo “# Ignore icmp broadcast request” >> /etc/sysctl.conf
echo “net.ipv4.icmp_echo_ignore_broadcasts = 1″ >> /etc/sysctl.conf
echo “# Decrease the time default value for tcp_fin_timeout connection” >> /etc/sysctl.conf
echo “net.ipv4.tcp_fin_timeout = 25″ >> /etc/sysctl.conf
echo “# Turn off the tcp_window_scaling” >> /etc/sysctl.conf
echo “net.ipv4.tcp_window_scaling = 0″ >> /etc/sysctl.conf
echo “# Turn off the tcp_sack” >> /etc/sysctl.conf
echo “net.ipv4.tcp_sack = 0″ >> /etc/sysctl.conf
echo “# Allow more SYN backlog” >> /etc/sysctl.conf
echo “net.ipv4.tcp_max_syn_backlog = 1048″ >> /etc/sysctl.conf
echo “# Lower retry rates” >> /etc/sysctl.conf
echo “net.ipv4.tcp_synack_retries = 2″ >> /etc/sysctl.conf
echo “net.ipv4.tcp_syn_retries = 3″ >> /etc/sysctl.conf
cp /etc/syslog.conf /etc/syslog.conf.old
echo “# Log all kernel messages to the new file /var/log/kernel” >> /etc/syslog.conf
echo “kern.* /var/log/kernel” >> /etc/syslog.conf
echo “# Log all logins to /var/log/login_log” >> /etc/syslog.conf
echo “auth.*;user.*;daemon.none /var/log/login_log” >> /etc/syslog.conf
when you look through the script, you can generally see what it is doing. But for a basic overveiw, the script is setting up the configs most people forget about, or maybe never even knew about. The script also makes a back-up of itself incase something might go wrong.
Enjoy.