LNMP下禁止无用User Agent抓取网站

LNMP环境是VPS评测与排名网最喜欢的WEB环境,也一直用LNMP搭建网站使用。前几天查看网站的日志发现,不少眼生的爬虫使劲的爬网站,搜索了下,这些爬虫根本没有,基本都是垃圾爬虫。

为了禁止垃圾User Agent爬虫抓取网站,达到节省宽带和节省资源的目的,就得想办法禁止垃圾User Agent爬虫来抓取自己的网站了,这里分享下LNMP即NGINX下是如何实现禁止无用User Agent抓取网站的。

在/usr/local/nginx/conf/rewrite目录下新建agent_deny.conf文件【特别提醒这个目录是最新版lnmp1.5版本】,内容如下: 继续阅读

LNMP安装ngx_cache_purge缓存清除组件

LNMP WEB环境是VPS评测与排名网一直用的一键包,而最近在折腾使用NGINX的Fastcgi_Cache为WordPress 缓存加速,其中要用到第三方ngx_cache_purge缓存清除组件。

这里,VPS评测与排名网就分享下在LNMP一键包的环境下安装ngx_cache_purge缓存清除组件的教材。

特别说明:VPS是linode ca机房VPS,LNMP为最新的1.5正式版。

LNMP安装ngx_cache_purge缓存清除组件步骤如下:

一、检查是否已安装 ngx_cache_purge

nginx -V 2>&1 | grep -o ngx_cache_purg

如果显示 ngx_cache_purge 则已安装。

二、编译安装 ngx_cache_purge步骤 继续阅读

Nginx不记录JPG图文、CSS及JS等访问日志实现方法

Nginx系现在主流的WEB服务器,VPS评测与排名站使用的是LNMP一键包,用起来也非常顺手。今天分享下LNMP优化之Nginx不记录JPG图文、CSS及JS等访问日志的实现方法。

具体大家看VPS评测与排名网分享自己的设置代码,在nginx配置文件里,具体在vhost文件下的www.vpsrr.com.conf配置文件里。

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
access_log off;
}

location ~ .*\.(js|css)?$
{
expires 7d;
access_log off;
}

location ~ /.well-known {
allow all;
}

location ~ /\.
{
deny all;
}

access_log /home/wwwlogs/www.vpsrr.com.log;

看红色部分,只需要在LNMP环境下的主机NGINX配置文件里面添加access_log off;即可,非常的简单,呵呵~

LNMP安装后nginx+mysql+php全面优化集合

VPS评测与排名网一直推荐使用LNMP一键安装包,自己也一直在使用。之前安装LNMP一键包后,必须做一些优化处理,让LNMP发挥更好的效能。

之前一直参照的张小三博文优化的LNMP集合,对LNMP中的nginx+mysql+php全面优化,但这两天发现张小三博文打不开了,为了方便自己参考,用百度快照功能转过来保持,呵呵~

一:lnmp的nginx优化

主要是修改 /usr/local/nginx/conf/nginx.conf

1.修改nginx的worker_processes的值

军哥的lnmp安装包中nginx的worker_processes默认设置是1,这里我们要根据服务器cpu具体的核心数来优化。通常4核的CPU我会把值设为3。

2核CPU,开启2个进程
worker_processes 2;
worker_cpu_affinity 01 10;

4核CPU,开3个进程
worker_processes 3;
worker_cpu_affinity 0010 0100 1000;

8核CPU,开8个进程
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

2.worker_rlimit_nofile参数默认是5xxxx

worker_rlimit_nofile 65535;

events
	{
		use epoll;
		worker_connections 32700;
	}

添加防压力测试:

if ($http_user_agent ~ ApacheBench|WebBench|Jmeter|must-revalidate|Havij) {retun 503;}

添加针对CVE-2013-4547链接空格的补丁:

if ($request_uri ~ " ") {return 444;}

二:lnmp的mysql优化

