vsftpd 配置文件详解

vsftpd配置文件采用“#”作为注释符,以“#”开头的行和空白行在解析时将被忽略,其余的行被视为配置命令行,每个配置命令的“=”两边不要留有空格。对于每个配置命令,在配置文件中还列出了相关的配置说明,利用vi编辑器可实现对配置文件的编辑修改。方法如下:
#vi /etc/vsftpd/vsftpd.conf

1.登录和对匿名用户的设置

write_enable=YES //是否对登录用户开启写权限。属全局性设置。默认NO
local_enable=YES //是否允许本地用户登录FTP服务器。默认为NO
anonymous_enable=YES //设置是否允许匿名用户登录FTP服务器。默认为YES
ftp_username=ftp //定义匿名用户的账户名称,默认值为ftp。
no_anon_password=YES //匿名用户登录时是否询问口令。设置为YES,则不询问。默
认NO
anon_world_readable_only=YES //匿名用户是否允许下载可阅读的文档,默认为YES。
anon_upload_enable=YES //是否允许匿名用户上传文件。只有在write_enable设置为
YES时,该配置项才有效。而且匿名用户对相应的目录必须有写权限。默认为NO。
anon_mkdir_write_enable=YES //是否允许匿名用户创建目录。只有在write_enable设置为 YES时有效。且匿名用户对上层目录有写入的权限。默认为NO。
anon_other_write_enable=NO //若设置为YES,则匿名用户会被允许拥有多于
上传和建立目录的权限,还会拥有删除和更名权限。默认值为NO。

2.设置欢迎信息

用户登录FTP服务器成功后,服务器可向登录用户输出预设置的欢迎信息。
ftpd_banner=Welcome to my FTP server.
//该配置项用于设置比较简短的欢迎信息。若欢迎信息较多,则可使用banner_file配置项。
banner_file=/etc/vsftpd/banner
//设置用户登录时,将要显示输出的文件。该设置项将覆盖ftpd_banner的设置。
dirmessage_enable=YES
//设置是否显示目录消息。若设置为YES,则当用户进入特定目录(比如/var/ftp/linux)时,将显示该目录中的由message_file配置项指定的文件(.message)中的内容。
message_file=.message //设置目录消息文件。可将显示信息存入该文件。该文件需要放在 相应的目录(比如/var/ftp/linux)下

3.设置用户登录后所在的目录
local_root=/var/ftp
// 设置本地用户登录后所在的目录。默认配置文件中没有设置该项,此时用户登录FTP服务器后,所在的目录为该用户的主目录,对于root用户,则为/root目录。
anon_root=/var/ftp
//设置匿名用户登录后所在的目录。若未指定,则默认为/var/ftp目录。

4.控制用户是否允许切换到上级目录
在默认配置下,用户可以使用“cd..”命名切换到上级目录。比如,若用户登录后所在的目录为/var/ftp,则在“ftp>”命令行 下,执行“cd..”命令后,用户将切换到其上级目录/var,若继续执行该命令,则可进入Linux系统的根目录,从而可以对整个Linux的文件系统 进行操作。

若设置了write_enable=YES,则用户还可对根目录下的文件进行改写操作,会给系统带来极大的安全隐患,因此,必须防止用户切换到Linux的根目录,相关的配置项如下:
chroot_list_enable=YES
// 设置是否启用chroot_list_file配置项指定的用户列表文件。设置为YES则除了列在j/etc/vsftpd/chroot_list文件中的的帐号外,所有登录的用户都可以进入ftp根目录之外的目录。默认NO
chroot_list_file=/etc/vsftpd/chroot_list
// 用于指定用户列表文件,该文件用于控制哪些用户可以切换到FTP站点根目录的上级目录。
chroot_local_user=YES
// 用于指定用户列表文件中的用户,是否允许切换到上级目录。默认NO
注意:要对本地用户查看效果,需先设置local_root=/var/ftp

