LAMP(Linux+Apache+Mysql+PHP)環(huán)境搭建

字號(hào):

本文測(cè)試環(huán)境為CentOS 5.0,如果使用Red Hat AS3/4則不能使用yum命令管理rpm包。
    一.系統(tǒng)約定
    軟件源代碼包存放位置 /usr/local/src
    源碼包編譯安裝位置(prefix) /usr/local/software_name
    腳本以及維護(hù)程序存放位置 /usr/local/sbin
    MySQL 數(shù)據(jù)庫(kù)位置 /var/lib/mysql
    Apache 網(wǎng)站根目錄 /home/www/wwwroot
    Apache 虛擬主機(jī)日志根目錄 /home/www/logs
    Apache 運(yùn)行帳戶 www:www
    二.系統(tǒng)環(huán)境部署及調(diào)整
    1.檢查系統(tǒng)是否正常
    # more /var/log/messages (檢查有無系統(tǒng)級(jí)錯(cuò)誤信息)
    # dmesg (檢查硬件設(shè)備是否有錯(cuò)誤信息)
    # ifconfig(檢查網(wǎng)卡設(shè)置是否正確)
    # ping www.britepic.org (檢查網(wǎng)絡(luò)是否正常)
    2.關(guān)閉不需要的服務(wù)
    # ntsysv
    以下僅列出需要啟動(dòng)的服務(wù),未列出的服務(wù)一律關(guān)閉:
    atd
    crond
    irqbalance
    microcode_ctl
    network
    sendmail
    sshd
    syslog
    3.重新啟動(dòng)系統(tǒng)
    # init 6
    4.配置 vim
    # vi /root/.bashrc
    在 alias mv='mv -i' 下面添加一行:alias vi='vim' 保存退出。
    # echo 'syntax on' > /root/.vimrc
    5.使用yum程序安裝所需軟件包(以下為標(biāo)準(zhǔn)的RPM包名稱)
    # yum install ntp vim-enhanced gcc gcc-c++ flex bison autoconf automake bzip2-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel kernel
    6.定時(shí)校正服務(wù)器時(shí)鐘,定時(shí)與中國(guó)國(guó)家授時(shí)中心授時(shí)服務(wù)器同步
    # crontab -e
    加入一行:
    */30 * * * * ntpdate 210.72.145.44
    7.源代碼編譯安裝所需包
    (1) GD2
    # cd /usr/local/src
    # wgethttp://www.libgd.org/releases/oldreleases/gd-2.0.34.tar.gz
    # tar xzvf gd-2.0.34.tar.gz
    # cd gd-2.0.34
    # ./configure --prefix=/usr/local/gd2
    # make
    # make install
    (2) LibXML2
    # cd /usr/local/src
    # wget [url=ftp://xmlsoft.org/libxml2/libxml2-2.6.29.tar.gz]ftp://xmlsoft.org/libxml2/libxml2-2.6.29.tar.gz[/url]
    # tar xzvf libxml2-2.6.29.tar.gz
    # cd libxml2-2.6.29
    # ./configure --prefix=/usr/local/libxml2
    # make
    # make install
    (3) LibMcrypt
    # cd /usr/local/src
    #wgethttp://jaist.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.bz2
    # tar xjvf libmcrypt-2.5.8.tar.bz2
    # cd libmcrypt-2.5.8
    # ./configure –prefix=/usr/local/libmcrypt
    # make
    # make install
    (4) Apache 日志截?cái)喑绦?BR>    # cd /usr/local/src
    # wgethttp://cronolog.org/download/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
    8.升級(jí)OpenSSL和OpenSSH
    # cd /usr/local/src
    # wgethttp://www.openssl.org/source/openssl-0.9.8e.tar.gz
    # wgethttp://mirror.mcs.anl.gov/openssh/portable/openssh-4.6p1.tar.gz
    # tar xzvf openssl-0.9.8e.tar.gz
    # cd openssl-0.9.8e
    # ./config --prefix=/usr/local/openssl
    # make
    # make test
    # make install
    # cd ..
    # tar xzvf openssh-4.6p1.tar.gz
    # cd openssh-4.6p1
    # ./configure \
    "--prefix=/usr" \
    "--with-pam" \
    "--with-zlib" \
    "--sysconfdir=/etc/ssh" \
    "--with-ssl-dir=/usr/local/openssl" \
    "--with-md5-passwords"
    # make
    # make install
    (1)禁用 SSH V1 協(xié)議
    找到:
    #Protocol 2,1
    改為:
    Protocol 2
    (2)禁止root直接登錄
    此處先建立一個(gè)普通系統(tǒng)用戶:
    # useradd username
    # passwd username
    找到:
    #PermitRootLogin yes
    改為:
    PermitRootLogin no
    (3)禁用服務(wù)器端GSSAPI
    找到以下兩行,并將它們注釋:
    GSSAPIAuthentication yes
    GSSAPICleanupCredentials yes
    (4)禁用 DNS 名稱解析
    找到:
    #UseDNS yeas
    改為:
    UseDNS no
    (5)禁用客戶端 GSSAPI
    # vi /etc/ssh/ssh_config
    找到:
    GSSAPIAuthentication yes
    將這行注釋掉。
    最后,確認(rèn)修改正確后重新啟動(dòng) SSH 服務(wù)
    # service sshd restart
    # ssh -v
    確認(rèn) OpenSSH 以及 OpenSSL 版本正確。   三、編譯安裝L.A.M.P環(huán)境
    1. 下載軟件
    # cd /usr/local/src
    # wgethttp://apache.mirror.phpchina.com/httpd/httpd-2.2.4.tar.bz2
    # wgethttp://download.discuz.net/env/mysql-5.0.27.tar.gz
    # wgethttp://cn.php.net/distributions/php-5.2.3.tar.bz2
    #wgethttp://downloads.phpchina.com/zend/optimizer/3.3.0/ZendOptimizer-3.3.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" \ (注意:/var 分區(qū)是否適合?)
    "--with-comment=Source" \
    "--with-server-suffix=-Comsenz" \
    "--with-mysqld-user=mysql" \
    "--without-debug" \
    "--with-big-tables" \
    "--with-charset=" \ (此處設(shè)置MySQL默認(rèn)字符集)
    "--with-collation= " \ (此處設(shè)置MySQL校正字符集)
    "--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
    # chkconfig --level 3 mysqld on
    # /etc/rc.d/init.d/mysqld start
    # bin/mysqladmin -u root password 'password_for_root'
    3. 編譯安裝Apache
    # cd /usr/local/src
    # tar xjvf httpd-2.2.4.tar.bz2
    # cd httpd-2.2.4
    # ./configure \
    "--prefix=/usr/local/apache2" \
    "--with-included-apr" \
    "--enable-so" \
    "--enable-deflate=shared" \
    "--enable-expires=shared" \
    "--enable-rewrite=shared" \
    "--enable-static-support" \
    "--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.3.tar.bz2
    # cd php-5.2.3
    # ./configure \
    "--prefix=/usr/local/php" \
    "--with-apxs2=/usr/local/apache2/bin/apxs" \
    "--with-config-file-path=/usr/local/php/etc" \
    "--with-mysql=/usr/local/mysql" \
    "--with-libxml-dir=/usr/local/libxml2" \
    "--with-gd=/usr/local/gd2" \
    "--with-jpeg-dir" \
    "--with-png-dir" \
    "--with-bz2" \
    "--with-freetype-dir" \
    "--with-iconv-dir" \
    "--with-zlib-dir " \
    "--with-openssl=/usr/local/openssl" \
    "--with-mcrypt=/usr/local/libmcrypt" \
    "--enable-soap" \
    "--enable-gd-native-ttf" \
    "--enable-memory-limit" \
    "--enable-ftp" \
    "--enable-mbstring" \
    "--enable-exif" \
    "--disable-ipv6" \
    "--disable-cgi" \
    "--disable-cli"
    # make
    # make install
    # mkdir /usr/local/php/etc
    # cp php.ini-dist /usr/local/php/etc/php.ini
    5. 安裝Zend Optimizer
    # cd /usr/local/src
    # tar xzvf ZendOptimizer-3.2.8-linux-glibc21-i386.tar.gz
    # ./ZendOptimizer-3.2.8-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
    找到:
    
    DirectoryIndex index.html
    

    將該行改為
    
    DirectoryIndex index.html index.htm index.php
    

    找到:
    #Include conf/extra/httpd-mpm.conf
    #Include conf/extra/httpd-info.conf
    #Include conf/extra/httpd-vhosts.conf
    #Include conf/extra/httpd-default.conf
    去掉前面的“#”號(hào),取消注釋。
    注意:以上 4 個(gè)擴(kuò)展配置文件中的設(shè)置請(qǐng)按照相關(guān)原則進(jìn)行合理配置!
    修改完成后保存退出。
    # /usr/local/apache2/bin/apachectl restart
    7. 查看確認(rèn)L.A.M.P環(huán)境信息、提升 PHP 安全性
    在網(wǎng)站根目錄放置 phpinfo.php 腳本,檢查phpinfo中的各項(xiàng)信息是否正確。
    #vi phpinfo.php
        phpinfo();
    ?>
    確認(rèn) PHP 能夠正常工作后,在 php.ini 中進(jìn)行設(shè)置提升 PHP 安全性。
    # vi /etc/php.ini
    找到:
    disable_functions =
    設(shè)置為:
    phpinfo,passthru,exec,system,chroot,scandir,chgrp,chown,escapeshellcmd,escapeshellarg,shell_exec,proc_open,proc_get_status,error_log,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,leak,popepassthru,stream_socket_server
    四、服務(wù)器安全性設(shè)置
    1. 設(shè)置系統(tǒng)防火墻
    # touch /usr/local/sbin/fw.sh
    將以下腳本命令(綠色部分)粘貼到 fw.sh 文件中。
    #!/bin/bash
    # Stop iptables service first
    service iptables stop
    # Load FTP Kernel modules
    /sbin/modprobe ip_conntrack_ftp
    /sbin/modprobe ip_nat_ftp
    # Inital chains default policy
    /sbin/iptables -F -t filter
    /sbin/iptables -P INPUT DROP
    /sbin/iptables -P OUTPUT ACCEPT
    # Enable Native Network Transfer
    /sbin/iptables -A INPUT -i lo -j ACCEPT
    # Accept Established Connections
    /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    # ICMP Control
    /sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
    # WWW Service
    /sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    # FTP Service
    /sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    # SSH Service
    /sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    # chmod 755 /usr/local/sbin/fw.sh
    # echo '/usr/local/sbin/fw.sh' >> /etc/rc.local
    # /usr/local/sbin/fw.sh