用/usr/local/mysql/share/mysql/目录下的my-large.cnf 文件替换根目录etc下的my.cnf文件。

my-huge.cnf: 适合1GB - 2GB RAM主机使用
my-large.cnf: 适合 512MB RAM使用
my-medium.cnf: 只有 32MB - 64MB RAM使用
my-small.cnf:小于64MB 用,MySQL会占用很少资源
my-innodb-heavy-4G.cnf 适合4G以上使用

禁用mysql日志:
修改 /etc/my.cnf 文件
在log-bin=mysql-bin和binlog_format=mixed 
这两行前面加#注释掉即可。

在query_cache_size= 16M下面添加一行:
tmp_table_size = 200M

三:lnmp的php相关参数优化

优化主要是修改/usr/local/php/etc/目录下的php-fpm.conf和php.ini文件。

1.php-fpm.conf参数优化

删除value name="display_errors" 这一行的代码,防止坏人从PHP错误中找到漏洞。

max_children默认参数是开启5个进程。数值要根据内存大小来定,每一个php-cgi所耗费的内存在20M左右。

126M内存默认即可
256M 10个
512M 20个
1G 40个

request_terminate_timeout参数默认是0s,修改为300s。

rlimit_files参数默认5xxxx,修改为65535。

2.php.ini参数优化

disable_functions = 默认禁用了一些参数,PHP中有一些函数的风险性还是相当大的,如果允许这些函数执行,当PHP 程序出现漏洞时,损失是非常严重的。

fsockopen这个参数用的比较多,可以删除。

另外从安全方面考虑可隐藏PHP版本号。
将文件里面的 expose_php = On 修改为 expose_php = Off 即可。

将display_errors =On改为Off。

最后修改最大连接数使重启后也可生效,在/etc/profile 最后增加一行 ulimit -SHn 65535。

另外军哥的LNMP安装包里有一个eAccelerator的安装文件。最好装一下。这个是加速PHP缓存的还不错。

关于eAccelerator的设置给出两个修改的地方吧:

eaccelerator.shm_size="16"

默认是占用16M共享内存,军哥好像是1,改成16吧,具体大小也可根据你的内存情况设置。

另外军哥默认是eaccelerator缓存目录是/usr/local/eaccelerator_cache,这样用硬盘缓存的话,某些情况会影响php的响应时间,可以直接放到共享内存里面。

运行命令:mkdir -p /dev/shm/eaccelerator_cache

修改目录为以下就OK了。

eaccelerator.cache_dir="/dev/shm/eaccelerator_cache"

最后全部修改完记得重启生效:/root/lnmp restart。

VPS评测与排名网特别感谢张小三博客的精彩分享,希望贵博能早点恢复并继续分享。

LNMP常见问题解决办法大全

VPS评测与排名网一直使用的是军哥的LNMP一键web环境,一直用起非常不错。不过,LNMP也会出现一些问题,这里看到CSDN上有一篇总结的非常不错的LNMP常见问题解答办法大全,特此收藏,希望帮到大家,也帮助自己,呵呵~

1、ERROR: invalid PHP executable specified by TEST_PHP_EXECUTABLE = /usr/local/src/php-5.3.18/sapi/cli/php

出现情况:make php过程中的错误

错误意思: 关闭安全模式就好了,都是safe模式的测试 惹的祸。最新版的php 安装包中已经没有safe模块了。

解决办法: 该错误可以忽略直接make install 好了。

2、nginx: [emerg] getpwnam("www") failed

出现情况:刚装完nginx后,运行测试文件的时候。

错误意思:获得 www 用户失败。

解决办法:在nginx.conf中 把"#user nobody;"的前面#注释去掉,改为"user www www;"

3、nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

出现情况:刚安装完nginx后重启时报的错。

错误意思:在 /usr/local/nginx/logs 目录下没有nginx.pid

解决办法: 运行这句话,问题解决 "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" 这句话的意思的为nginx指定配置文件路径。