具体情况有以下几种:
1)当chroot_list_enable=YES,chroot_local_user=YES时,在/etc/vsftpd/chroot_list文件中列出的用户,可以切换到上级目录;未在文件中列出的用户,不能切换到站点根目录的上级目录。
2)当chroot_list_enable=YES,chroot_local_user=NO时,在/etc/vsftpd/chroot_list文件中列出的用户,不能切换到站点根目录的上级目录;未在文件中列出的用户,可以切换到上级目录。
3)当chroot_list_enable=NO,chroot_local_user=YES时,所有用户均不能切换到上级目录。
4)当chroot_list_enable=NO,chroot_local_user=NO时,所有用户均可以切换到上级目录。
5)当用户不允许切换到上级目录时,登录后FTP站点的根目录“/”是该FTP账户的主目录,即文件的系统的/var/ftp目录。

5.设置访问控制
(1)设置允许或不允许访问的主机(见TBP14)
tcp_wrappers=YES用来设置vsftpd服务器是否与tcp wrapper相结合,进行主机的访问控制。默认设置为YES,vsftpd服务器会检查/etc/hosts.allow和/etc /hosts.deny中的设置,以决定请求连接的主机是否允许访问该FTP服务器。这两个文件可以起到简易的防火墙功能。
比如,若要仅允许192.168.168.1~192.168.168.254的用户,可以访问连接vsftpd服务器,则可在/etc/hosts.allow文件中添加以下内容:
vsftpd:192.168.168.0/255.255.255.0 :allow
all:all:deny

(2)设置允许或不允许访问的用户
对用户的访问控制由/etc/vsftpd/user_list和/etc/vsftpd/ftpusers文件来控制实现。相关配置命令如下:
userlist_enable=YES
// 决定/etc/vsftpd/user_list文件是否启用生效。YES则生效,NO不生效。
userlist_deny=YES
// 决定/etc/vsftpd/user_list文件中的用户是允许访问还是不允许访问。若设置为YES,则/etc/vsftpd/user_list 文件中的用户将不允许访问FTP服务器;若设置为NO,则只有vsftpd.user_list文件中的用户,才能访问FTP服务器。

6.设置访问速度
anon_max_rate=0
//设置匿名用户所能使用的最大传输速度,单位为b/s。若设置为0,则不受速度限制,此为默认值。
local_max_rate=0
// 设置本地用户所能使用的最大传输速度。默认为0,不受限制。

7.定义用户配置文件
在vsftpd服务器中,不同用户还可使用不同的配置,这要通过用户配置文件来实现。
user_config_dir=/etc/vsftpd/userconf //用于设置用户配置文件所在的目录。
设置了该配置项后,当用户登录FTP服务器时,系统就会到/etc/vsftpd/userconf目录下读取与当前用户名相同的文件,并根据文件中的配 置命令,对当前用户进行更进一步的配置。比如,利用用户配置文件,可实现对不同用户进行访问的速度进行控制,在各用户配置文件中,定义 local_max_rate配置,以决定该用户允许的访问速度。

8.与连接相关的设置
listen=YES
//设置vsftpd服务器是否以standalone模式运行。以standalone模式运行是一种较好的方式,此时listen必须设置为YES, 此为默认值,建议不要更改。很多与服务器运行相关的配置命令,需要此运行模式才有效。若设置为NO,则vsftpd不是以独立的服务运行,要受 xinetd服务的管理控制,功能上会受限制。

max_clients=0
//设置vsftpd允许的最大连接数,默认为0,表示不受限制。若设置为150时,则同时允许有150个连接,超出的将拒绝建立连接。只有在以standalone模式运行时才有效。

max_per_ip=0
// 设置每个IP地址允许与FTP服务器同时建立连接的数目。默认为0,不受限制。通常可对此配置进行设置,防止同一个用户建立太多的连接。只有在以standalone模式运行时才有效。

listen_address=IP地址
//设置在指定的IP地址上侦听用户的FTP请求。若不设置,则对服务器所绑定的所有IP地址进行侦听。只有在以standalone模式运行时才有效。 对于只绑定了一个IP地址的服务器,不需要配置该项,默认情况下,配置文件中没有该配置项。若服务器同时绑定了多个IP地址,则应通过该配置项,指定在哪 个IP地址上提供FTP服务,即指定FTP服务器所使用的IP地址。
注意:设置此值前后,可以通过netstat -tnl对比端口的监听情况

accept_timeout=60
//设置建立被动(PASV)数据连接的超时时间,单位为秒,默认值为60。
connect_timeout=60
// PORT方式下建立数据连接的超时时间,单位为秒。
data_connection_timeout=300
//设置建立FTP数据连接的超时时间,默认为300秒。

