centos nginx上搭建服务器

来源:互联网 发布:sql如何使用 编辑:程序博客网 时间:2024/06/05 07:45

第一步:在centos下面下载 --- nginx下载地址 :http://nginx.org/download

         wget http://nginx.org/download/nginx-1.8.0.tar.gz

         解压 tar zxf nginx-1.8.0.tar.gz
第二部:新建一个用户nginx在安装操作
        useradd xiangping
        passwd xiangping
第三步、准备需要的环境

yum install gcc

yum -y install pcre-devel

yum install -y zlib-devel


第四步、解压和测试安装环境

./configure

make && make install


第五步、防火墙,把nginx端口防火墙打开


***************防火墙知识点****************

service firewalld restart 重启

service firewalldstart 开启

service firewalld stop 关闭

systemctl status firewall /firewall-cmd--state查看firewall服务状态

firewall-cmd--zone=public--permanent--add-port=8010/tcp添加8010端口:--zone=public:指定的zone为public

firwall-cmd--permanent--add-port=9527/tcp命令的方式添加端口

firewall-cmd--list-all查看防火墙规则

systemctl disable firewalld.service #禁止firewall开机启动


yuminstall iptables-services安装iptables防火墙

vi /etc/sysconfig/iptables编辑防火墙配置文件

service iptables start/stop/restart 开启

systemctl enable iptables.service#设置防火墙开机启动


1、开启端口(以80端口为例)

      方法一:

         /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT   写入修改

         /etc/init.d/iptables save   保存修改

        service iptables restart    重启防火墙,修改生效

       方法二:

       vi /etc/sysconfig/iptables  打开配置文件加入如下语句:

       -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT   重启防火墙,修改完成

 

2、关闭端口

     方法一:

         /sbin/iptables -I INPUT -p tcp --dport 80 -j DROP   写入修改

         /etc/init.d/iptables save   保存修改

        service iptables restart    重启防火墙,修改生效

       方法二:

       vi /etc/sysconfig/iptables  打开配置文件加入如下语句:

       -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j DROP   重启防火墙,修改完成

 

3、查看端口状态

      /etc/init.d/iptables status


CentOS 配置防火墙操作实例(启、停、开、闭端口):

 

注:防火墙的基本操作命令:

查询防火墙状态:

[root@localhost ~]# service   iptables status<回车>

 

停止防火墙:

[root@localhost ~]# service   iptables stop <回车>

 

启动防火墙:

[root@localhost ~]# service   iptables start <回车>

 

重启防火墙:

[root@localhost ~]# service   iptables restart <回车>

 

永久关闭防火墙:

[root@localhost ~]# chkconfig   iptables off<回车>

 

永久关闭后启用:

[root@localhost ~]# chkconfig   iptables on<回车>

******************************************************************


打开nginx服务

service nginx start/stop/restart

也可以去安装的路径下:

cd /usr/local/nginx/sbin

./nginx 打开

./nginx -s stop 关闭

./nginx -s quit 关闭



第六步、配置ftp和nginx,让“http://”也可以访问“ftp://”文件

1、打开vi /usr/local/nginx/conf/nginx.conf

2、修改配置文件的这几个地方

第一行添加:use root;



把以前的location注释掉换成自己的路径

location / {
            root   /home/xiangping/images;       

}


再次启动nginx服务


第七步:安装ftp服务器

# 安装vsftpd

rpm -qa | grep vsftpd

yum -y install vsftpd

# 启动ftp服务
service vsftpd start
# 查看ftp服务状态
service vsftpd status
# 重启ftp服务
service vsftpd restart
# 关闭ftp服务
service vsftpd stop


第八步、修改ftp配置文件

#进入vsftpd配置文件

vim /etc/vsftpd/vsftpd.conf 