/usr/local/nginx/sbin/nginx -t 测试nginx的配置文件是否正确

4、nginx: [warn] conflicting server name "" on 0.0.0.0:80, ignored

出现情况: 配置完nginx的 配置文件后,reload时出现的这个错误。

错误意思:nginx的 配置文件中有域名冲突了

解决办法:查看配置文件是否有冲突的域名,如果有的话删除再reload就可以了。

linux上用LINUX下用grep "VPSRR.COM"搜索一下,一般是由于修改的是没有注意造成的。

搜索到有相同的重复,去掉重复的域名配置就可以了。

5、nginx: [alert] kill(34719, 1) failed (3: No such process)

出现情况:重启linux系统后,运行 "/usr/local/nginx/sbin/nginx -s reload" 报这个错误

错误意思:nginx启动的时候杀死进程34719失败,原因是没有这个进程。

解决办法:

[root @mylinux ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root @mylinux ~]# /usr/local/nginx/sbin/nginx -s reload

指定一下nginx 的配置文件 然后重启就好了。

6、[root@mylinux logs]# /usr/local/php/sbin/php-fpm -t

[23-Jun-2013 21:00:29] ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2)
[23-Jun-2013 21:00:29] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'
[23-Jun-2013 21:00:29] ERROR: FPM initialization failed

出现情况:运行 " /usr/local/php/sbin/php-fpm -t" 测试 php-fpm 的配置文件时出现。

错误意思:找不到 php-fpm.conf 文件(php-fpm配置文件)

解决办法:

[root@mylinux init.d]# cd /usr/local/php/etc
[root@mylinux etc]# cp php-fpm.conf.default php-fpm.conf
[root@mylinux etc]# /usr/local/php/sbin/php-fpm -t
[23-Jun-2013 21:04:00] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful

7、[root@mylinux init.d]# /etc/init.d/php-fpm start 或者 service php-fpm start

Starting php-fpm [23-Jun-2013 21:16:01] ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2)
[23-Jun-2013 21:16:01] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'
[23-Jun-2013 21:16:01] ERROR: FPM initialization failed
failed

出现情况:启动php-fpm的时候遇到,该问题和问题6、实际是同一个问题。

错误意思: 找不到 php-fpm.conf 文件(php-fpm配置文件)

解决办法:

[root@mylinux init.d]# cd /usr/local/php/etc
[root@mylinux etc]# cp php-fpm.conf.default php-fpm.conf
[root@mylinux etc]# service php-fpm start
Starting php-fpm done

8、Starting MySQL.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/mylinux.pid).

出现情况:运行" service mysqld start"或者" /etc/init.d/mysqld start"启动mysqld守护进程的时

错误意思:重启mysql失败,原因是找不到

解决办法:从报错上看,看不出具体问题,只能看日志了

[root@mylinux /]# cd /usr/local/mysql/data
[root@mylinux data]# tail -f -n 20 mylinux.err

130623 16:34:53 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
130623 16:34:53 [Note] Plugin 'FEDERATED' is disabled.
130623 16:34:53 InnoDB: The InnoDB memory heap is disabled
130623 16:34:53 InnoDB: Mutexes and rw_locks use GCC atomic builtins
130623 16:34:53 InnoDB: Compressed tables use zlib 1.2.3
130623 16:34:53 InnoDB: Initializing buffer pool, size = 128.0M
130623 16:34:53 InnoDB: Completed initialization of buffer pool
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
130623 16:36:33 InnoDB: Unable to open the first data file
InnoDB: Error in opening ./ibdata1
130623 16:36:33 InnoDB: Operating system error number 11 in a file operation.
InnoDB: Error number 11 means 'Resource temporarily unavailable'.
InnoDB: Some operating system error numbers are described at
InnoDB: http://dev.mysql.com/doc/refman/5.5/en/operating-system-error-codes.html
130623 16:36:33 InnoDB: Could not open or create data files.
130623 16:36:33 InnoDB: If you tried to add new data files, and it failed here,
130623 16:36:33 InnoDB: you should now edit innodb_data_file_path in my.cnf back
130623 16:36:33 InnoDB: to what it was, and remove the new ibdata files InnoDB created
130623 16:36:33 InnoDB: in this failed attempt. InnoDB only wrote those files full of
130623 16:36:33 InnoDB: zeros, but did not yet use them in any way. But be careful: do not
130623 16:36:33 InnoDB: remove old data files which contain your precious data!
130623 16:36:33 [ERROR] Plugin 'InnoDB' init function returned error.
130623 16:36:33 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
130623 16:36:33 [ERROR] Unknown/unsupported storage engine: InnoDB
130623 16:36:33 [ERROR] Aborting
130623 16:36:33 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
130623 16:36:33 mysqld_safe mysqld from pid file /usr/local/mysql/data/mylinux.pid ended