idle_session_timeout=600
//设置多长时间不对FTP服务器进行任何操作,则断开该FTP连接,单位为秒,默认为600秒。即设置发呆的逾时时间,在这个时间内,若没有数据传送或指令的输入,则会强行断开连接。
pam_service_name=vsftpd
//设置在PAM所使用的名称,默认值为vsftpd。

setproctitle_enable=NO|YES
//设置每个与FTP服务器的连接,是否以不同的进程表现出来,默认值为NO,此时只有一个名为vsftpd的进程。若设置为YES,则每个连接都会有一个vsftpd进程,使用“ps -ef|grep ftp”命令可查看到详细的FTP连接信息。安全起见,建议关闭。

9.FTP工作方式与端口设置
(1)FTP工作方式简介
FTP的工作方式有两种,一种是PORT FTP,另一种是PASV FTP。下面介绍其工作方式。
二者的区别在于PORT FTP的数据传输端口是由FTP服务器指定的,而PASV FTP则是由FTP客户端指定的,而且每次数据连接所使用的端口号都不同。正因为如此,所以在CuteFTP等FTP客户端软件中,其连接类型设置项中有PORT和PASV两种选择。

当FTP服务器设置为PASV工作模式时,客户端也必须设置为PASV连接类型。若客户端连接类型设置为PORT,则能建立FTP连接,但在执行ls或get等需要数据请求的命令时,将会出现无响应并最终报告无法建立数据连接。

(2)与端口相关的配置
listen_port=21
// 设置FTP服务器建立连接所侦听的端口,默认值为21。
连接非标准端口示例:ftp www.sunflower.org 7000
connect_from_port_20=YES
// 默认值为YES,指定FTP数据传输连接使用20端口。若设置为NO,则进行数据连接时,所使用的端口由ftp_data_port指定。

ftp_data_port=20
//设置PORT方式下FTP数据连接所使用的端口,默认值为20。
pasv_enable=YES|NO
//若设置为YES,则使用PASV工作模式;若设置为NO,使用PORT模式。默认为YES,即使用PASV模式。
pasv_max_port=0
//设置在PASV工作方式下,数据连接可以使用的端口范围的上界。默认值为0,表示任意端口。
pasv_mim_port=0
//设置在PASV工作方式下,数据连接可以使用的端口范围的下界。默认值为0,表示任意端口。

10.设置传输模式
FTP在传输数据时,可使用二进制(Binary)方式,也可使用ASCII模式来上传或下载数据。
ascii_download_enable=YES //设置是否启用ASCII模式下载数据。默认为NO。
ascii_upload_enable=YES //设置是否启用ASCII模式上传数据。默认为NO。

11.设置上传文档的所属关系和权限
(1)设置匿名上传文档的属主
chown_uploads=YES
//用于设置是否改变匿名用户上传的文档的属主。默认为NO。若设置为YES,则匿名用户上传的文档的属主将被设置为chown_username配置项所设置的用户名。
chown_username=whoever
//设置匿名用户上传的文档的属主名。只有chown_uploads=YES时才有效。建议不要设置为root用户。 但系统默root

(2)新增文档的权限设定
local_umask=022
//设置本地用户新增文档的umask,默认为022,对应的权限为755。umask为022,对应的二进制数为000 010 010,将其取反为111 101 101,转换成十进制数,即为权限值755,代表文档的所有者(属主)有读写执行权,所属组有读和执行权,其他用户有读和执行权。022适合于大多数情 况,一般不需要更改。若设置为077,则对应的权限为700。
anon_umask=022 //设置匿名用户新增文档的umask。默认077
file_open_mode=0755 //设置上传文档的权限。权限采用数字格式。 默认0666

12.日志文件
xferlog_enable=YES //是否启用上传/下载日志记录。默认为NO
xferlog_file=var/log/vsftpd.log //设置日志文件名及路径。需启用xferlog_enable选项
xferlog_std_format=YES //日志文件是否使用标准的xferlog日志文件格式(与wu-ftpd使用的格式相同) 。默认为NO