# 禁止匿名用户anonymous登录
anonymous_enable=NO
# 允许本地用户登录
local_enable=YES
# 让登录的用户有写权限(上传,删除)
write_enable=YES
# 默认umask
local_umask=022
# 把传输记录的日志保存到/var/log/vsftpd.log
xferlog_enable=YES
xferlog_file=/var/log/vsftpd.log
xferlog_std_format=NO
# 允许ASCII模式上传
ascii_upload_enable=YES
# 允许ASCII模式下载
ascii_download_enable=YES
# 使用20号端口传输数据
connect_from_port_20=YES
# 欢迎标语
ftpd_banner=Welcome to use my test ftp server.
# 接下来的三条配置很重要
# chroot_local_user设置了YES,那么所有的用户默认将被chroot,
# 也就用户目录被限制在了自己的home下,无法向上改变目录。
# chroot_list_enable设置了YES,即让chroot用户列表有效。
# ★超重要:如果chroot_local_user设置了YES,那么chroot_list_file
# 设置的文件里,是不被chroot的用户(可以向上改变目录)
# ★超重要:如果chroot_local_user设置了NO,那么chroot_list_file
# 设置的文件里,是被chroot的用户(无法向上改变目录)
chroot_list_enable=YES
# touch /etc/vsftpd/chroot_list 新建
chroot_list_file=/etc/vsftpd/chroot_list
use_localtime=YES
# 以standalone模式在ipv4上运行
listen=YES
# PAM认证服务名,这里默认是vsftpd,在安装vsftpd的时候已经创建了这个pam文件,
# 在/etc/pam.d/vsftpd,根据这个pam文件里的设置,/etc/vsftpd/ftpusers
# 文件里的用户将禁止登录ftp服务器,比如root这样敏感的用户,所以你要禁止别的用户
# 登录的时候,也可以把该用户追加到/etc/vsftpd/ftpusers里。
pam_service_name=vsftpd


# 重启 vsftpd
service vsftpd restart



第九步、 防火墙开启21端口

因为ftp默认的端口为21,而centos默认是没有开启的,所以要修改iptables文件

[root@localhost ~]# vim /etc/sysconfig/iptables

在行上面有22 -j ACCEPT 下面另起一行输入跟那行差不多的,只是把22换成21,然后:wq保存。

还要运行下,重启iptables:service iptables restart  


第十步、 修改selinux

外网是可以访问上去了,可是发现没法返回目录(使用ftp的主动模式,被动模式还是无法访问),也上传不了,因为selinux作怪了。

修改selinux:

执行以下命令查看状态:getsebool -a | grep ftp

allow_ftpd_anon_write --> off

allow_ftpd_full_access --> off

allow_ftpd_use_cifs --> off

allow_ftpd_use_nfs --> off

ftp_home_dir --> off

ftpd_connect_db --> off

ftpd_use_passive_mode --> off

httpd_enable_ftp_server --> off

tftp_anon_write --> off

执行上面命令,再返回的结果看到两行都是off,代表,没有开启外网的访问

[root@localhost ~]# setsebool -P allow_ftpd_full_access on[root@localhost ~]# setsebool -P ftp_home_dir on

这样应该没问题了(如果,还是不行,看看是不是用了ftp客户端工具用了passive模式访问了,如提示Entering Passive mode,就代表是passive模式,默认是不行的,因为ftp passive模式被iptables挡住了,下面会讲怎么开启,如果懒得开的话,就看看你客户端ftp是否有port模式的选项,或者把passive模式的选项去掉。如果客户端还是不行,看看客户端上的主机的电脑是否开了防火墙,关吧)

FileZilla的主动、被动模式修改:

菜单:编辑→设置


第十一步、设置开机启动vsftpd ftp服务

[root@localhost ~]# chkconfig vsftpd on
第十步、nginx和ftp都从启动

service nginx restart

service vsftpd restart




systemctl enable nginx.service             #设置开机自启动systemctl disable nginx.service            #停止开机自启动

原创粉丝点击