出现情况:查看以上日志很明显是一句 InnoDB: Check that you do not already have another mysqld process

说明已经有mysqld进程在运行了,所以启动mysql失败。

解决办法:

[root@mylinux etc]#ps -ef|grep mysqld
[root@mylinux etc]#kill -9 进程号
[root@mylinux etc]#service mysqld start

这是启动一次 mysql 报的具体的错误。

我重启了一下CentOS服务器,再重启一下mysqld,问题自己解决了,可能是由于LVM磁盘管理软件的原因。

出现这个问题可能的原因有多种,具体什么原因最好的办法是先查看下错误日志:

(1)、可能是/usr/local/mysql/data/rekfan.pid文件没有写的权限

解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data” “chmod -R 755 /usr/local/mysql/data” 然后重新启动mysqld!

(2)、可能进程里已经存在mysql进程

解决方法:用命令“ps -ef|grep mysqld”查看是否有mysqld进程,如果有使用“kill -9 进程号”杀死,然后重新启动mysqld!

(3)、可能是第二次在机器上安装mysql,有残余数据影响了服务的启动。

解决方法:去mysql的数据目录/data看看,如果存在mysql-bin.index,就赶快把它删除掉吧,它就是罪魁祸首了。本人就是使用第三条方法解决的 !http://blog.rekfan.com/?p=186

(4)、mysql在启动时没有指定配置文件时会使用/etc/my.cnf配置文件,请打开这个文件查看在[mysqld]节下有没有指定数据目录(datadir)。

解决方法:请在[mysqld]下设置这一行:datadir = /usr/local/mysql/data

(5)、skip-federated字段问题

解决方法:检查一下/etc/my.cnf文件中有没有没被注释掉的skip-federated字段,如果有就立即注释掉吧。

(6)、错误日志目录不存在

解决方法:使用“chown” “chmod”命令赋予mysql所有者及权限

(7)、selinux惹的祸,如果是centos系统,默认会开启selinux

解决方法:关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器试试。

(8)、mysql的data目录挂载的磁盘空间已经用完

解决方法:df -h 查看磁盘使用情况,果然磁盘满了,因为当时分区采用系统默认,不知道为什么不能自动扩容然后清理多余数据,以后在研究吧。启动mysql,成功启动!

VPS评测与排名站出现过多个问题,都解决了,呵呵~

LNMP安装时出现EPEL安装源Errno问题解决办法

VPS评测与排名网酷爱LNMP的web环境,也一直使用的是军哥的LNMP一键包。并在此站也分享过LNMP一键包的按照教程,具体大家可以在本站搜索下,呵呵~

不过今天VPS评测与排名网手上的一个ramnode vps用了很多年,LNMP环境有点旧了,就打算换新版的LNMP,不过出问题了!

说明下:VPS系RAMNODE OVZ,系统是CENTOS 6,出现的问题是EPEL安装源Errno错误,具体如下:

http://mirror.symnds.com/distributions/fedora-epel/6/x86_64/repodata/repomd.xml: [Errno -1] repomd.xml does not match metalink for epel
Trying other mirror.