13.其他设置
text_userdb_names=NO
//设置在执行ls命令时,是显示UID、GID还是显示出具体的用户名或组名称。默认为NO,以UID和GID方式显示,若希望显示用户名和组名称,则设置为YES。
ls_recurse_enable=YES
//若设置为YES,则允许执行“ls –R”这个命令,默认值为NO。在配置文件中该配置项被注释掉了,与此类似的还有一些配置,需要启用时,将注释符去掉并进行YES或NO的设置即可

linux ftp 服务器配置

# 安装前准备准备
1. 关闭防火墙
service iptables stop 或者 systemctl stop  iptables.service (centos7.0)
service firewalld stop 或者  systemctl stop  firewalld.service
2.  sestatus -v  查看状态是不是disabled,如果不是,
–  修改/etc/selinux/config 文件将SELINUX=enforcing改为SELINUX=disabled (修改后,重启生效)
–    setenforce 0   临时关闭

# 安装
1.yum install  vsftpd -y
#  配置
1.  /ets/vsftpd 文件内容
vsftpd.conf: 主配置文件
ftpusers: 指定哪些用户不能访问FTP服务器
user_list: 指定的用户是否可以访问ftp服务器由vsftpd.conf文件中的 userlist_deny的取值来决定。
2. 查看配置文件配置 cat /etc/vsftpd/vsftpd.conf |grep -v ‘^#’;
3. 配置文件vsftpd.conf 里面以下
anon_upload_enable=YES
anon_mkdir_write_enable=YES
开启匿名匿名用户的配置
4. 我们可以在vsftpd.conf 文件设置如下参数:

限制IP 访问FTP:
#vi /etc/hosts.allow

vsftpd:192.168.5.128:DENY 设置该IP地址不可以访问ftp服务

FTP 访问时间限制:

#cp /usr/share/doc/vsftpd-1.1.3/vsftpd.xinetd /etc/xinetd.d/vsftpd

#vi /etc/xinetd.d/vsftpd/

修改 disable = no

