yum -y install vim-enhanced mv /bin/vi /bin/vi.save ln -s /usr/bin/vim /bin/vi cp /etc/vimrc /etc/vimrc.save sed -i “39 s/^/ set number \n filetype on\n set history=1000\n syntax on\n set tabstop=4\n set showmatch\n set vb t_vb=\n set mouse=a\n set ignorecase\n set autowrite\n /” /etc/vimrc
cd /usr/local/src vi install.sh ###############################################
#安裝openssl cd /usr/local/src tar zxvf openssl-0.9.8h.tar.gz cd openssl-0.9.8h ./config shared zlib make make test make install mv /usr/bin/openssl /usr/bin/openssl.save mv /usr/include/openssl /usr/include/openssl.save mv /usr/lib/libssl.so /usr/lib/libssl.so.save ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl ln -s /usr/local/ssl/include/openssl /usr/include/openssl ln -sv /usr/local/ssl/lib/libssl.so.0.9.8 /usr/lib/libssl.so cd ..
#!/bin/bash # # Startup script for the Apache Web Server # # chkconfig: - 85 15 # description: Apache is a World Wide Web server. It is used to serve \ # HTML files and CGI. # processname: httpd # pidfile: /usr/local/apache2/logs/httpd.pid # config: /usr/local/apache2/conf/httpd.conf
# Source function library. . /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi
# This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS=”"
# Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/local/apache2/bin/apachectl httpd=/usr/local/apache2/bin/httpd pid=/usr/local/apache2/logs/httpd.pid prog=httpd RETVAL=0
# The semantics of these two functions differ from the way apachectl does # things — attempting to start while running is a failure, and shutdown # when not running is also a failure. So we just do it the way init scripts # are expected to behave here. start() { echo -n $”Starting $prog: “ daemon $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] touch /var/lock/subsys/httpd return $RETVAL } stop() { echo -n $”Stopping $prog: “ killproc $httpd RETVAL=$? echo [ $RETVAL = 0 ] rm -f /var/lock/subsys/httpd $pid } reload() { echo -n $”Reloading $prog: “ killproc $httpd -HUP RETVAL=$? echo }
# See how we were called. case “$1″ in start) start ;; stop) stop ;; status) status $httpd RETVAL=$? ;; restart) stop start ;; condrestart) if [ -f $pid ] ; then stop start fi ;; reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $”Usage: $prog {start|stop|restart|condrestart|reload|status” echo $”|fullstatus|graceful|help|configtest}” exit 1 esac
vi /usr/local/apache2/conf/extra/httpd-default.conf
Timeout 15 KeepAlive Off MaxKeepAliveRequests 50 KeepAliveTimeout 5 UseCanonicalName Off AccessFileName .htaccess ServerTokens Prod ServerSignature Off HostnameLookups Off
cd /usr/local/src tar zxvf phpMyAdmin-2.11.8.1-all-languages-utf-8-only.tar.gz cp -rf phpMyAdmin-2.11.8.1-all-languages-utf-8-only /data/www/wwwroot/test.com/phpmyadmin cd /data/www/wwwroot/test.com/phpmyadmin
cp config.sample.inc.php config.inc.php
sed -i -e “/^\$cfg\['blowfish_secret'\]/{ s@”;@’88888888888888888′;@; }” config.inc.php
3、系統(tǒng)環(huán)境部署及調(diào)整 (1)檢查系統(tǒng)是否正常 # more /var/log/messages //檢查有無系統(tǒng)內(nèi)核級錯誤信息 # dmesg //檢查硬件設(shè)備是否有錯誤信息 # ifconfig //檢查網(wǎng)卡設(shè)置是否正確 # ping www.163.com // 檢查網(wǎng)絡(luò)是否正常 (2)關(guān)閉不需要的服務(wù) # export LANG='en_US' //設(shè)置語言 # setup //選擇啟動的服務(wù) 進入system service 選項。 以space 鍵選定所需服務(wù)。 以下僅列出需要啟動的服務(wù),未列出的服務(wù)一律關(guān)閉: crond irqbalance 僅當(dāng)服務(wù)器CPU為S.M.P架構(gòu)或支持雙核心、HT技術(shù)時,才需開啟,否則關(guān)閉。 microcode_ctl network iptables vsftpd sshd syslog yum-updatesd (3)修改/etc/yum.repos.d/CentOS-Base.repo,將鏡象站點地址改為在中國的鏡象站點地址。不然我們通過yum安裝軟件速度會極慢。修改如下: # CentOS-Base.repo # # This file uses a new mirrorlist system developed by Lance Davis for CentOS. # The mirror system uses the connecting IP address of the client and the # update status of each mirror to pick mirrors that are updated to and # geographically close to the client. You should use this for CentOS updates # unless you are manually picking other mirrors. # # If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead. # # [base] name=CentOS-$releasever - Base baseurl=http://mirrors.shlug.org/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5 protect=1 #released updates [updates] name=CentOS-$releasever - Updates baseurl=http://mirrors.shlug.org/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5 protect=1 #packages used/produced in the build but not released [addons] name=CentOS-$releasever - Addons baseurl=http://mirrors.shlug.org/centos/$releasever/addons/$basearch/ gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5 protect=0 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras baseurl=http://mirrors.shlug.org/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5 protect=0 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus baseurl=http://mirrors.shlug.org/centos/$releasever/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5 protect=1 #contrib - packages by Centos Users [contrib] name=CentOS-$releasever - Contrib baseurl=http://mirrors.shlug.org/centos/$releasever/contrib/$basearch/ gpgcheck=1 enabled=0 protect=0 gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5 保存文件。 (4)更新系統(tǒng),我們使用yum, 運行: # yum upgrade 建議更新所有列出的程序,包括內(nèi)核,rhel 5.X的穩(wěn)定性還要繼續(xù)努力呢。 (5)定時校正服務(wù)器時間 # yum install –y ntp # crontab -e 0 23 * * * /usr/sbin/ntpdate 210.72.145.44 以上命令設(shè)置好后存盤。 # /sbin/service crond reload 重載計劃任務(wù)配置 您的機器將在每天的23:00根據(jù)中國國家授時中心的NTP服務(wù)器時間自動校準時間。 (6)FTP服務(wù)器的配置 vi /etc/vsftpd/vsftpd.conf 把anonymous_enable=YES 修改為anonymous_enable=No不允許匿名登錄。 添加兩句 chroot_local_user=yes listen_port=2121 將用戶鎖定在主目錄,并將FTP監(jiān)聽端口修改為2121 把ftpd_banner=*前的注釋去掉。后面改成你的歡迎信息(這樣設(shè)置可以避免顯示ftp服務(wù)器的版本信息) 然后保存,service vsftpd start就可以了。 這時應(yīng)當(dāng)添加用戶,因為root默認不能通過FTP方式登錄,也不安全。 groupadd upload useradd upload -g upload -d /usr/local/apache2/htdocs/ -M 如果FTP登錄時出現(xiàn) ftp服務(wù)器連接失敗,錯誤提示: 500 OOPS: cannot change directory:/home/******* 500 OOPS: child died 解決方法: # setsebool ftpd_disable_trans 1 # service vsftpd restart 這樣對于我們上傳一些文件到系統(tǒng)中很方便。 大家有興趣的可以看下這篇文章對vsftpd進行一個比較詳細的了解 http://blog.chinaunix.net/u/10047/showart_198837.html 4、重新啟動系統(tǒng) # init 6 此時系統(tǒng)啟動成功,可以刪除老的內(nèi)核
7、編譯mysql 5.0.50 mysql 5.0.50是企業(yè)版本,貌似雙數(shù)版本都是企業(yè)版本了。個人覺得代碼質(zhì)量要比社區(qū)版本要好一些。大家可以下載,免費使用。并不需要向mysql公司交錢。 #cd /usr/local/src # wget http://mirror.provenscaling.com/mysq...-5.0.50.tar.gz # tar xzvf mysql-5.0.50.tar.gz # cd mysql-5.0.50 修改mysql 客戶端最大連接數(shù), 默認的只有100,遠遠達不到我們的要求。 # vi sql/mysqld.cc 搜索找到下面一行: {"max_connections", OPT_MAX_CONNECTIONS, "The number of simultaneous clients allowed.", (gptr*) max_connections, (gptr*) max_connections, 0, GET_ULONG, REQUIRED_ARG, 100, 1, 16384, 0, 1, 0}, 將其中的100改為1500, 當(dāng)然小點也可以,根據(jù)你的需要來,不建議改的太大。 {"max_connections", OPT_MAX_CONNECTIONS, "The number of simultaneous clients allowed.", (gptr*) max_connections, (gptr*) max_connections, 0, GET_ULONG, REQUIRED_ARG, 1500, 1, 16384, 0, 1, 0}, 保存。 # CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-comment=Source --with-server-suffix=-enterprise-gpl --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,latin1 --with-pthread --enable-static --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-innodb --without-ndb-debug --without-isam --enable-local-infile --with-readline --with-raid 配置成功會提示: MySQL has a Web site at http://www.mysql.com/ which carries details on the latest release, upcoming features, and other information to make your work or play with MySQL more productive. There you can also find information about mailing lists for MySQL discussion. Remember to check the platform specific part of the reference manual for hints about installing MySQL on your platform. Also have a look at the files in the Docs directory. Thank you for choosing MySQL! # make 編譯的時間可能會比較長,畢竟優(yōu)化的比較厲害。 # make install 編譯安裝完成后執(zhí)行后續(xù)操作: # useradd mysql //添加 mysql 用戶 # cd /usr/local/mysql # bin/mysql_install_db --user=mysql # chown -R root:mysql . //設(shè)置權(quán)限,注意后面有一個 "." # chown -R mysql /var/lib/mysql //設(shè)置 mysql 目錄權(quán)限 # chgrp -R mysql . //注意后面有一個 "." # cp share/mysql/my-medium.cnf /etc/my.cnf # cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld //開機自動啟動 mysql。 # chmod 755 /etc/rc.d/init.d/mysqld # chkconfig --add mysqld #添加LIB PATH echo "/usr/local/mysql/lib" >> /etc/ld.so.conf ldconfig vi /etc/my.cnf 修改 MySQL 配置,增加部分優(yōu)化參數(shù),如下: [mysqld] ft_min_word_len=2 運行以下命令即可啟動 MySQL 服務(wù)器: # /etc/rc.d/init.d/mysqld start //啟動 MySQL # bin/mysqladmin -u root password "password_for_root" # service mysqld stop //關(guān)閉 MySQL
9、編譯php 5.2.5 Suhosin是php增強型安全補丁,可以編譯到靜態(tài)內(nèi)核中,也可以編譯成php動態(tài)擴展。我個人強烈你建議安裝成靜態(tài)內(nèi)核。Suhosin已經(jīng)進入 Gentoo Linux、FreeBSD、OpenSuSE Linux、Mandriva Linux、Debian Linux官方包。下面的以下先說靜態(tài)安裝步驟。當(dāng)然你也可以在安裝php后將它編譯成php的動態(tài)擴展。 # cd /usr/local/src # wget http://cn.php.net/get/php-5.2.5.tar.gz/from/this/mirror wget http://www.hardened-php.net/suhosin/...9.6.2.patch.gz # tar zxvf php-5.2.5.tar.gz # gunzip suhosin-patch-5.2.5-0.9.6.2.patch.gz # cd php-5.2.5 # patch -p 1 -i ../suhosin-patch-5.2.5-0.9.6.2.patch # ./buildconf --force # CHOST="i686-pc-linux-gnu" CFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -mcpu=pentium4 -march=pentium4 -pipe -fomit-frame-pointer" CXXFLAGS="-O3 -msse2 -mmmx -Wall -W -mfpmath=sse -funroll-loops -mcpu=pentium4 -march=pentium4 -pipe -felide-constructors -fno-exceptions -fno-rtti -fomit-frame-pointer" ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-pear=/usr/share/php --with-zlib-dir --with-bz2 --with-libxml-dir=/usr --with-gd=/usr/local/gd2 --enable-gd-native-ttf --enable-gd-jis-conv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-ttf=shared,/usr --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-config-file-path=/etc --with-iconv --disable-ipv6 --enable-static --enable-zend-multibyte --enable-inline-optimization --enable-zend-multibyte --enable-sockets --enable-soap --with-openssl --with-gettext --enable-suhosin 配置成功會提示: +--------------------------------------------------------------------+ | License: | | This software is subject to the PHP License, available in this | | distribution in the file LICENSE. By continuing this installation | | process, you are bound by the terms of this license agreement. | | If you do not agree with the terms of this license, you must abort | | the installation process at this point. | +--------------------------------------------------------------------+ Thank you for using PHP. # make # make test # make install # cp php.ini-recommended /etc/php.ini # echo "/usr/local/php/lib" >> /etc/ld.so.conf ldconfig 在這里也順便說一下將suhosin安裝成為php的動態(tài)擴展的方法。畢竟網(wǎng)上根本不見它的中文安裝教程。 雖然我個人不推薦這種方式。 wget http://www.hardened-php.net/suhosin/...sin-0.9.20.tgz tar zxvf suhosin-0.9.20.tgz cd suhosin-0.9.20 ./configure --with-php-config=/usr/local/php/bin/php-config make make install 會提示編譯的模塊存在的目錄,記住它。 Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20060613/ 然后在php.ini中增加一行下列語句。 extension="/usr/local/php/lib/php/extensions/no-debug-zts-20060613/suhosin.so"
10、整合apache 與php # vi /usr/local/apache2/conf/httpd.conf 在最后一行加上: AddType application/x-httpd-php .php 查找:(設(shè)置 WEB 默認文件) DirectoryIndex index.html 修改為: DirectoryIndex index.php index.html index.htm 找到這一段: # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride none 更改為AllowOverride all 允許apache rewrite 保存httpd.conf,退出。 # /usr/local/apache2/bin/apachectl restart //重啟 Apache 這時會出現(xiàn)錯誤: /usr/local/apache2/bin/apachectl start httpd: Syntax error on line 107 of /usr/local/apache2/conf/httpd.conf: Cannot load /usr/local/apache2/modules/libphp5.so into server: /usr/local/apache2/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied 不急,我們慢慢解決。 這個Permission denied問題,在centos 5下面一般是Selinux引起的,作為生產(chǎn)用服務(wù)器,我建議你千萬別草率地關(guān)掉Selinux一了百了。就像家里的防盜網(wǎng),阻礙了你的貓自由進出窗戶,你不能為了貓方便,就把防盜網(wǎng)簡單拆除是同樣的道理。我看見網(wǎng)上許多人建議把Selinux簡單關(guān)閉來解決這個問題,這是削足適履的做法,不值得提倡。 我們可以這樣操作: # audit2allow -a //查看究竟問題出在什么地方
allow unconfined_t usr_t:file execmod; allow useradd_t var_log_t:file { read write }; 然后 # cd /etc/selinux/targeted/modules/ # audit2allow -M local -d 屏幕產(chǎn)生如下提示: Generating type enforcment file: local.te Compiling policy checkmodule -M -m -o local.mod local.te semodule_package -o local.pp -m local.mod ******************** IMPORTANT *********************** In order to load this newly created policy package into the kernel, you are required to execute semodule -i local.pp 我們運行 # semodule -i local.pp 這樣就讓Selinux加載了新的規(guī)則。 更詳細的內(nèi)容請看我在BLOG上的轉(zhuǎn)貼: http://www.cnprint.org/bbs/blogs/1/blog48.html 重啟apache 哈哈,apache不會再報錯了吧? 這樣我保留了selinux的功能,同時apache也能正常運行。 PHP5.1.x開始需要設(shè)置時區(qū),默認時區(qū)與中國時區(qū)差8個小時,這種情況需要在php.ini中這么設(shè)置,找到date.timezone,去掉前面的分號,修改為以下值,大陸地區(qū)可用的值是:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次為重慶,上海,烏魯木齊) 不然一些php程序的時間老是和中國標準時間相差8個小時。我的我的VBB論壇在windows上就是這樣。這兒有亞洲地區(qū)的對應(yīng)時區(qū)。 http://www.php.net/manual/en/timezones.asia.php 11、安裝 Zend Optimizer # cd /usr/local/src # wget http://downloads.zend.com/optimizer/...21-i386.tar.gz # tar xzvf ZendOptimizer-3.3.0-linux-glibc21-i386.tar.gz # ./ZendOptimizer-3.3.0-linux-glibc21-i386/install.sh 按照它的提示一步步進行就行了。 總之一句話。如果你的服務(wù)器環(huán)境不需要ZendOptimizer,那么能不安就不安裝這個。避免和eaccelerator沖突。 12. 查看確認 L.A.M.P 環(huán)境信息 vi /usr/local/apache2/htdocs/phpinfo.php 新增加下面一行,并保存。
//用于提供perl訪問mysql數(shù)據(jù)庫的接口規(guī)范,請確認你已經(jīng)安裝了perl,一般默認系統(tǒng)都裝上了。 因為我們要用到mysqlhotcopy功能,需要這兩個小程序的支持。 # wget http://mirrors.xueron.com/CPAN/autho...I-1.601.tar.gz 首先,安裝DBI包: # tar zxvf DBI-1.061.tar.gz # cd DBI-1.061 # perl Makefile.PL # make # make test # make install
wget http://search.cpan.org/CPAN/authors/...l-4.005.tar.gz # tar zxvf DBD-mysql-4.005.tar.gz # cp /usr/local/mysql/lib/mysql/libmysqlclient.so.15 /usr/lib/ # perl Makefile.PL --libs="-L/usr/local/mysql/lib/mysql -lmysqlclient -L/usr/lib -lz " --cflags=-I/usr/local/mysql/include/mysql --mysql_config=/usr/local/mysql/bin/mysql_config --testhost=127.0.0.1--testsocket=/tmp/mysql.sock --testdb=test --testuser=root --testpassword="youpassword" # make # make test # make instll 測試執(zhí)行 mysqlhotcopy 並出現(xiàn)如下類似錯誤訊息 #/usr/local/mysql/bin/mysqlhotcopy mysql /tmp/test -u root -p 'password' Invalid db.table name 'mysql.mysql`.`activity' at /usr/local/bin/mysqlhotcopy line 855. Ans: 找到了 [MySQL Bugs: #27303: mysqlhotcopy dies with error Invalid db.table name 'foo.bar`.`baz'] 說明將 mysqlhotcopy 文件修改一下后即可順利執(zhí)行 mysqlhotcopy 了 #vi /usr/local/mysql/bin/mysqlhotcopy //在第 835 下新增一行 835 my @dbh_tables = eval { $dbh->tables() }; 836 map { s/^.*?\.//o } @dbh_tables; //加入此行 mysql 5.0.50后已經(jīng)修正這個錯誤。
16、配置https vi /usr/local/apache2/conf/httpd.conf # 監(jiān)聽443端口,支持https連接 取消注釋 httpd.conf 中的 Include conf/extra/httpd-ssl.conf 設(shè)置SSL并創(chuàng)建自己的CA # cd /etc/pki/tls/misc # ./CA -newca 屏幕上出現(xiàn)如下的提示:CA certificate filename (or enter to create) 這是要求輸入要創(chuàng)建的CA的證書文件名, 可以直接回車或輸入證書文件名。 Making CA certificate ... Generating a 1024 bit RSA private key .........++++++ ................................++++++ writing new private key to './demoCA/private/./cakey.pem' Enter PEM pass phrase: Verifying password - Enter PEM pass phrase:- 此時要求輸入和驗證CA的私鑰口令、國家代碼(中國是CN)、省份、城市或地區(qū)、組織或企業(yè)名稱、部門名稱、CA的名稱或服務(wù)器的主機名稱、管理員電子郵件地址。 至此,在當(dāng)前目錄下生成了demoCA的目錄,CA的證書就在該目錄下,文件名為cacert.pem 生成服務(wù)器的證書請求 # ./CA -newreq 屏幕上出現(xiàn)如下的提示: Generating a 1024 bit RSA private key .....................................................++++++ .....++++++ writing new private key to 'newreq.pem' Enter PEM pass phrase: Verifying password - Enter PEM pass phrase: 此時要求輸入和驗證服務(wù)器的私鑰口令、國家代碼(中國是CN)、省份、城市或地區(qū)、組織或企業(yè)名稱、部門名稱、CA的名稱或服務(wù)器的主機名稱、管理員電子郵件地址。 Please enter the following 'extra' attributes to be sent with your certificaterequest A challenge password []: An optional company name []: .Request (and private key) is in newreq.pem 這是要求輸入服務(wù)器的相關(guān)信息。 此時,在當(dāng)前目錄下生成了一個名為newreq.pem的文件,包含了要生成服務(wù)器數(shù)字證書的請求。 簽署證書 # ./CA -sign 屏幕上出現(xiàn)如下的提示: Using configuration from /usr/share/ssl/openssl.cnf Enter PEM pass phrase: 此時一樣需要輸入CA的私鑰口令、國家代碼(中國是CN)、省份、城市或地區(qū)、組織或企業(yè)名稱、部門名稱、CA的名稱或服務(wù)器的主機名稱、管理員電子郵件地址。 Certificate is to be certified until Nov 19 13:46:19 2002 GMT (365 days) Sign the certificate? [y/n]:y 這時顯示證書請求文件中的各項信息,并詢問是否要簽署證書,回答y,進行簽署。 1 out of 1 certificate requests certified, commit? [y/n]y 回答y,會顯示已經(jīng)簽署的證書的信息,并在當(dāng)前目錄下生成服務(wù)器的證書文件newcert.pem。 # mkdir /usr/local/apache2/conf/ssl.crt/ # mkdir /usr/local/apache2/conf/ssl.key/ # cp newcert.pem /usr/local/apache2/conf/ssl.crt/server.pem # cp newreq.pem /usr/local/apache2/conf/ssl.key/server.pem 更改服務(wù)器的證書文件的相關(guān)配置 # vi /usr/local/apache2/conf/extra/httpd-ssl.conf 查找并修改 # Server Certificate: # Point SSLCertificateFile at a PEM encoded certificate. If # the certificate is encrypted, then you will be prompted for a # pass phrase. Note that a kill -HUP will prompt again. Keep # in mind that if you have both an RSA and a DSA certificate you # can configure both in parallel (to also allow the use of DSA # ciphers, etc.) SSLCertificateFile /usr/local/apache2/conf/ssl.crt/server.pem #SSLCertificateFile /usr/local/apache2/conf/server-dsa.crt # Server Private Key: # If the key is not combined with the certificate, use this # directive to point at the key file. Keep in mind that if # you've both a RSA and a DSA private key you can configure # both in parallel (to also allow the use of DSA ciphers, etc.) SSLCertificateKeyFile /usr/local/apache2/conf/ssl.key/server.pem #SSLCertificateKeyFile /usr/local/apache2/conf/server-dsa.key 示例文件 在SSL的根目錄中生成一個index.html,它是如下所示: <html> 這是SSL示例! </html> 測試 假如Web服務(wù)器的DNS名稱是www.cnprint.org. 在瀏覽器的URL地址欄里輸入 http://www.cnprint.org/,瀏覽器便會顯示APACHE安裝時確省的Test Page. 在瀏覽器的URL地址欄里輸入 https://www.cnprint.org/,注意:是 https 而不是http ! 瀏覽器會提示站點已經(jīng)采用了SSL進行數(shù)據(jù)的加密傳輸.由于我們的CA證書不是瀏覽器缺省的信任的根證書,所以,瀏覽器會說無法確認服務(wù)器的證書可信。暫時不管,一直NEXT,最后,瀏覽器會顯示:這是SSL示例! 可以把CA的證書放在非SSL的站點上,讓瀏覽器下載并安裝CA證書,并將其設(shè)置成可信任的根證書,便可解決上面的問題.8 解除HTTPD起動時的口令輸入。 由于安全的原因,Web服務(wù)器的私鑰是口令加密了的,每次重新起動HTTPD或Linux時,都會要求輸入Web服務(wù)器的私鑰的口令。 如果要解除HTTPD起動時的口令輸入,可以這樣: # cd /usr/local/apache2/conf/ssl.key/ # cp server.pem server.pem.org # openssl rsa -in server.pem.org -out server.pem # chmod 400 server.pem 另外在網(wǎng)上看到一個方法,我沒有試。有興趣的可以試下。 創(chuàng)建SSL密碼自動應(yīng)答文件,否則每次Apache啟動的時候,都會要求你輸入SSL的密碼. 創(chuàng)建 /usr/local/apache2/conf/ssl.key/sendsslpwd ,內(nèi)容如下. #!/bin/bash SSLpasswd="YOUR PASSPHRASE" echo $SSLpasswd chmod 755 /usr/local/apache2/conf/ssl.key/sendsslpwd 此時,Web服務(wù)器的私鑰已經(jīng)沒有口令加密,一定要確保server.pem文件除root外,任何用戶均無權(quán)讀取它。
17、安裝phpmyadmin,管理mysql數(shù)據(jù)庫 # cd /usr/local/apache2/htdocs/ # wget http://nchc.dl.sourceforge.net/sourc...-8-only.tar.gz # tar zxvf phpMyAdmin-2.11.1-all-languages-utf-8-only.tar.gz # mv phpMyAdmin-2.11.1-all-languages-utf-8-only phpmyadmin # cd phpmyadmin/libraries 修改配置文件 # vi config.default.php 找到這幾行進行修改: $cfg['Servers'][$i]['auth_type'] = 'http'; // Authentication method (valid choices: config, http, HTTP, signon or cookie) $cfg['Servers'][$i]['user'] = 'root'; // MySQL user $cfg['Servers'][$i]['password'] = 'PASSWORD'; // MySQL password (only needed
五、服務(wù)器安全配置
18、編譯安裝mod_security mod_security是一個集入侵檢測和防御引擎功能的開源web應(yīng)用安全程序(或web應(yīng)用程序防火墻)。 它以Apache Web服務(wù)器的模塊方式運行, 目標是增強web應(yīng)用程序的安全性, 防止web應(yīng)用程序受到已知或未知的攻擊。 # cd /usr/local/src # wget http://www.modsecurity.org/download/...e_2.1.3.tar.gz # tar -zxvf modsecurity-apache_2.1.3.tar.gz # cd modsecurity-apache_2.1.3/apache2 # cat /usr/local/apache2/conf/httpd.conf | grep "ServerRoot" | grep -v "#" ServerRoot "/usr/local/apache2" # #vi Makefile top_dir = /usr/local/apache2 # # make # make install vi /usr/local/apache2/conf/httpd.conf 加載下列模塊 LoadModule unique_id_module modules/mod_unique_id.so LoadModule security2_module modules/mod_security2.so 添加一行: Include conf/modsecurity/*.conf 保存。 cd /usr/local/src/modsecurity-apache_2.1.3/rules # mkdir /usr/local/apache2/conf/modsecurity # cp *.conf /usr/local/apache2/conf/modsecurity/ 根據(jù)你的需要,You may want to edit and customize modsecurity_crs_10_config.conf. Additionally you may want to edit modsecurity_crs_30_http_policy.conf which enforces an application specific HTTP protocol usage. 重啟apache
19、Iptables規(guī)則 vi /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 2121 -j ACCEPT # SSH Service /sbin/iptables -A INPUT -p tcp --dport 59825 -j ACCEPT # Anti DDOS /sbin/iptables -I INPUT -p tcp --syn -m ttl --ttl-eq 117 -j DROP /sbin/iptables -I INPUT -p tcp --syn -m length --length :40 -j DROP
21、mysql優(yōu)化及安全設(shè)置 Mysql的優(yōu)化設(shè)置 打開/etc/my.cnf文件,修改以下設(shè)置,如果沒有,可手動添加。調(diào)整設(shè)置時,請量力而行,這與你的服務(wù)器的配置有關(guān),特別是內(nèi)存大小。以下設(shè)置比較適合于1G內(nèi)存的服務(wù)器,但并不絕對。 #指定索引緩沖區(qū)的大小,它決定索引處理的速度,尤其是索引讀的速度。通過檢查狀態(tài)值Key_read_requests和Key_reads,可以知道 key_buffer_size設(shè)置是否合理。比例key_reads / key_read_requests應(yīng)該盡可能的低,至少是1:100,1:1000更好(上述狀態(tài)值可以使用show status like 'key_reads'獲得)。key_buffer_size只對MyISAM表起作用。即使你不使用MyISAM表,但是內(nèi)部的臨時磁盤表是 MyISAM表,也要使用該值。可以使用檢查狀態(tài)值created_tmp_disk_tables得知詳情。 key_buffer = 384M #要求MySQL能有的連接數(shù)量。當(dāng)主要MySQL線程在一個很短時間內(nèi)得到非常多的連接請求,這就起作用,然后主線程花些時間(盡管很短)檢查連接并且啟動一個新線程。back_log值指出在MySQL暫時停止回答新請求之前的短時間內(nèi)多少個請求可以被存在堆棧中。只有如果期望在一個短時間內(nèi)有很多連接,你需要增加它,換句話說,這值對到來的TCP/IP連接的偵聽隊列的大小。你的操作系統(tǒng)在這個隊列大小上有它自己的限制。試圖設(shè)定back_log高于你的操作系統(tǒng)的限制將是無效的。默認數(shù)值是50 back_log = 200 #一個包的最大尺寸。消息緩沖區(qū)被初始化為net_buffer_length字節(jié),但是可在需要時增加到max_allowed_packet個字節(jié)。缺省地,該值太小必能捕捉大的(可能錯誤)包。如果你正在使用大的BLOB列,你必須增加該值。它應(yīng)該象你想要使用的最大BLOB的那么大。 max_allowed_packet = 4M #允許的同時客戶的數(shù)量。增加該值增加 mysqld要求的文件描述符的數(shù)量。這個數(shù)字應(yīng)該增加,否則,你將經(jīng)常看到 Too many connections 錯誤。 默認數(shù)值是100 max_connections = 1024 #指定表高速緩存的大小。每當(dāng)MySQL訪問一個表時,如果在表緩沖區(qū)中還有空間,該表就被打開并放入其中,這樣可以更快地訪問表內(nèi)容。通過檢查峰值時間的狀態(tài)值Open_tables和Opened_tables,可以決定是否需要增加table_cache的值。如果你發(fā)現(xiàn)open_tables等于 table_cache,并且opened_tables在不斷增長,那么你就需要增加table_cache的值了(上述狀態(tài)值可以使用show status like 'Open_tables'獲得)。注意,不能盲目地把table_cache設(shè)置成很大的值。如果設(shè)置得太高,可能會造成文件描述符不足,從而造成性能不穩(wěn)定或者連接失敗。 table_cache = 512 #每個線程排序所需的緩沖 sort_buffer_size = 4M #當(dāng)一個查詢不斷地掃描某一個表,MySQL會為它分配一段內(nèi)存緩沖區(qū)。read_buffer_size變量控制這一緩沖區(qū)的大小。如果你認為連續(xù)掃描進行得太慢,可以通過增加該變量值以及內(nèi)存緩沖區(qū)大小提高其性能。 read_buffer_size = 4M #加速排序操作后的讀數(shù)據(jù),提高讀分類行的速度。如果正對遠遠大于可用內(nèi)存的表執(zhí)行GROUP BY或ORDER BY操作,應(yīng)增加read_rnd_buffer_size的值以加速排序操作后面的行讀取。仍然不明白這個選項的用處…… read_rnd_buffer_size = 8M #用于REPAIR TABLE。不明白這個選項的用處,百度上找到的設(shè)置方向也是五花八門,有128M、64M、32M等,折中選一個。 myisam_sort_buffer_size = 64M #可以復(fù)用的保存在中的線程的數(shù)量。如果有,新的線程從緩存中取得,當(dāng)斷開連接的時候如果有空間,客戶的線置在緩存中。如果有很多新的線程,為了提高性能可以這個變量值。通過比較 Connections 和 Threads_created 狀態(tài)的變量,可以看到這個變量的作用。 thread_cache_size = 128 #查詢結(jié)果緩存。第一次執(zhí)行某條SELECT語句的時候,服務(wù)器記住該查詢的文本內(nèi)容和它返回的結(jié)果。服務(wù)器下一次碰到這個語句的時候,它不會再次執(zhí)行該語句。作為代替,它直接從查詢緩存中的得到結(jié)果并把結(jié)果返回給客戶端。 query_cache_size = 32M #最大并發(fā)線程數(shù),cpu數(shù)量*2 thread_concurrency = 2 #設(shè)置超時時間,能避免長連接 wait_timeout = 120 #關(guān)閉不需要的表類型,如果你需要,就不要加上這個 skip-innodb skip-bdb 關(guān)于mysql的優(yōu)化設(shè)置及檢查,這篇文章很值得一看 http://tech.itdb.cn/n/200607/27/n20060727_30398.shtml Mysql的安全設(shè)置 打開/etc/my.cnf文件,修改以下設(shè)置,如果沒有,可手動添加。 #取消文件系統(tǒng)的外部鎖 skip-locking #不進行域名反解析,注意由此帶來的權(quán)限/授權(quán)問題 skip-name-resolve #禁止MySQL中用“LOAD DATA LOCAL INFILE”命令。這個命令會利用MySQL把本地文件讀到數(shù)據(jù)庫中,然后用戶就可以非法獲取敏感信息了。網(wǎng)絡(luò)上流傳的一些攻擊方法中就有用它的,它也是很多新發(fā)現(xiàn)的SQL Injection攻擊利用的手段! local-infile = 0 #關(guān)閉遠程連接,即3306端口。這是MySQL的默認監(jiān)聽端口。由于此處MySQL只服務(wù)于本地腳本,所以不需要遠程連接。盡管MySQL內(nèi)建的安全機制很嚴格,但監(jiān)聽一個TCP端口仍然是危險的行為,因為如果MySQL程序本身有問題,那么未授權(quán)的訪問完全可以繞過MySQL的內(nèi)建安全機制。(你必須確定,你是否真的不需要遠程連接mysql) skip-networking 修改完my.cnf后,還需要對mysql的用戶名、帳號、及默認數(shù)據(jù)庫進行調(diào)整 首先先登錄mysql,在終端窗口輸入 /usr/local/mysql/bin/mysql -u root -p 然后會提示輸入密碼,輸入正確密碼后,會出現(xiàn)mysql>提示符。 輸入以下命令: mysql>use mysql; mysql>update user set user="centos" where user="root"; (將mysql的root用戶名修改成centos,防止root的密碼被暴力破解) mysql>select Host,User,Password,Select_priv,Grant_priv from user; mysql>delete from user where user=''; (刪除user用戶) mysql>delete from user where password=''; (刪除user用戶) mysql>delete from user where host=''; (刪除user用戶) mysql>drop database test; (刪除默認的test數(shù)據(jù)庫) mysql>flush privileges; (刷新mysql的緩存,讓以上設(shè)置立即生效) mysql>quit; 為了使以上優(yōu)化和安全設(shè)置生效,請重啟Mysql服務(wù)或Linux。 關(guān)于Mysql的安全設(shè)置,這篇文章很值得一看 http://www.unixren.com/linux/bencandy.php?fid=21id=459
22、操作系統(tǒng)安全調(diào)整 1、 CentOS或Red Had Enterprise Linux 4 的用戶要首先要打開SElinux,方法是修改/etc/selinux/config文件中的SELINUX="" 為enforcing 。它可以保證你的系統(tǒng)不會非正常的崩潰。有些人認為應(yīng)該關(guān)閉,我強烈不推薦,當(dāng)然只是將centos用來玩玩,不是用于實際服務(wù)器則無所謂了。 2、啟用iptables 防火墻,對增加系統(tǒng)安全有許多好處。設(shè)置好防火墻的規(guī)則。 3、執(zhí)行setup 關(guān)閉那些不需要的服務(wù) ,記住少開一個服務(wù),就少一個危險。 4、禁止Control-Alt-Delete 鍵盤關(guān)閉命令 在"/etc/inittab" 文件中注釋掉下面這行(使用#): ca::ctrlaltdel:/sbin/shutdown -t3 -r now 改為: #ca::ctrlaltdel:/sbin/shutdown -t3 -r now 為了使這項改動起作用,輸入下面這個命令: # /sbin/init q 5、給"/etc/rc.d/init.d" 下script文件設(shè)置權(quán)限 給執(zhí)行或關(guān)閉啟動時執(zhí)行的程序的script文件設(shè)置權(quán)限。 # chmod -R 700 /etc/rc.d/init.d/* 這表示只有root才允許讀、寫、執(zhí)行該目錄下的script文件。 6、修改"/etc/host.conf"文件 "/etc/host.conf"說明了如何解析地址。編輯"/etc/host.conf"文件(vi /etc/host.conf),加入下面這行: # Lookup names via DNS first then fall back to /etc/hosts. order bind,hosts # We have machines with multiple IP addresses. multi on # Check for IP address spoofing. nospoof on 第一項設(shè)置首先通過DNS解析IP地址,然后通過hosts文件解析。第二項設(shè)置檢測是否"/etc/hosts"文件中的主機是否擁有多個IP地址(比如有多個以太口網(wǎng)卡)。第三項設(shè)置說明要注意對本機未經(jīng)許可的電子欺騙。 7、使"/etc/services"文件免疫 使"/etc/services"文件免疫,防止未經(jīng)許可的刪除或添加服務(wù): # chattr +i /etc/services 8.阻止你的系統(tǒng)響應(yīng)任何從外部/內(nèi)部來的ping請求。 既然沒有人能ping通你的機器并收到響應(yīng),你可以大大增強你的站點的安全性。你可以加下面的一行命令到/etc/rc.d/rc.local,以使每次啟動后自動運行。 echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 9、對你的系統(tǒng)上所有的用戶設(shè)置資源限制可以防止DoS類型攻擊(denial of service attacks) 如最大進程數(shù),內(nèi)存數(shù)量等。例如,對所有用戶的限制象下面這樣: vi /etc/security/limits.conf 下面的代碼示例中,所有用戶每個會話都限制在 10 MB,并允許同時有四個登錄。第三行禁用了每個人的內(nèi)核轉(zhuǎn)儲。第四行除去了用戶 bin 的所有限制。ftp 允許有 10 個并發(fā)會話(對匿名 ftp 帳號尤其實用);managers 組的成員的進程數(shù)目限制為 40 個。developers 有 64 MB 的 memlock 限制,wwwusers 的成員不能創(chuàng)建大于 50 MB 的文件。 清單 3. 設(shè)置配額和限制 * hard rss 10000 * hard maxlogins 4 * hard core 0 bin - ftp hard maxlogins 10 @managers hard nproc 40 @developers hard memlock 64000 @wwwusers hard fsize 50000 要激活這些限制,您需要在 /etc/pam.d/login 底部添加下面一行: session required /lib/security/pam_limits.so。 10、注釋掉不需要的用戶和用戶組。 vipw root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin news:x:9:13:news:/etc/news: uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin rpm:x:37:37::/var/lib/rpm:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin netdump:x:34:34:Network Crash Dump user:/var/crash:/bin/bash nscd:x:28:28:NSCD Daemon:/:/sbin/nologin sshd:x:74:74:Privilerpc:x:32:32:Portmapper RPC user:/:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin pcap:x:77:77::/var/arpwatch:/sbin/nologin xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin gdm:x:42:42::/var/gdm:/sbin/nologin pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin htt:x:100:101:IIIMF Htt:/usr/lib/im:/sbin/nologin wangjing:x:500:500::/home/wangjing:/bin/bash mysql:x:101:102:MySQL server:/var/lib/mysql:/bin/bash apache:x:48:48:Apache:/var/www:/sbin/nologin ge-separated SSH:/var/empty/sshd:/sbin/nologin 對于不需要的用戶全部加 # 注釋掉。注意,我不建議直接刪除,當(dāng)你某種原因需要某個用戶時,自己重新會很麻煩。 vi /etc/group root:x:0:root bin:x:1:root,bin,daemon daemon:x:2:root,bin,daemon sys:x:3:root,bin,adm adm:x:4:root,adm,daemon tty:x:5: disk:x:6:root lp:x:7:daemon,lp mem:x:8: kmem:x:9: wheel:x:10:root mail:x:12:mail news:x:13:news uucp:x:14:uucp man:x:15: games:x:20: gopher:x:30: dip:x:40: ftp:x:50: lock:x:54: nobody:x:99: users:x:100: dbus:x:81: floppy:x:19: vcsa:x:69: rpm:x:37: haldaemon:x:68: utmp:x:22: netdump:x:34: nscd:x:28: slocate:x:21: sshd:x:74: rpc:x:32: rpcuser:x:29: nfsnobody:x:65534: mailnull:x:47: smmsp:x:51: pcap:x:77: xfs:x:43: ntp:x:38: gdm:x:42: pegasus:x:65: htt:x:101: wangjing:x:500: mysql:x:102: apache:x:48: 對于不需要的用戶組全部加 # 注釋掉。注意,我不建議直接刪除,當(dāng)你某種原因需要某個用戶組時,自己重新會很麻煩。 11、用chattr命令給下面的文件加上不可更改屬性。 # chattr +i /etc/passwd # chattr +i /etc/shadow # chattr +i /etc/group # chattr +i /etc/gshadow 注意執(zhí)行這個操作后,以root身份都不能向系統(tǒng)增加用戶或者修改密碼了。如果我們要增加用戶或者修改密碼的。應(yīng)該先用chattr -i /etc/passwd等命令解除不可寫設(shè)置,再進行操作。 12、修改sshd的端口。 修改/etc/ssh/sshd_config,將里面的 Port 改為 59825,(具體的端口你隨意。當(dāng)然不能和其他程序的端口沖突了)。并注釋掉前面的#號,然后 pkill sshd service sshd start 就行了 注意最好在本地修改這個端口,否則容易出現(xiàn)把自己鎖在外面的情況。修改了本處端口后,還要注意修改防火墻的ssh端口。 13、 內(nèi)核參數(shù)調(diào)整 vi /etc/sysctl.conf net.ipv4.conf.default.accept_source_route=0 net.ipv4.icmp_echo_ignore_broadcasts=1 #net.ipv4.icmp_echo_ignore_all=1 net.ipv4.icmp_ignore_bogus_error_responses=1 net.ipv4.ip_conntrack_max=65535 net.ipv4.tcp_syn_retries=1 net.ipv4.tcp_fin_timeout=5 net.ipv4.tcp_synack_retries=1 net.ipv4.tcp_syncookies=1 net.ipv4.route.gc_timeout=100 net.ipv4.tcp_keepalive_time=500 net.ipv4.tcp_max_syn_backlog=10000 # sysctl -p //查看 14、經(jīng)常檢查系統(tǒng)日志。系統(tǒng)日志主要位于/var/log/目錄下。防患于未然。 通過以上設(shè)置你的系統(tǒng)一般來說就比較安全了。當(dāng)然安全與不安全是道與魔的斗爭。 經(jīng)過這幾個步驟,我們一個比較安全的LAMP服務(wù)器就環(huán)境基本建立成功啦。感覺上也不是很難,是吧?
六、日常常用的管理功能 # cd /usr/local/src # wget ftp://ftp.ncftp.com/ncftp/ncftp-3.2.1-src.tar.gz # tar zxvf ncftp-3.2.1-src.tar.gz # cd ncftp-3.2.1-src # ./configure --prefix=/usr/local/ncftp # make make install