本文所用的操作系统为 CentOS 3.8,如果使用 Red Hat AS3/4 则不能使用 yum RPM 包管理命令!
推荐使用 CentOS 3.8 系统。
一、系统约定
软件源代码包存放位置 | /usr/local/src |
源码包编译安装位置(prefix) | /usr/local/software_name |
脚本以及维护程序存放位置 | /usr/local/sbin |
MySQL 数据库位置 | /var/lib/mysql |
Apache 网站根目录 | /data/wwwroot |
Apache 虚拟主机日志根目录 | /data/logs/www |
Apache 运行账户 | www:www |
yum RPM包信息文件 | /etc/yum.list |
二、系统环境部署及调整
1. 检查系统是否正常
# more /var/log/messages (检查有无系统内核级错误信息)
# dmesg (检查硬件设备是否有错误信息)
# ifconfig(检查网卡设置是否正确)
# ping www.163.com (检查网络是否正常)
2. 关闭不需要的服务
# ntsysv
以下仅列出需要启动的服务,未列出的服务一律关闭:
crond
irqbalance (仅当服务器CPU为S.M.P架构或支持双核心、HT技术时,才需开启,否则关闭。)
microcode_ctl
network
random
sendmail
sshd
syslog
3. 对TCP/IP网络参数进行调整,加强抗SYN Flood能力
# echo 'net.ipv4.tcp_syncookies = 1' >> /etc/sysctl.conf
# sysctl -p
4. 配置yum
# rpm --import /usr/share/doc/centos-release-3/RPM-GPG-KEY-CentOS-3
# yum list | tee /etc/yum.list
5. 修改命令history记录
# vi /etc/profile
找到 HISTSIZE=1000 改为 HISTSIZE=50
6. 定时校正服务器时间
# yum install ntp
# crontab -e
加入一行:
*/15 * * * * ntpdate 210.72.145.44
## 210.72.145.44 为中国国家授时中心服务器地址。
7. 重新启动系统
# init 6
8. 使用 yum 程序安装所需开发包(以下为标准的 RPM 包名称)
gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel
9. 源码编译安装所需包(Source)
(1) GD2
# cd /usr/local/src
# wget http://download.discuz.net/env/gd-2.0.33.tar.gz
# tar xzvf gd-2.0.33.tar.gz
# cd gd-2.0.33
# ./configure --prefix=/usr/local/gd2
# make
# make install
(2) LibXML2
# cd /usr/local/src
# wget http://download.discuz.net/env/libxml2-2.6.26.tar.bz2
# tar xjvf libxml2-2.6.26.tar.bz2
# cd libxml2-2.6.26
# ./configure --prefix=/usr/local/libxml2
# make
# make install
(3) Apache日志截断程序
# cd /usr/local/src
# wget http://download.discuz.net/env/cronolog-1.6.2.tar.gz
# tar xzvf cronolog-1.6.2.tar.gz
# cd cronolog-1.6.2
# ./configure –prefix=/usr/local/cronolog
# make
# make install
10. 升级OpenSSL和OpenSSH
# cd /usr/local/src
# wget http://download.discuz.net/env/openssl-0.9.7j.tar.gz
# wget http://download.discuz.net/env/openssh-4.2p1.tar.gz
# tar xzvf openssl-0.9.7j.tar.gz
# cd openssl-0.9.7j
# ./config --prefix=/usr/local/openssl
# make
# make test
# make install
# cd ..
# tar xzvf openssh-4.2p1.tar.gz
# cd openssh-4.2p1
# ./configure /
"--prefix=/usr" /
"--with-pam" /
"--with-zlib" /
"--with-ssl-dir=/usr/local/openssl" /
"--with-md5-passwords"
# make
# make install
禁止root直接登录,此处先建立一个普通系统用户:
# useradd username
# passwd username
# echo 'Protocol 2' >> /etc/ssh/sshd_config
# echo 'Protocol 2' >> /usr/etc/sshd_config
# echo 'PermitRootLogin no' >> /etc/ssh/sshd_config
# echo 'PermitRootLogin no' >> /usr/etc/sshd_config
# /etc/rc.d/init.d/sshd restart
三、编译安装L.A.M.P环境
1. 下载软件
# cd /usr/local/src
# wget http://download.discuz.net/env/httpd-2.2.3.tar.bz2
# wget http://download.discuz.net/env/mysql-5.0.27.tar.gz
# wget http://download.discuz.net/env/php-5.2.0.tar.bz2
# wget http://download.discuz.net/env/ZendOptimizer-3.2.0-linux-glibc21-i386.tar.gz
2. 编译安装MySQL
# tar xzvf mysql-5.0.27.tar.gz
# cd mysql-5.0.27
# ./configure /
"--prefix=/usr/local/mysql" /
"--localstatedir=/var/lib/mysql" /
"--with-comment=Source" /
"--with-server-suffix=-Comsenz" /
"--with-mysqld-user=mysql" /
"--without-debug" /
"--with-big-tables" /
"--with-charset=" /
"--with-collation= " /
"--with-extra-charsets=all" /
"--with-pthread" /
"--enable-static" /
"--enable-thread-safe-client" /
"--with-client-ldflags=-all-static" /
"--with-mysqld-ldflags=-all-static" /
"--enable-assembler" /
"--without-isam" /
"--without-innodb" /
"--without-ndb-debug"
# make
# make install
# useradd mysql
# cd /usr/local/mysql
# bin/mysql_install_db --user=mysql
# chown -R root:mysql .
# chown -R mysql /var/lib/mysql
# cp share/mysql/my-huge.cnf /etc/my.cnf
# cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# /etc/rc.d/init.d/mysqld start
# bin/mysql -u root password "password_for_root"
3. 编译安装Apache
# cd /usr/local/src
# tar xjvf httpd-2.2.3.tar.bz2
# cd httpd-2.2.3
# ./configure /
"--prefix=/usr/local/apache2" /
"--enable-module=so" /
"--enable-deflate=shared" /
"--enable-expires=shared" /
"--enable-rewrite=shared" /
"--enable-static-support" /
"--enable-static-htpasswd" /
"--enable-static-htdigest" /
"--enable-static-rotatelogs" /
"--enable-static-logresolve" /
"--enable-static-htdbm" /
"--enable-static-ab" /
"--enable-static-checkgid" /
"--disable-userdir"
# make
# make install
# echo '/usr/local/apache2/bin/apachectl start ' >> /etc/rc.local
4. 编译安装PHP
# cd /usr/local/src
# tar xjvf php-5.2.0.tar.bz2
# cd php-5.2.0
# ./configure /
"--prefix=/usr/local/php" /
"--with-apxs2=/usr/local/apache2/bin/apxs" /
"--with-config-file-path=/etc" /
"--with-mysql=/usr/local/mysql" /
"--with-libxml-dir=/usr/local/libxml2" /
"--with-gd=/usr/local/gd2" /
"--enable-gd-native-ttf" /
"--with-jpeg-dir" /
"--with-png-dir" /
"--with-bz2" /
"--with-freetype-dir" /
"--with-iconv-dir" /
"--with-zlib-dir" /
"--enable-mbstring" /
"--disable-ipv6" / # IPv6 离我们还好远,暂时不用它
"--disable-cgi" / # 出于安全考虑,禁用 CGI 支持
"--disable-cli" # 出于安全考虑,禁止编译出 php Command Line Shell 命令
# make
# make install
# cp php.ini-dist /etc/php.ini
5. 安装Zend Optimizer
# cd /usr/local/src
# tar xzvf ZendOptimizer-3.2.0-linux-glibc21-i386.tar.gz
# ./ZendOptimizer-3.2.0-linux-glibc21-i386/install.sh
安装Zend Optimizer过程的最后不要选择重启Apache。
6. 整合Apache与PHP
# vi /usr/local/apache2/conf/httpd.conf
找到:
AddType application/x-gzip .gz .tgz
在该行下面添加
AddType application/x-httpd-php .php
AddOutputFilterByType DEFLATE text/html text/plain text/xml
找到:
<IfModule dir_module>
DirectoryIndex index.html index.html.var
</IfModule>
将该行改为
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
</IfModule>
找到:
#Include conf/extra/httpd-mpm.conf
#Include conf/extra/httpd-info.conf
#Include conf/extra/httpd-vhosts.conf
#Include conf/extra/httpd-default.conf
去掉前面的“#”号,取消注释,设置相关的模块配置文件。
修改完成后保存退出。
# /usr/local/apache2/bin/apachectl restart
7. 查看确认L.A.M.P环境信息,提升 PHP 安全性
# echo '#!/usr/local/php/bin/php /
<?php /
phpinfo(); /
?>' > /usr/local/php/phpinfo.php
# chmod 755 /usr/local/php/phpinfo.php
# /usr/local/php/phpinfo.php > /usr/local/php/phpinfo
# more /usr/local/php/phpinfo
检查phpinfo中的各项信息是否正确。
确认 PHP 能够正常工作后,在 php.ini 中进行设置提升 PHP 安全性。
# vi /etc/php.ini
找到:
disable_functions =
设置为:
disable_functions = phpinfo,passthru,exec,system,popen,chroot,escapeshellcmd,escapeshellarg,shell_exec,proc_open,proc_get_status,ini_restore
最后,重新启动 Apache 服务即可。