access_time = hour:min-hour:min (添加配置访问的时间限制(注:与vsftpd.conf中listen=NO相对应)

例: access_time = 8:30-11:30 17:30-21:30 表示只有这两个时间段可以访问ftp
5. 要注意的是userlist_enable=YES(是否读取userlist), userlist_deny=NO(userlist允许ftp连接,默认YES),只有在userlist用户才允许ftp链接,其他用户不允许链接,
系统会读取userlist文件列表,但还会读取ftpusers(那些用户不允许ftp登陆)

# 新建用户
1. useradd -d /home/test test
2. passwd test 设置密码
3. usermod -s /sbin/nologin test 不让登陆shell

CentOS的ssh sftp配置及权限设置

1、从安全方面看,sftp会更安全一点
2、线上服务器提供在线服务,对用户需要控制,只能让用户在自己的home目录下活动
3、用户只能使用sftp,不能ssh到机器进行操作
提供sftp服务,可以用系统自带的internal-sftp,也可以使用vsftpd,这里需求不多,直接选用internal-sftp。
限制用户只能在自己的home目录下活动,这里需要使用到chroot,openssh 4.8p1以后都支持chroot,我现在用的是CentOS 6.3,自带的openssh已经是5.3p1,足够了。
可以输入:
# ssh -V  
来查看openssh的版本,如果低于4.8p1,需要自行升级安装,不在这里具体介绍了。
假设,有一个名为sftp的组,这个组中的用户只能使用sftp,不能使用ssh,且sftp登录后只能在自己的home目录下活动
1、创建sftp组
# groupadd sftp  
2、创建一个sftp用户,名为mysftp
# useradd -g sftp -s /bin/false mysftp
# passwd mysftp
3、sftp组的用户的home目录统一指定到/data/sftp下,按用户名区分,这里先新建一个mysftp目录,然后指定mysftp的home为/data/sftp/mysftp
# mkdir -p /data/sftp/mysftp
# usermod -d /data/sftp/mysftp mysftp
4、配置sshd_config
编辑 /etc/ssh/sshd_config
# vim +132 /etc/ssh/sshd_config  
找到如下这行,并注释掉
Subsystem      sftp    /usr/libexec/openssh/sftp-server  
添加如下几行
Subsystem       sftp    internal-sftp  
Match Group sftp  
ChrootDirectory /data/sftp/%u  
ForceCommand    internal-sftp  
AllowTcpForwarding no  
X11Forwarding no  
解释一下添加的几行的意思
Subsystem       sftp    internal-sftp  
这行指定使用sftp服务使用系统自带的internal-sftp
Match Group sftp  
这行用来匹配sftp组的用户,如果要匹配多个组,多个组之间用逗号分割
当然,也可以匹配用户
Match User mysftp
这样就可以匹配用户了,多个用户名之间也是用逗号分割,但我们这里按组匹配更灵活和方便
ChrootDirectory /data/sftp/%u  
用chroot将用户的根目录指定到/data/sftp/%u,%u代表用户名,这样用户就只能在/data/sftp/%u下活动,chroot的含义,可以参考这里:http://www.ibm.com/developerworks/cn/linux/l-cn-chroot/
ForceCommand    internal-sftp  
指定sftp命令
AllowTcpForwarding no  
X11Forwarding no  
这两行,如果不希望该用户能使用端口转发的话就加上,否则删掉
5、设定Chroot目录权限
# chown root:sftp /data/sftp/mysftp
# chmod 755 /data/sftp/mysftp
错误的目录权限设定会导致在log中出现”fatal: bad ownership or modes for chroot directory XXXXXX”的内容
目录的权限设定有两个要点:
1、由ChrootDirectory指定的目录开始一直往上到系统根目录为止的目录拥有者都只能是root
2、由ChrootDirectory指定的目录开始一直往上到系统根目录为止都不可以具有群组写入权限
所以遵循以上两个原则
1)我们将/data/sftp/mysftp的所有者设置为了root,所有组设置为sftp
2)我们将/data/sftp/mysftp的权限设置为755,所有者root有写入权限,而所有组sftp无写入权限
6、建立SFTP用户登入后可写入的目录
照上面设置后,在重启sshd服务后,用户mysftp已经可以登录,但使用chroot指定根目录后,根应该是无法写入的,所以要新建一个目录供mysftp上传文件。这个目录所有者为mysftp,所有组为sftp,所有者有写入权限,而所有组无写入权限
# mkdir /data/sftp/mysftp/upload  
# chown mysftp:sftp /data/sftp/mysftp/upload  
# chmod 755 /data/sftp/mysftp/upload  
7、重启sshd服务
# service sshd restart  
到这里,mysftp已经可以通过sftp客户端登录并可以上传文件到upload目录。
如果还是不能在此目录下上传文件,提示没有权限,检查SElinux是否关闭,可以使用如下指令关闭SElinux
修改/etc/selinux/config文件中的SELINUX=”” 为 disabled ,然后重启。或者
# setenforce 0

一开始,我以为是权限问题导致的无法上传文件,即使给777权限给文件夹也不行. 然后干脆关闭了SElinux,终于可以上传了。

如果你链接服务器的时候出现下面的提示:
Write failed: Broken pipe
Couldn’t read packet: Connection reset by peer
这个问题的原因是ChrootDirectory的权限问题,你设定的目录必须是root用户所有,否则就会出现问题。所以请确保sftp用户根目录的所有人是root, 权限是 750 或者 755。

Fix “502 Bad Gateway” error on NGINX server after upgrading PHP

I check my servers for upgrades on a regular basis and when I noticed yesterday that PHP was upgraded to version 5.5.12, I backed up everything of course (backup guide) and decided to upgrade which can easily be done with sudo apt-get dist-upgrade. Unfortunately I’ve ran into quite an annoying issue which I had not encountered before, all pages returned “502 Bad Gateway” errors.

I’ve done this a numerous time and the upgrade went very smoothly. I was even asked if I wanted to keep local configuration files or not. I choose yes and the installation proceeded. After installation was completed php got restarted and I received a notice that the upgrade was successful. Unfortunately when I went to check my website I got annoying 502 Bad Gateway” errors on every single page.

badgateway

What does this error message mean? A gateway, is like an access point, a bridge that communicate one service with another, in this case the gateway can be a service/application (WordPress, running on PHP) that is working and recieving requests from NGINX web server. So there is a communication issue between PHP and NGINX.

Problem Solving 1: is PHP-FPM running?

As you can see I am also running the latest version of NGINX (1.6.0 as of May 10th 2014) which was updated a few days earlier. It was clearly an issue with PHP so I started to think of what could’ve gone wrong. The first thing that you should always do is check if PHP is even running. Perhaps something went wrong while restarting PHP. Use the following command to check whether PHP-FPM is running.