一直是类似的错误出现,搞的VPS评测与排名网很是郁闷,用来这么多年的LNMP,第一次出现这种问题。

出现问题,总要解决问题了,搜索了半天都没相关解答,到国外网站去看英文的,并多次尝试终于解决了EPEL安装源Errno问题。先记录如下:

修改/etc/yum.repos.d/目录下的epel.repo文件,相应[epel]部分用以下替代即可。

[epel]  
name=Extra Packages for Enterprise Linux 6 - $basearch
enabled=1
failovermethod=priority
gpgcheck=1
gpgkey=https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
sslverify=true

VPS评测与排名网替换后,再行安装LNMP一键包,不再出现EPEL安装源Errno错误,安装LNMP成功,至此文件解决。

希望碰到相关问题的网友们,不用浪费时间了,直接解决,呵呵~

MySql出现The server quit without updating PID file无法启动解决方法

VPS评测与排名站非常喜欢的web环境是LNMP环境,具体搭建的教材搭建可以参考:VPS搭建网站WEB服务器环境之LNMP篇。不过,今天重启LNMP MYSQL出现了“The server quit without updating PID file”的错误,从而导致MYSQL无法启动。

经过搜索和尝试,终于解决了LNMP环境下MYSQL出现的“The server quit without updating PID file”错误,这里记录下,方便下次遇到解决这个问题。

1.可能是/usr/local/mysql/data/6258.pid文件没有写的权限

解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data” “chmod -R 755 /usr/local/mysql/data” 然后重新启动mysqld!

2.可能进程里已经存在mysql进程

解决方法:用命令“ps -ef|grep mysqld”查看是否有mysqld进程,如果有使用“kill -9 进程号”杀死,然后重新启动mysqld!

3.可能是第二次在机器上安装mysql,有残余数据影响了服务的启动。

解决方法:去mysql的数据目录/data看看,如果存在mysql-bin.index,就赶快把它删除掉吧,它就是罪魁祸首了。

4.mysql在启动时没有指定配置文件时会使用/etc/my.cnf配置文件,请打开这个文件查看在[mysqld]节下有没有指定数据目录(datadir)。

解决方法:请在[mysqld]下设置这一行:datadir = /usr/local/mysql/data

5.skip-federated字段问题

解决方法:检查一下/etc/my.cnf文件中有没有没被注释掉的skip-federated字段,如果有就立即注释掉吧。

6.错误日志目录不存在

解决方法:使用“chown” “chmod”命令赋予mysql所有者及权限

7.selinux惹的祸,如果是centos系统,默认会开启selinux

解决方法:关闭它,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器试试。

8.硬盘空间占满

解决办法:删除一些不需要的文件,保证硬盘空间足够。可以删除垃圾文件、记录文件等。

以上就是全部的方法,VPS评测与排名站就是用上面的方法解决的。

LNMP 1.2版本安装eAccelerator及简单优化

LNMP已经更新到1.2版本了,VPS评测与排名站也抽空体验了下,相比之下还是有很多的进步。这里记录下LNMP 1.2版本安装eAccelerator加速方法,并给出了简单优化。

LNMP 1.2版本安装eAccelerator命令:

一、进入LNMP文件目录

cd /lnmp1.2-full

二、执行安装脚本

./addons.sh install eaccelerator

这里按提示就行了,我装的是默认1.0的版本。

不一会儿eAccelerator就安装好了,下面做下简单的优化。

执行以下命令:

mkdir -p /dev/shm/eaccelerator_cache

然后将PHP.ini对于部分修改如下:

[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="48"
eaccelerator.cache_dir="/dev/shm/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="0"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="7200"
eaccelerator.shm_prune_period="7200"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys = "shm_and_disk"
eaccelerator.sessions = "shm_and_disk"
eaccelerator.content = "shm_and_disk"

至此已优化完成,不过大家可以自行调节参数。