VPS重装记
最近把VPS给重装了,中间是历尽了九九八十一难啊。。
本文权当自己记录一下相关的内容,下次再折腾直接就复制黏贴了。。
写完了这都五四青年节了啊。祝各位青年们节日快乐。
顺路搭顺风车提问,有哪位大牛知道为什么登录wordpress后memcached生效,查询时间很短,而登出wordpress之后就变得非常慢?
自问自答系列
关闭了StatPressCN插件之后速度就正常了。。
也许因为这插件对访客进行统计然后写入数据库造成的。默认设置下是不记录登录用户的访问记录的。。。
前提工作
1.更新内核及rpm包
1 2 3 4 |
#安装yum-fastestmirror插件 yum -y install yum-fastestmirror #更新内核以及rpm包 yum -y update |
2.修改ssh端口
1 |
vim /etc/ssh/sshd_config |
将#port 22 改成 port 端口号。示例:port 233
1 2 |
#重启sshd服务 service sshd restart |
3.清理系统内已包含的apache、mysql、php等包,并设置时区以及关闭selinux
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
rpm -qa|grep httpd rpm -e httpd rpm -qa|grep mysql rpm -e mysql rpm -qa|grep php rpm -e php yum -y remove httpd yum -y remove php yum -y remove mysql-server mysql yum -y remove php-mysql #设置操作系统时区 rm -rf /etc/localtime cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #关闭selinux /usr/sbin/setenforce 0 #查看selinux状态 /usr/sbin/sestatus #加入开机启动 echo "/usr/sbin/setenforce 0" >> /etc/rc.local |
4.安装编译前必备包
1 |
yum -y install gcc gcc-c++ bison patch unzip mlocate flex wget automake autoconf cpp gettext readline-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel libmcrypt libmcrypt-devel zlib zlib-devel libmhash libmhash-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel libidn libidn-devel openldap openldap-devel openldap-clients openldap-servers nss_ldap expat-devel libtool libtool-ltdl-devel |
为避免编译过程中遇到错误,安装openssl和libcrytp
openssl
不安装openssl则有可能在安装apache过程中遇到 configure: error: ...No recognized SSL/TLS toolkit detected
1 |
yum -y install openssl* |
libcrytp
不安装则有可能在安装php过程中遇到configure: error: mcrypt.h not found. Please reinstall libmcrypt.错误
1 2 3 4 5 6 |
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz tar -zxvf libmcrypt-2.5.7.tar.gz cd libmcrypt-2.5.7 ./configure make && make install |
获取Apache Mysql PHP
1 2 3 4 5 6 7 8 9 |
cd mkdir lamp cd lamp wget http://apache.dataguru.cn//httpd/httpd-2.2.27.tar.gz wget http://down1.chinaunix.net/distfiles/mysql-5.1.59.tar.gz wget http://us1.php.net/distributions/php-5.3.27.tar.gz tar -zxvf httpd-2.2.27.tar.gz tar -zxvf mysql-5.1.59.tar.gz tar -zxvf php-5.3.27.tar.gz |
编译安装Mysql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
cd mysql-5.1.59 #増加专门用于管理mysql的用户 groupadd mysql useradd -r -g mysql mysql #生成makefile文件 ./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data \ --enable-thread-safe-client --enable-assembler --enable-local-infile \ --with-charset=utf8 --with-collation=utf8_general_ci \ --with-extra-charsets=all --with-big-tables --with-readline --with-ssl \ --with-embedded-server make && make install cd /usr/local/mysql chown -R mysql . chgrp -R mysql . bin/mysql_install_db --user=mysql ln -s /usr/local/mysql/bin/mysql /usr/bin cd ~/lamp/mysql-5.1.59 cp support-files/my-medium.cnf /etc/my.cnf cp support-files/mysql.server /etc/init.d/mysqld chmod 0755 /etc/init.d/mysqld ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql ln -s /usr/local/mysql/lib/mysql /usr/bin/mysql ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk ln -s /usr/local/mysql/include/mysql /usr/include/mysql #开机启动 chkconfig --add mysqld #启动mysql服务 service mysqld start |
配置mysql
1 2 3 4 5 6 7 8 9 |
#配置root密码(MYSQL数据库的ROOT用户) /usr/local/mysql/bin/mysqladmin -u root password "new-password" mysql -u root -p mysql > use mysql; mysql > delete from user where password=""; mysql > exit; mysql>drop database test; mysql>flush privileges; mysql>quit; |
编译安装Apache
1 2 3 4 5 6 7 |
cd ~/lamp/httpd-2.2.27 ./configure --prefix=/usr/local/apache --enable-so --enable-cache \ --enable-disk-cache --enable-file-cache --enable-mem-cache \ --enable-deflate --enable-headers --enable-mime-magic \ --enable-proxy --enable-rewrite --enable-ssl \ --enable-mods-shared=all --enable-expires make && make install |
编译安装PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
cd ~/lamp/php-5.3.27 ./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --with-apxs2=/usr/local/apache/bin/apxs \ --with-mysql=/usr/local/mysql \ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-pdo-mysql=/usr/local/mysql \ --with-curl \ --with-pear \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir \ --with-freetype-dir \ --with-mcrypt \ --with-mhash \ --with-openssl \ --with-xmlrpc \ --with-bz2 \ --with-gettext \ --disable-debug \ --enable-exif \ --enable-wddx \ --enable-zip \ --enable-bcmath \ --enable-calendar \ --enable-ftp \ --enable-mbstring \ --enable-soap \ --enable-sockets \ --enable-sqlite-utf8 \ --enable-shmop \ --enable-dba \ --enable-magic-quotes make && make install |
这里取消了--with-mime-magic \,PHP 5.3.27编译时遇到warning,可能php 5.3.27不再支持该参数,故删除该参数。
配置apache
1 |
vim /usr/local/apache/conf/httpd.conf |
将
User daemon
Gruop daemon
改成
User www
Gruop www
改完后添加以下内容
1 2 3 4 |
LoadModule php5_module modules/libphp5.so <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch> |
并找到
1 2 3 |
<IfModule dir_module> DirectoryIndex index.html </IfModule> |
修改为:
1 2 3 4 |
<IfModule dir_module> DirectoryIndex index.html index.php </IfModule> |
1 |
vim /usr/local/apache/bin/apachectl |
添加以下代码
1 2 |
# chkconfig:345 85 15 # description:httpd |
将ServerAdmin you@example.com修改为自己的邮箱
最后执行
1 2 3 4 5 6 7 8 9 |
groupadd www useradd -M -g www -s /sbin/nologin www cp /usr/local/apache/bin/apachectl /etc/init.d/httpd #开机启动 chkconfig --add httpd chkconfig --level 345 httpd on #启动服务 service httpd start |
配置php
1 2 3 4 |
cd ~/lamp/php-5.3.27 cp php.ini-production /usr/local/php/etc/php.ini ln -s /usr/local/php/bin/php /usr/bin vim /usr/local/php/etc/php.ini |
short_open_tag = Off编辑为short_open_tag = On
重启apache,如果提示module php5_module is already loaded, skipping,那么编辑httpd.cong,注释掉下面一行
#LoadModule php5_module /usr/local/apache2/modules/libphp5.so
配置apache虚拟主机
1 |
vim /usr/local/apache/conf/httpd.conf |
去掉Include conf/extra/httpd-vhosts.conf前面的#,然后编辑httpd-vhost.conf即可。
安装配置eaccelerator
1 2 3 4 5 6 7 |
wget https://lnamp-web-server.googlecode.com/files/eaccelerator-eaccelerator-42067ac.tar.gz tar -zxvf eaccelerator-eaccelerator-42067ac.tar.gz cd eaccelerator-eaccelerator-42067ac export PHP_PREFIX="/usr/local/php" $PHP_PREFIX/bin/phpize ./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config make && make install |
安装过程中会提示一个路径,类似于:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20060613/
记下备用。
1 |
vim /usr/local/php/etc/php.ini |
添加以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[eaccelerator] extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so" #上面这个路径替换成提示路径 eaccelerator.shm_size="32" eaccelerator.cache_dir="/usr/cache/eaccelerator" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="0" eaccelerator.shm_prune_period="0" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9" |
建立缓存目录
1 2 3 4 |
mkdir -p /usr/cache/eaccelerator chmod 0777 /usr/cache/eaccelerator #重启apache service httpd restart |
测试是否安装成功,用如下代码存储成文件,放在网站目录并使用浏览器访问。
1 |
<?php phpinfo(); ?> |
正常会出现类似于下面的内容:
This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with eAccelerator v0.9.5, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
安装Memcached
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz wget http://www.monkey.org/~provos/libevent-1.2.tar.gz tar zxvf libevent-1.2.tar.gz cd libevent-1.2 ./configure --prefix=/usr make && make install cd ../ tar zxvf memcached-1.2.0.tar.gz cd memcached-1.2.0 ./configure --with-libevent=/usr make && make install cd ../ wget http://pecl.php.net/get/memcache-2.2.7.tgz tar vxzf memcache-2.2.7.tgz cd memcache-2.2.7 /usr/local/php/bin/phpize ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir make && make install |
安装后会有类似如下路径提示
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/
1 |
vim /usr/local/php/etc/php.ini |
插入如下内容
1 2 3 |
[memcache] extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so" #路径替换成自己的 |
启动memcached
1 |
/usr/local/bin/memcached -d -m 64 -u root -l 127.0.0.1 -p 11211 |
自启动
1 |
vim /etc/rc.d/rc.local |
添加如下内容
1 |
/usr/local/bin/memcached -d -m 64 -u root -l 127.0.0.1 -p 11211 |
安装mod_pagespeed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
32位请执行:wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.rpm 64位请执行:wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm rpm2cpio ./mod-pagespeed-stable_current_i386.rpm | cpio -idmv ./etc/cron.daily/mod-pagespeed ./etc/httpd/conf.d/pagespeed.conf ./etc/httpd/conf.d/pagespeed_libraries.conf ./usr/bin/pagespeed_js_minify ./usr/lib/httpd/modules/mod_pagespeed.so ./usr/lib/httpd/modules/mod_pagespeed_ap24.so ./var/cache/mod_pagespeed ./var/log/pagespeed 27020 blocks cp ./usr/lib/httpd/modules/mod_pagespeed.so /usr/local/apache/modules/ #apache版本>=2.4.2,用pagespeed_ap24.so chmod 0755 /usr/local/apache/modules/mod_pagespeed.so vim /usr/local/apache/conf/pagespeed.conf #新建配置文件,请根据自己需求修改 |
配置文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
LoadModule pagespeed_module /usr/local/apache/modules/mod_pagespeed.so <IfModule pagespeed_module> ModPagespeed on ModPagespeedInheritVHostConfig on AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html ModPagespeedFileCachePath "/var/cache/mod_pagespeed/" ModPagespeedFileCacheInodeLimit 500000 ModPagespeedAvoidRenamingIntrospectiveJavascript on ModPagespeedEnableFilters collapse_whitespace <Location /mod_pagespeed_beacon> SetHandler mod_pagespeed_beacon </Location> <Location /mod_pagespeed_statistics> Order allow,deny Allow from localhost Allow from 127.0.0.1 SetHandler mod_pagespeed_statistics </Location> ModPagespeedMessageBufferSize 100000 <Location /mod_pagespeed_message> Allow from localhost Allow from 127.0.0.1 SetHandler mod_pagespeed_message </Location> </IfModule> |
设置缓存目录
1 2 3 4 5 6 |
mkdir -p /var/mod_pagespeed/ chown -R www.www /var/mod_pagespeed/ echo 'Include conf/pagespeed.conf' >> /usr/local/apache/conf/httpd.conf /usr/local/apache/bin/apachectl -t Syntax OK service httpd restart |
配置ftp
1 2 |
yum install vsftpd vim /etc/vsftpd/vsftpd.conf |
将底下三行
- #chroot_list_enable=YES
- # (default follows)
- #chroot_list_file=/etc/vsftpd.chroot_list
修改为
- chroot_list_enable=YES
- # (default follows)
- chroot_list_file=/etc/vsftpd/chroot_list
1 2 |
useradd -d /home/wwwroot/ftpuser -g ftp -s /sbin/nologin ftpuser passwd ftpuser |
编辑chroot_list,加入不受限制的用户名
1 |
vim /etc/vsftpd/chroot_list |
启动ftp服务
1 |
service vsftpd start |
开机自动启动ftp服务
1 |
chkconfig --level 35 vsftpd on |
acl权限控制
setfacl -R -m u:ftp:rwx .
对当前目录 . 递归设置acl规则 u:ftp:rwx ,
u:ftp:rwx这条规则是针对指定用户设定的(u);该用户名为ftp; 权限规则是rwx
组权限类推
setfacl -R -m g:ftp:rwx .
设置默认
setfacl -R -d --set u:ftp:rwx .
均可类推推广。
多个Wordpress共用Memcached
给wp-config.php添加
1 2 |
define('WP_CACHE_KEY_SALT', 'ooxx1234'); #ooxx1234是用来区别各个wordpress缓存的数据的,名字随便取,叫godson都行。 |
2014-05-04晚更新如下内容
Apache开启Gzip
先检查以下两个模块是否加载。
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
如果已经加载,则在httpd.conf添加以下代码:
1 2 3 4 5 6 7 8 9 10 |
DeflateCompressionLevel 4 AddOutputFilter DEFLATE html xml php js css SetOutputFilter DEFLATE BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html SetEnvIfNoCase Request_URI \\.(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary #Header append Vary User-Agent env=!dont-vary #对代理的设置 |
Apache启动错误一例
Apache启动时出现如下错误
解决方法:
1 2 3 |
vim httpd.conf #ServerName www.example.com:80 ServerName localhost:80 |
关闭Apache目录浏览
编辑httpd.conf
将
1 |
Options Indexes FollowSymLinks |
修改成
1 |
Options -Indexes FollowSymLinks |
参考
安装及配置eaccelerator-0.9.6.1加速PHP5.3.8
VPS手动编译安装配置LAMP(apache+mysql+php)
linux下通过acl配置灵活目录文件权限(可用于ftp,web服务器的用户权限控制)
感谢老谢的大力支持。
更新记录
2014-05-04 添加开启Gzip压缩的内容
2014-05-08 修正一些错误,感谢PK牛找茬~
2014-06-13 修正一些错误,并添加一些内容
已有 6 条评论
发表评论
电子邮件地址不会被公开。 必填项已标注。
赞啊赞啊赞啊赞啊赞~
@dej.sf 这个一般。。
喜欢自己编译折腾啊
@傅小黑 哈哈,生命在于折腾嘛
已保存学习,大D威武。话说gzip那儿多了一个字。。原谅我这强迫症

@frankiez 哈哈,感谢纠正。最近还是在郁闷为啥子memcache只对登录用户生效这事儿。