ps aux | grep php

ps aux will output all processes that are running, so we add | grep php to only output processes with php in the name. If you see PHP processes, then this is not the the issue. Otherwise try to stop/start/restart PHP.

sudo service php5-fpm stop
sudo service php5-fpm start
sudo service php5-fpm restart

If there are still no PHP-FPM processes running, you might try to remove PHP and reinstall it. If PHP-FPM is running correctly, skip this step and go to the following section.

sudo apt-get remove php5 php5-cgi php5-fpm
sudo apt-get install php5 php5-cgi php5-fpm
Problem Solving 2a: is PHP-FPM listening correctly?

A common issue is that the PHP-FPM service is not listening to the host/port which is configured in NGINX. Find the www.conf file on your server in the PHP folder. On Ubuntu this can be found here:

/etc/php5/fpm/pool.d/www.conf

Search for the listen parameter and make note of it:

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000

As you can see this is set to 127.0.0.1:9000, but it is also commonly set to/var/run/php5-fpm.sock. In my config this was set to the latter and I found out that php5-fpm.sock was simply missing (removed during the upgrade), so I changed it to 127.0.0.1:9000.

Now find your NGINX server configuration file, usually located at/etc/nginx/sites-available, and open it. Look for something like this (location ~ \.php$):

location ~ \.php$ {
	try_files $uri =404;
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index index.php;
	include fastcgi_params;       
}

The parameter we are looking for is fastcgi_pass. This has to be the samevalue as in the listen parameter in the www.conf file. Change it accordingly. In my case I changed fastcgi_pass unix:/var/run/php5-fpm.sock; tofastcgi_pass 127.0.0.1:9000;.

Of course you may never forget to restart the NGINX and PHP5-FPM services! You would be surprised how many people forget this step. It is necessary to do this so that the updated configuration files are loaded.

sudo service php5-fpm restart
sudo service nginx restart
Problem Solving 2b: permanently fix the issue

While the above fix works perfectly, it is not ideal since we need to change parameters which were probably changed for a reason. It turns out that issues like this are very common due to file permissions and owner issues. That is way php-fpm.sock was missing. The bug report can be found here. Follow these steps to fix permissions and owner on Ubuntu.

So first of all, make sure that your virtual hosts NGINX files are usingfastcgi_pass unix:/var/run/php5-fpm.sock; at the php configuration like this:

location ~ \.php$ {
	try_files $uri =404;
	fastcgi_pass unix:/var/run/php5-fpm.sock;
	fastcgi_index index.php;
	include fastcgi_params;       
}

Now edit the NGINX configuration file, found at /etc/nginx/nginx.conf and take note of the user (usually located on line 1). This can be www-data, nginx or something else you have set. I will call this “NGINXUSER”.

Now we will change the php-fpm configuration file, located at/etc/php5/fpm/pool.d/www.conf. Simply locate the listen parameter and change/add the appropriate values to match this (change NGINXUSER to the user you have noted down earlier):

listen = /var/run/php5-fpm.sock
listen.owner = NGINXUSER
listen.group = NGINXUSER

Restart PHP5-FPM and NGINX with the following commands and now you should be able to upgrade PHP without any issues.

sudo service php5-fpm restart
sudo service nginx restart
Problem Solving 3: Change NGINX config

Perhaps the buffer and timeout parameters are configured incorrectly for your server or they don’t suffice anymore. You can find the NGINX configuration file at /etc/nginx/nginx.conf. Try increasing the following parameters.
Increase buffer and timeouts inside http block:

http {
	...
	fastcgi_buffers 8 16k;
	fastcgi_buffer_size 32k;
	fastcgi_connect_timeout 300;
	fastcgi_send_timeout 300;
	fastcgi_read_timeout 300;
	...
}
Problem Solving 4: Disable APC

APC caching can cause 502 Bad Gateway issues under particular environments causing segmentation faults. I highly suggest using Memcache(d), but XCACHE is also a good alternative.

Conclusion

Getting unexpected are always very annoying and can really cause a lot of issues (decreased potential income, loss of visitors,…). Especially when you have no clue what could be causing the error. Hopefully this article helped you solve this issue.

