高枫 發表於 2011-9-28 23:31:58

Linux 环境搭建推荐教程(php运行环境)

<strong>一、系统约定 <br /></strong>软件源代码包存放位置:/usr/local/src <br />源码包编译安装位置(prefix):/usr/local/software_name <br />脚本以及维护程序存放位置:/usr/local/sbin <br />MySQL 数据库位置:/var/lib/MySQL(可按情况设置) <br />Apache 网站根目录:/home/www/wwwroot(可按情况设置) <br />Apache 虚拟主机日志根目录:/home/www/logs(可按情况设置) <br />Apache 运行账户:www:www <br /><strong>二、系统环境部署及调整 <br /></strong>1. 检查系统是否正常 <br /># more /var/log/messages(检查有无系统级错误信息) <br /># dmesg(检查硬件设备是否有错误信息) <br /># ifconfig(检查网卡设置是否正确) <br /># ping www.163.com(检查网络是否正常) <br />2. 关闭不需要的服务 <br /># ntsysv <br />以下仅列出需要启动的服务,未列出的服务一律推荐关闭: <br />atd <br />crond <br />irqbalance <br />microcode_ctl <br />network <br />sendmail <br />sshd <br />syslog <br />3. 重新启动系统 <br /># init 6 <br />4. 配置 vim <br /># vi /root/.bashrc <br />在 alias mv='mv -i' 下面添加一行:alias vi='vim' 保存退出。 <br /># echo 'syntax on' &gt; /root/.vimrc <br />5. 使用 yum 程序安装所需开发包(以下为标准的 RPM 包名称) <br /># yum install ntp vim-enhanced gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel kernel <br />6. 定时校正服务器时钟,定时与中国国家授时中心授时服务器同步 <br /># crontab -e <br />加入一行: <br />*/30 * * * * ntpdate 210.72.145.44 <br />7. 源码编译安装所需包 (Source) <br />(1) GD2 <br /># cd /usr/local/src <br /># tar xzvf gd-2.0.34.tar.gz <br /># cd gd-2.0.34 <br /># ./configure --prefix=/usr/local/gd2 <br /># make <br /># make install <br />(2) LibXML2 <br /># cd /usr/local/src <br /># tar xjvf libxml2-2.6.30.tar.bz2 <br /># cd libxml2-2.6.30 <br /># ./configure --prefix=/usr/local/libxml2 <br /># make <br /># make install <br />(3) LibMcrypt <br /># cd /usr/local/src <br /># tar xjvf libmcrypt-2.5.8.tar.bz2 <br /># cd libmcrypt-2.5.8 <br /># ./configure &ndash;prefix=/usr/local/libmcrypt <br /># make <br /># make install <br />(4) Apache日志截断程序 <br /># cd /usr/local/src <br /># tar xzvf cronolog-1.7.0-beta.tar.gz <br /># cd cronolog-1.7.0-beta <br /># ./configure &ndash;prefix=/usr/local/cronolog <br /># make <br /># make install <br />8. 升级OpenSSL和OpenSSH <br /># cd /usr/local/src <br /># tar xzvf openssl-0.9.8e.tar.gz <br /># cd openssl-0.9.8e <br /># ./config --prefix=/usr/local/openssl <br /># make <br /># make test <br /># make install <br /># cd .. <br /># tar xzvf openssh-4.7p1.tar.gz <br /># cd openssh-4.7p1 <br /># ./configure \ <br />&quot;--prefix=/usr&quot; \ <br />&quot;--with-pam&quot; \ <br />&quot;--with-zlib&quot; \ <br />&quot;--sysconfdir=/etc/ssh&quot; \ <br />&quot;--with-ssl-dir=/usr/local/openssl&quot; \ <br />&quot;--with-md5-passwords&quot; <br /># make <br /># make install <br />(1)禁用 SSH V1 协议 <br />找到: <br />#Protocol 2,1 <br />改为: <br />Protocol 2 <br />(2)禁止root直接登录 <br />此处先建立一个普通系统用户: <br /># useradd username <br /># passwd username <br />找到: <br />#PermitRootLogin yes <br />改为: <br />PermitRootLogin no <br />(3)禁用服务器端GSSAPI <br />找到以下两行,并将它们注释: <br />GSSAPIAuthentication yes <br />GSSAPICleanupCredentials yes <br />(4)禁用 DNS 名称解析 <br />找到: <br />#UseDNS yes <br />改为: <br />UseDNS no <br />(5)禁用客户端 GSSAPI <br /># vi /etc/ssh/ssh_config <br />找到: <br />GSSAPIAuthentication yes <br />将这行注释掉。 <br />最后,确认修改正确后重新启动 SSH 服务 <br /># service sshd restart <br /># ssh -v <br />确认 OpenSSH 以及 OpenSSL 版本正确。 <br /><strong>三、编译安装L.A.M.P环境 <br /></strong>1. 下载软件 <br /># cd /usr/local/src <br />httpd-2.2.6.tar.bz2 <br />MySQL-5.0.45-linux-i686-glibc23.tar.gz <br />php-5.2.4.tar.bz2 <br />ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz <br />2. 安装MySQL <br /># tar xzvf MySQL-5.0.45-linux-i686-glibc23.tar.gz <br /># mv MySQL-5.0.45-linux-i686-glibc23 /usr/local/ <br /># ln -s /usr/local/ MySQL-5.0.45-linux-i686-glibc23 /usr/local/MySQL <br /># useradd MySQL <br /># chown -R MySQL:root /usr/local/MySQL/ <br /># cd /usr/local/MySQL <br /># ./scripts/MySQL_install_db --user=MySQL <br /># cp ./support-files/MySQL.server /etc/rc.d/init.d/MySQLd <br /># chown root:root /etc/rc.d/init.d/MySQLd <br /># chmod 755 /etc/rc.d/init.d/MySQLd <br /># chkconfig --add MySQLd <br /># chkconfig --level 3 5 MySQLd on <br /># cp ./support-files/my-huge.cnf /etc/my.cnf <br /># mv /usr/local/MySQL/data /var/lib/MySQL <br /># chown -R MySQL:MySQL /var/lib/MySQL/ <br /># vi /etc/my.cnf <br />修改以下内容: <br />(1) 在 段增加一行: <br />default-character-set = GBK | latin1 | utf8 | BIG5 <br />(2) 在 段增加或修改: <br />datadir = /var/lib/MySQL <br />--skip-innodb <br />default-character-set = GBK | latin1 | utf8 | BIG5 <br />--wait-timeout = 3 | 5 | 10 <br />max_connections = 256 | 384 | 512 <br />max_connect_errors = 10000000 <br />thread_concurrency = CPU个数&times;2 <br />将 log-bin 注释 <br /># bin/MySQLadmin -u root password 'password_for_root' <br />3. 编译安装Apache <br /># cd /usr/local/src <br /># tar xjvf httpd-2.2.6.tar.bz2 <br /># cd httpd-2.2.6 <br /># ./configure \ <br />&quot;--prefix=/usr/local/apache2&quot; \ <br />&quot;--with-included-apr&quot; \ <br />&quot;--enable-so&quot; \ <br />&quot;--enable-deflate=shared&quot; \ <br />&quot;--enable-expires=shared&quot; \ <br />&quot;--enable-rewrite=shared&quot; \ <br />&quot;--enable-static-support&quot; \ <br />&quot;--disable-userdir&quot; <br /># make <br /># make install <br /># echo '/usr/local/apache2/bin/apachectl start ' &gt;&gt; /etc/rc.local <br />4. 编译安装PHP <br /># cd /usr/local/src <br /># tar xjvf php-5.2.4.tar.bz2 <br /># cd php-5.2.4 <br /># ./configure \ <br />&quot;--prefix=/usr/local/php&quot; \ <br />&quot;--with-apxs2=/usr/local/apache2/bin/apxs&quot; \ <br />&quot;--with-config-file-path=/usr/local/php/etc&quot; \ <br />&quot;--with-MySQL=/usr/local/MySQL&quot; \ <br />&quot;--with-libxml-dir=/usr/local/libxml2&quot; \ <br />&quot;--with-gd=/usr/local/gd2&quot; \ <br />&quot;--with-jpeg-dir&quot; \ <br />&quot;--with-png-dir&quot; \ <br />&quot;--with-bz2&quot; \ <br />&quot;--with-freetype-dir&quot; \ <br />&quot;--with-iconv-dir&quot; \ <br />&quot;--with-zlib-dir &quot; \ <br />&quot;--with-openssl=/usr/local/openssl&quot; \ <br />&quot;--with-mcrypt=/usr/local/libmcrypt&quot; \ <br />&quot;--enable-soap&quot; \ <br />&quot;--enable-gd-native-ttf&quot; \ <br />&quot;--enable-ftp&quot; \ <br />&quot;--enable-mbstring&quot; \ <br />&quot;--enable-exif&quot; \ <br />&quot;--disable-ipv6&quot; \ <br />&quot;--disable-cgi&quot; \ <br />&quot;--disable-cli&quot; <br /># make <br /># make install <br /># mkdir /usr/local/php/etc <br /># cp php.ini-dist /usr/local/php/etc/php.ini <br />5. 安装Zend Optimizer <br /># cd /usr/local/src <br /># tar xzvf ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz <br /># ./ZendOptimizer-3.3.0-linux-glibc21-i386/install.sh <br />安装Zend Optimizer过程的最后不要选择重启Apache。 <br />6. 整合Apache与PHP <br /># vi /usr/local/apache2/conf/httpd.conf <br />找到: <br />AddType application/x-gzip .gz .tgz <br />在该行下面添加 <br />AddType application/x-httpd-php .php <br />找到: <br />DirectoryIndex index.html <br />将该行改为 <br />DirectoryIndex index.html index.htm index.php <br />找到: <br />#Include conf/extra/httpd-mpm.conf <br />#Include conf/extra/httpd-info.conf <br />#Include conf/extra/httpd-vhosts.conf <br />#Include conf/extra/httpd-default.conf <br />去掉前面的&ldquo;#&rdquo;号,取消注释。 <br />注意:以上 4 个扩展配置文件中的设置请按照相关原则进行合理配置! <br />修改完成后保存退出。 <br /># /usr/local/apache2/bin/apachectl restart <br />7. 查看确认L.A.M.P环境信息、提升 PHP 安全性 <br />在网站根目录放置 phpinfo.php 脚本,检查phpinfo中的各项信息是否正确。 <br />确认 PHP 能够正常工作后,在 php.ini 中进行设置提升 PHP 安全性。 <br /># vi /etc/php.ini <br />找到: <br />disable_functions = <br />设置为: <br />phpinfo,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server <br /><strong>四、服务器安全性设置</strong> <br />1. 设置系统防火墙 <br /># vi /usr/local/sbin/fw.sh <br />将以下脚本命令粘贴到 fw.sh 文件中。 <br />#!/bin/bash <br /># Stop iptables service first <br />service iptables stop <br /># Load FTP Kernel modules <br />/sbin/modprobe ip_conntrack_ftp <br />/sbin/modprobe ip_nat_ftp <br /># Inital chains default policy <br />/sbin/iptables -F -t filter <br />/sbin/iptables -P INPUT DROP <br />/sbin/iptables -P OUTPUT ACCEPT <br /># Enable Native Network Transfer <br />/sbin/iptables -A INPUT -i lo -j ACCEPT <br /># Accept Established Connections <br />/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT <br /># ICMP Control <br />/sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT <br /># WWW Service <br />/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT <br /># FTP Service <br />/sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT <br /># SSH Service <br />/sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT <br /># chmod 755 /usr/local/sbin/fw.sh <br /># echo '/usr/local/sbin/fw.sh' &gt;&gt; /etc/rc.local <br /># /usr/local/sbin/fw.sh
頁: [1]
查看完整版本: Linux 环境搭建推荐教程(php运行环境)