linux下安装nginx与php5.6

本人是centos的服务器,下面教大家配置ngnix服务器并搭建php的运行环境

1、安装ngnix

yum install nginx  

安装完成后可以启动nginx,在浏览器里面访问,查看nginx是否安装成功。端口默认为80。

systemctl start nginx  
nginx中yum安装的默认网站根目录在/usr/share/nginx/html

2.安装 php + php-fpm

rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm

yum install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64

yum install php56w-fpm

php -v # 查看 php 版本

3、配置nginx与php一起工作

打开nginx主配置文件。

vim /etc/nginx/nginx.conf

在http模块中添加配置:  
       location / {  
        root   /usr/share/nginx/html;  
           index  index.html index.htm index.php;  
        }  
location ~ \.php$ { 
           fastcgi_pass   127.0.0.1:9000;  
           fastcgi_index  index.php;  
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
           include        fastcgi_params;  
       }  

改动nginx默认的fastcgiparams配置文件: vim /etc/nginx/fastcgi_params 在文件的最后增加两行:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
fastcgi_param PATH_INFO                $fastcgi_script_name;  

PHPMVC框架的路由规则参考:https://blog.csdn.net/sun_96216/article/details/88222734

然后重启一下服务:

systemctl restart nginx 

systemctl restart php-fpm 

在网站根目录创建一个index.php文件,并运行

文件内容如下:

<?php    
phpinfo();    
?>    

提示nginx中yum安装的默认网站根目录在/usr/share/nginx/html

4、安装配置VSFTPD

yuminstall -y vsftpd

#开放ftp服务添加到防火墙外
firewall-cmd --permanent --add-service=ftp

#使其生效
firewall-cmd --reload

#重启防火墙
systemctl restart firewalld.service

设置SELinux

大部分情况下,我们访问ftp的时候会被SELinux拦截,当然没也设置SELiunx也能正常访问,此步可以跳过。

通常的作法是关闭SELiunx,这样做会引起其它安全问题,嫌麻烦的可以直接关闭:

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存退出

setenforce 0 #让SELinux进入Permissive模式(宽容模式)

SELiunx一共有三种模式:

  1. enforcing(强制模式 ):开始限制domain/type
  2. permissive(宽容模式) :仅会有警告信息
  3. disabled(关闭):关闭SELinux

设置SELiunx:

[root@localhost ~]# /usr/sbin/sestatus -v     #查看SELinux状态
SELinux status:                 enabled    #启用
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
setenforce 0 #暂时让SELinux进入Permissive模式

这个时候我们尝试访问一下ftp目录,发现能够正常访问。我们查看一下权限:

[root@localhost ~]# getsebool -a | grep ftp
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> off

ftp_home_dir和allow_ftpd_full_access必须为on 才能使vsftpd 具有访问ftp根目录,以及文件传输等权限。

setsebool -P tftp_home_dir 1
setsebool -P allow_ftpd_full_access 1

让我们再回到强制模式:

setenforce 1 #进入Enforcing模式

如果还是不行的话,可能是我们的目录没有权限:

chmod -R 777 /usr/yong.cao/ftp   #ftp的访问路径

用vim /etc/vsftpd/vsftpd.conf打开后修改或者新增:

anonymous_enable=NO    #不允许匿名访问

write_enable=YES    #写权限

local_root=/usr/yong.cao/ftp   #这里是我自定义的ftp目录

chroot_local_user=YES    #这个是限制ftp用户只能在自己目录,如果ftp可以跳到其它任意目录是比较危险的,建议限制

allow_writeable_chroot=YES   #2.3.5之后,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了,需新增此配置

//

在网上找了很多,都是改PASV被动模式,在配置文件中添加下面语句:

pasv_enable=YES
pasv_min_port=6000
pasv_max_port=7000

添加一个ftp用户

此用户就是用来登录ftp服务器用的,不可以用这个登录
# useradd ftpuser -s /sbin/nologin -d 你的上传路径

# passwd ftpuser
输入两次密码后修改密码

chown -R ftp用户名:组名 目录  //组名可不写,修改目录所属者-为了FTP也可以控制PHP WEB用户生成的文件夹
systemctl restart vsftpd.service

5、设置开机自启项

systemctl enable XXXXXX

6、防火墙开启端口

firewall-cmd --zone=public --add-port=80/tcp --permanent
命令含义:  --zone #作用域    --add-port=80/tcp #添加端口,格式为:端口/通讯协议    --permanent #永久生效,没有此参数重启后失效

同时也需要在阿里云管理平台的安全组设置规则:

网站 80

FTP 20、21 (被动模式增加6000/7000)

MYSQL 3306

开启后需要重启防火墙才生效     【重启命令】:  firewall-cmd --reload

要点:服务器防火墙端口开放,开机启动,selinux也得要注意。如果服务器是阿里云的,也要在它的安全组设置开放端口,NND!
 


版权声明:本文为sun_96216原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。