centOS 7.0 安装lamp 环境

centOS 7.0 安装lamp 环境
# 准备
1. 7.0 防火墙已有firewall作为防火墙
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
2. yum install iptables-services #安装
systemctl restart iptables.service #最后重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动
3. vi /etc/selinux/config
SELINUX=enforcing #注释掉
SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效

# 安装apache
1. yum install httpd #根据提示,输入Y安装即可成功安装
chkconfig httpd on 开机自启动
service httpd start 启动httpd 服务
2. 设置虚拟主机
3. 注意事项
– 删除welcome.conf 不然设置的虚拟主机没用

`或者`
systemctl start httpd.service #启动apache
systemctl stop httpd.service #停止apache
systemctl restart httpd.service #重启apache
systemctl enable httpd.service #设置apache开机启动
# 安装mysql
1. CentOS 7.0中,已经使用MariaDB替代了MySQL数据库
ystemctl start mariadb.service #启动MariaDB
systemctl stop mariadb.service #停止MariaDB
systemctl restart mariadb.service #重启MariaDB
systemctl enable mariadb.service #设置开机启动
2. 设置mysql的密码
mysqladmin -u root password “123456”
`and`
GRANT ALL PRIVILEGES ON *.* TO root@”%” IDENTIFIED BY “123456”;

# 安装php
1. 事先确认yum源的链接是不是有效的。
– rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
– 确认安装的php版本
yum list –enablerepo=remi –enablerepo=remi-php56 | grep php
– 安装php5.6
yum install –enablerepo=remi –enablerepo=remi-php56 php php-opcache php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common

# apache 配置
1. 编辑 http.conf(配置文件在 /etc/httpd/conf.d)
添加
AddType application/x-httpd-php .php .phtml .php3 .inc
AddType application/x-httpd-php-source .phps
2. 重启apache service http restart 或者
systemctl restart httpd.service

centos 7.0 关闭防火墙

1. Disable Firewalld Service.

[root@rhel-centos7-tejas-barot-linux ~]# systemctl mask firewalld

2. Stop Firewalld Service.

[root@rhel-centos7-tejas-barot-linux ~]# systemctl stop firewalld

3. Install iptables service related packages.

[root@rhel-centos7-tejas-barot-linux ~]# yum -y install iptables-services

4. Make sure service starts at boot:

[root@rhel-centos7-tejas-barot-linux ~]# systemctl enable iptables

# If you do not want ip6tables, You can skip following command.

[root@rhel-centos7-tejas-barot-linux ~]# systemctl enable ip6tables

5. Now, Finally Let’s start the iptables services.

[root@rhel-centos7-tejas-barot-linux ~]# systemctl start iptables

# If you do not want ip6tables, You can skip following command.

[root@rhel-centos7-tejas-barot-linux ~]# systemctl start ip6tables

Firewalld Service is now disabled and stop, You can use iptables.

全解:解决Apache下403 Forbidden错误

出现这样的问题一般有两种可能性。

一种可能性是DocumentRoot选项的设置,如果在安装好apache2后修改了该选项,并且忘记了配置该新目录的访问权限就会出现这样的情况。比如apache2安装好后默认的参数如下

DocumentRoot /usr/local/www/data

<directory “/usr/local/www/data”>
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</directory>我们常常会重新指定web文件存放的目录,比如设定
DocumentRoot /var/www
这时往往会忽略了对后面的Directory 项的修改,必须将里面的路径同时修改为 /var/www才行,否则将会访问所有目录都出现 403 forbidden错误。

 

 

配置了下php的php.in文件再次localhost打开发现错误:HTTP 错误 403 – 禁止访问,即403 Forbidden:You don’t have permission to access / on this server.权限又不够了?

 

马上打开apache的配置文件httpd.conf,逐行检查。在大约快一半的地方有以下这段代码:

<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
发现了吧。由于配置了php后,这里的“Deny from all”已经拒绝了一切连接。把该行改成“allow from all”,修改后的代码如下,问题解决。

<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
allow from all
</Directory>

 

 

另外一种可能性出现在我们配置了不同的VirtualHost,并且有某个VirtualHost的DocumentRoot不在全局的DocumentRoot目录下,这时必须在全局种单独增加对该目录的Directory 项进行设置,否则该VirtualHost下的所有访问均会出现403 forbidden错误。

