专注系统运维、网络架构,研究技术凯发app官方网站的解决方案,记录我的思想轨迹、工作学习、生活和关注的领域
分类: linux
2011-08-28 18:12:34
linux apache mysql perl/php/python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的web平台。随着开源潮流的蓬勃发展,开放源的lamp已经与和商业软件形成三足鼎立之势,并且该软件开发的项目在软件方面的投资成本较低,因此受到整个界的关注。从网站的流量上来说,70%以上的访问流量是lamp来提供的,lamp是最强大的网站凯发app官方网站的解决方案.
各种集合的编译过程:
building a lamp server
by
this document will walk you through the installation of what is known as a "lamp" system: linux, apache, mysql and php. depending on who you talk to, the p also stands for perl or python, but in general, it is assumed to be php. i run on my servers; these directions were written for centos/red hat/fedora. i have had requests for suse (another rpm-based distribution) as well as debian-based systems, so i will work on variants of these directions for those distributions in the future (donations might help speed that process up!). the main difference between the distributions is in the paths to the startup scripts. red hat systems used /etc/rc.d/init.d and suse uses /etc/init.d.
i have my first translation! provided by -- thank you very much!
if you need an ssl-enabled server, i have a as well.
i designed this document so you can just copy/paste each line or block of commands into your shell session and it will "just work" for you. this avoids tedious typing, and the inevitable typos or missed steps that result. these commands work properly via copy/paste. if you are having problems and you are not using copy/paste, please re-check your typing before sending me an email saying "it doesn't work."
text in a "command" box like this one is a literal linux commandline, and should be typed or pasted exactly as written.
one note: many many people have followed these directions as written,
and have not had any problems.
if you are having a problem, chances are it's something you are doing (or not doing), something different
about your computer, etc.
it is probably not this procedure. :)
initial steps
please be aware that a source-based installation like this one is not needed for a basic lamp server! you should only be doing a source-based installation if you need to alter settings in one or more components of the lamp stack (e.g., you need a feature in php that isn't in the default rpm). if you are just getting started with lamp, use the binaries provided by your distribution - it is much simpler, and a lot easier to upgrade later.
most out-of-the-box red hat linux installations will have one or more of the lamp components installed via rpm files. i personally believe in installing things like this from source, so i get the most control over what's compiled in, what's left out, etc. but source code installs can wreak havoc if overlaid on top of rpm installs, as the two most likely won't share the same directories, etc.
if you have not yet installed your linux os, or just for future reference, do not choose to install apache, php, or mysql during the system installation. then you can immediately proceed with the source-based install listed here.
note: to install applications from source code, you will need a c compiler (gcc ) installed. this is generally taken care of, but i've had enough queries about it that i've added this note to avoid getting more! you can use your distribution's install cds to get the proper version of the compiler. or, if you are using an rpm based distro, you can use a site like to locate the correct rpm version for your system. (you will obviously not be able to use/rebuild a source rpm to get the compiler installed, as you need the compiler to build the final binary rpm!) on a fedora system, you can do this command:
su - root
yum install gcc gcc-c
log in as root
because we will be installing software to directories that "regular" users don't have write access to, and also possibly uninstalling rpm versions of some applications, we'll log in as root. the only steps that need root access are the actual installation steps, but by doing the configure and make steps as root, the source code will also be inaccessible to "regular" users.
if you do not have direct access (via keyboard) to the server, please use to access the server and not telnet!! whenever you use telnet (or plain ftp for that matter), you are transmitting your username, password, and all session information in "plain text". this means that anyone who can access a machine someplace between your pc and your server can snoop your session and get your info. use encryption wherever possible!
su - root
remove rpm versions of the applications
before we start with our source code install, we need to remove all the existing rpm files for these products. to find out what rpms are already installed, use the rpm query command:
rpm -qa
in conjunction with grep to filter your results:
rpm -qa | grep -i apache
rpm -qa | grep -i httpd
rpm -qa | grep -i php
rpm -qa | grep -i mysql
the 'httpd' search is in case you have apache2 installed via rpm.
to remove the rpms generated by these commands, do
rpm -e filename
for each rpm you found in the query. if you have any content in your mysql database already, the rpm removal step should not delete the database files. when you reinstall mysql, you should be able to move all those files to your new mysql data directory and have access to them all again.
get the source code for all applications
we want to put all our source code someplace central, so it's not getting mixed up in someone's home directory, etc.
cd /usr/local/src
one way application source code is distributed is in what are known as "tarballs." the tar command is usually associated with making tape backups - tar stands for tape archive. it's also a handy way to pack up multiple files for easy distribution. use the man tar command to learn more about how to use this very flexible tool.
at the time of updating this, the current versions of all the components we'll use are:
mysql - 4.1.22
apache - 1.3.37
php - 4.4.6
please note: these are the only versions of these that i have set up myself, and verified these steps against. if you use another version of any component, especially a newer version, this howto may not be accurate, and i won't be able to provide free support under those circumstances. paid support and assistance is always available however.
wget
wget
there may be an apache mirror closer to you - check their for other sources. then insert the url you get in place of the above for the wget command.
for mysql, go to and choose an appropriate mirror to get the newest mysql version (v4.1.22).
unpack the source code
tar zxf php-4.4.6.tar.gz
tar zxf apache_1.3.37.tar.gz
tar zxf mysql-4.1.22.tar.gz
this should leave you with the following directories:
/usr/local/src/php-4.4.6
/usr/local/src/apache_1.3.37
/usr/local/src/mysql-4.1.22
build and install mysql
first, we create the group and user that "owns" mysql. for security purposes, we don't want mysql running as root on the system. to be able to easily identify mysql processes in top or a ps list, we'll make a user and group named mysql:
groupadd mysql
useradd -g mysql -c "mysql server" mysql
if you get any messages about the group or user already existing, that's fine. the goal is just to make sure we have them on the system.
what the useradd command is doing is creating a user mysql in the group mysql with the "name" of mysql server. this way when it's showed in various user and process watching apps, you'll be able to tell what it is right away.
now we'll change to the "working" directory where the source code is, change the file 'ownership' for the source tree (this prevents build issues in reported in some cases where the packager's username was included on the source and you aren't using the exact same name to compile with!) and start building.
the configure command has many options you can specify. i have listed some fairly common ones; if you'd like to see others, do:
./configure --help | less
to see them all. read the for a more detailed explanation of each option.
cd /usr/local/src/mysql-4.1.22
chown -r root.root *
make clean
./configure \
--prefix=/usr/local/mysql \
--localstatedir=/usr/local/mysql/data \
--disable-maintainer-mode \
--with-mysqld-user=mysql \
--with-unix-socket-path=/tmp/mysql.sock \
--without-comment \
--without-debug \
--without-bench
18-jul-2005: if you are installing mysql 4.0.x on fedora core 4, there is a problem with linuxthreads that prevents mysql from compiling properly. installing on fedora core 3 works fine though. thanks to kevin spencer for bringing this to my attention. there is a workaround listed at . thanks to collin campbell for that link. another solution can be found at . thanks to kaloyan raev for that one.
now comes the long part, where the source code is actually compiled and then installed. plan to get some coffee or take a break while this step runs. it could be 10-15 minutes or more, depending on your system's free memory, load average, etc.
make && make install
configure mysql
mysql is "installed" but we have a few more steps until it's actually "done" and ready to start. first run the script which actually sets up mysql's internal database (named, oddly enough, mysql).
./scripts/mysql_install_db
then we want to set the proper ownership for the mysql directories and data files, so that only mysql (and root) can do anything with them.
chown -r root:mysql /usr/local/mysql
chown -r mysql:mysql /usr/local/mysql/data
copy the default configuration file for the expected size of the database (small, medium, large, huge)
cp support-files/my-medium.cnf /etc/my.cnf
chown root:sys /etc/my.cnf
chmod 644 /etc/my.cnf
if you get an error message about the data directory not existing, etc., something went wrong in the mysql_install_db step above. go back and review that; make sure you didn't get some sort of error message when you ran it, etc.
now we have to tell the system where to find some of the dynamic libraries that mysql will need to run. we use dynamic libraries instead of static to keep the memory usage of the mysql program itself to a minimum.
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
ldconfig
now create a startup script, which enables mysql auto-start each time your server is restarted.
cp ./support-files/mysql.server /etc/rc.d/init.d/mysql
chmod x /etc/rc.d/init.d/mysql
/sbin/chkconfig --level 3 mysql on
then set up symlinks for all the mysql binaries, so they can be run from anyplace without having to include/specify long paths, etc.
cd /usr/local/mysql/bin
for file in *; do ln -s /usr/local/mysql/bin/$file /usr/bin/$file; done
mysql security issues
first, we will assume that only applications on the same server will be allowed to access the database (i.e., not a program running on a physically separate server). so we'll tell mysql not to even listen on port 3306 for tcp connections like it does by default.
edit /etc/my.cnf and uncomment the
skip-networking
line (delete the leading #).
for more security info, check out this .
start mysql
first, test the linked copy of the startup script in the normal server runlevel start directory, to make sure the symlink was properly set up:
cd ~
/etc/rc.d/rc3.d/s90mysql start
if you ever want to manually start or stop the mysql server, use these commands:
/etc/rc.d/init.d/mysql start
/etc/rc.d/init.d/mysql stop
let's "test" the install to see what version of mysql we're running now:
mysqladmin version
it should answer back with the version we've just installed...
now we'll set a password for the mysql root user (note that the mysql root user is not the same as the system root user, and definitely should not have the same password as the system root user!).
mysqladmin -u root password new-password
(obviously, insert your own password in the above command instead of the "new-password" string!)
you're done! mysql is now installed and running on your server. it is highly recommended that you read about mysql security and lock down your server as much as possible. the mysql site has info at .
test mysql
to run a quick test, use the command line program mysql:
mysql -u root -p
and enter your new root user password when prompted. you will then see the mysql prompt:
mysql>
first, while we're in here, we'll take care of another security issue and delete the sample database test and all default accounts except for the mysql root user. enter each of these lines at the mysql> prompt:
drop database test;
use mysql;
delete from db;
delete from user where not (host="localhost" and user="root");
flush privileges;
as another security measure, i like to change the mysql administrator account name from root to something harder to guess. this will make it that much harder for someone who gains shell access to your server to take control of mysql.
make sure you remember this new name, and use it wherever
you see "root" in other directions, websites, etc.
once you do this step, the username "root" will cease to
exist in your mysql configuration!
update user set user="sqladmin" where user="root";
flush privileges;
now, on with the "standard" testing... first, create a new database:
create database foo;
you should see the result:
query ok, 1 row affected (0.04 sec)
mysql>
delete the database:
drop database foo;
you should see the result:
query ok, 0 rows affected (0.06 sec)
mysql>
to exit from mysql enter \q:
\q
build and install apache (with dso support)
the advantage to building apache with support for dynamically loaded modules is that in the future, you can add functionality to your webserver by just compiling and installing modules, and restarting the webserver. if the features were compiled into apache, you would need to rebuild apache from scratch every time you wanted to add or update a module (like php). your apache binary is also smaller, which means more efficient memory usage.
the downside to dynamic modules is a slight performance hit compared to having the modules compiled in.
cd /usr/local/src/apache_1.3.37
make clean
./configure \
--prefix=/usr/local/apache \
--enable-shared=max \
--enable-module=rewrite \
--enable-module=so
make && make install
build and install php
this section has only been tested with php v4.x. if you are trying to build php 5.x, i do not have experience with this yet, and do not provide free support for you to get it working. please note that there are many options which can be selected when compiling php. some will have library dependencies, meaning certain software may need to be already installed on your server before you start building php. you can use the command
./configure --help | less
once you change into the php source directory. this will show you a list of all possible configuration switches. for more information on what these switches are, please check the .
cd /usr/local/src/php-4.4.6
./configure \
--with-apxs=/usr/local/apache/bin/apxs \
--disable-debug \
--enable-ftp \
--enable-inline-optimization \
--enable-magic-quotes \
--enable-mbstring \
--enable-mm=shared \
--enable-safe-mode \
--enable-track-vars \
--enable-trans-sid \
--enable-wddx=shared \
--enable-xml \
--with-dom \
--with-gd \
--with-gettext \
--with-mysql=/usr/local/mysql \
--with-regex=system \
--with-xml \
--with-zlib-dir=/usr/lib
make && make install
cp php.ini-dist /usr/local/lib/php.ini
i like to keep my config files all together in /etc. i set up a symbolic link like this:
ln -s /usr/local/lib/php.ini /etc/php.ini
then i can just open /etc/php.ini in my editor to make changes.
recommended reading on securing your php installation is at .
edit the apache configuration file (httpd.conf)
i like to keep all my configuration files together in /etc, so i set up a symbolic link from the actual location to /etc:
ln -s /usr/local/apache/conf/httpd.conf /etc/httpd.conf
now open /etc/httpd.conf in your favorite text editor, and set all the basic apache options in accordance with (beyond the scope of this howto).
also recommended is the article on .
to ensure your php files are properly interpreted, and not just downloaded as text files, remove the # at the beginning of the lines which read:
if the addtype lines above don't exist, manually enter them (without the leading # of course) after the line
addtype application/x-tar .tgz
or anyplace within the
if you wish to use other/additional extensions/filetypes for your php scripts instead of just .php, add them to the addtype directive:
addtype application/x-httpd-php .php .foo
addtype application/x-httpd-php-source .phps .phtmls
an example: if you wanted every single html page to be parsed and processed like a php script, just add .htm and .html:
addtype application/x-httpd-php .php .htm .html
there will be a bit of a performance loss if every single html page is being checked for php code even if it doesn't contain any. but if you want to use php but be "stealthy" about it, you can use this trick.
add index.php to the list of valid directory index files so that your "default page" in a directory can be named index.php.
directoryindex index.php index.htm index.html
you can add anything else you want here too. if you want foobar.baz to be a valid directory index page, just add the .baz filetype to the addtype line, and add foobar.baz to the directoryindex line.
start apache
we want to set apache up with a normal start/stop script in /etc/rc.d/init.d so it can be auto-started and controlled like other system daemons. set up a symbolic link for the apachectl utility (installed automatically as part of apache):
ln -s /usr/local/apache/bin/apachectl /etc/rc.d/init.d/apache
then set up auto-start for runlevel 3 (where the server will go by default):
ln -s /etc/rc.d/init.d/apache /etc/rc.d/rc3.d/s90apache
then start the daemon:
/etc/rc.d/init.d/apache start
you can check that it's running properly by doing:
ps -ef
and look for the httpd processes.
a note from the author
i hope you find this guide, howto, tutorial -- call it what you will -- useful. i also hope it saves you some time. if you do find it useful, i would be grateful if you could make a donation using the button below (and if it wasn't useful, and tell me why not).
i receive a great deal of mail as a result of this howto, much of it asking me to solve various lamp-related problems. please bear in mind that the money that puts food on my family's table comes from the consultancy work that i do. if you would like help with any of the points discussed in this article, and tell me what that help is worth to you. quote any amount you like, and if i'm able to help you out, you can make a paypal donation by way of thanks. fair enough?