这个问题是因为Apache2对于权限和安全的更高要求,对分布在不同磁盘上的目录文件进行严格管理,我们进行web规划的时候必须注意这一点。

 

 

试试下面的步骤。

第一:看看是不是Directory配置错了,好像一般不会是这个原因
第二:看看User Group指定的用户有没有权限访问那个目录,否则用chown修改目录的所有者
第三:看看是不是seLinux搞得鬼,一般没事把selinux停了再重启linux,selinux的配置文件在/etc/selinux/config,改成disable
第四:我把所有的都做了发现还是不行,那么可能是apache是用root安装的,把apache卸了用一个非root用户重新安装。

关闭防火墙

备注:
一、查看SELinux状态:
1、/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态
SELinux status: enabled
2、getenforce ##也可以用这个命令检查
关闭SELinux:
1、临时关闭(不用重启机器):
setenforce 0 ##设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式
2、修改配置文件需要重启机器:
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可

二、关闭防火墙
重启后生效
开启: chkconfig iptables on
关闭: chkconfig iptables off 或者 /sbin/chkconfig –level 2345 iptables off

2) 即时生效,重启后失效
service 方式
开启: service iptables start
关闭: service iptables stop
iptables方式
查看防火墙状态:
/etc/init.d/iptables status
暂时关闭防火墙:
/etc/init.d/iptables stop
重启iptables:
/etc/init.d/iptables restart

redis 学习笔记

# redis 学习笔记
## redis 实用命令
1. 启动服务,指定端口
开启服务 `redis-server –port 6370`
停止服务 `redis-cli -p 6370 shutdown `
2. 获取所有的配置
~~config get *
3. 远程服务器上的命令
~~ redis-cli -h host -p port -a password
4. 获取所有的键 `keys * `

5. 返回键的剩余时间 `ttl key`
6. 返回键的类型 `type key`
7. 选择数据库 ` select 1 `
8. 关闭连接 `quit`
9. 服务器验证给定的密码 `auth password`
10. 服务器的统计数据和信息。 `info`
11. `slaveof 114.11.11.11 6379` 设置当前服务器为指定主机的从属服务器,并且删除原有数据,同步主服务器内容
%% 如果没有同步主服务器内容,修改从服务器的配置,bind ip 或者 bind 0.0.0.0 (不安全)

## redis 数据类型
1. Strings-字符串
~~ set xiaofang xiaofnag
~~ get xiaofng
2. Hashs-哈希值;Redis的哈希键值对的集合。 Redis的哈希值是字符串字段和字符串值之间的映射,所以它们被用来表示对象
~~ hmset user:1 name xiaofang age 22 sex male
~~ hgetall user:1
3. lists-列表
~~ lpush list redis
~~ lrange list 0 10
4. sets-集合
~~ sadd list2 redis
~~ smembers list2
5. zset-有序集合
~~ zadd list3 0 redis
~~ zrangbyscore list3 0 1000

## redis 的键命令
1. 此命令删除键,如果存在
~~ del xiaofang
2. 该命令返回存储在指定键的值的序列化版本。
~~ dump key
3. 此命令检查该键是否存在。
~~  exist key
4. 指定键的过期时间
~~  expire key 100000
5. 指定键的过期世间,unix时间戳
~~ expireat key 100000
6. 设置键以毫秒为单位到期
~~ pexpire key milliseconds
7. 移动键到另一个数据库
~~ move key 1
8. 选择数据库
~~ select 1
9. 移除过期的键
~~ persist key
10. 以毫秒为单位获取剩余时间的到期键
~~ pttl xiaofang
11. 以秒为单位获取剩余时间的到期键
~~ ttl key
12. 从Redis返回随机键
~~ randomkey
13. 更改键的名称
~~ rename key newkey
14. 返回存储在键的数据类型的值。
~~ type key

redis Connection refused

Could not connect to Redis at (ip):6379: Connection refused

连接失败

1.检查redis-server 有没有开启服务

2.修改配置文件(/usr/local/redis/etc/redis.conf)
bind 指定ip     0.0.0.0(所有ip)

3.关闭防火墙   chkconfig iptables off  重启永久生效

备注: slaveof ip port 如果不能同步数据,也是检查以上情况