搭建一个页面并备份用户上传的文件项目作业

本文档详细介绍了在Linux系统中搭建小游戏服务器的过程,包括安装配置Nginx、限制特定IP访问、安装PHP以处理动态请求。同时,还提供了利用NFS和Sersync实现数据备份的方案,确保游戏数据的安全与同步。整个流程涵盖了从安装依赖、配置文件修改到服务启动和测试的每个步骤。

项目需求:

搭建小游戏

支持备份

限制192.168.1.8访问

一、下载nginx并安装

 [root@web03 opt]# wget http://nginx.org/download/nginx-1.19.10.tar.gz
    [root@web03 opt]# tar -xf nginx-1.19.10.tar.gz 
    [root@web03 opt]# cd nginx-1.19.10/
    [root@web03 nginx-1.19.10]# useradd www -u 1000
    
    
    #检查系统配置
    [root@web3 nginx-1.19.10]# ./configure --user=www --group=www --prefix=/usr/local/nginx   --with-stream    --with-mail        --with-http_perl_module      --without-http_gzip_module     --with-http_ssl_module
    
    #解决依赖
    [root@web03 nginx-1.19.10]# yum install -y pcre pcre-devel
    [root@web03 nginx-1.19.10]# yum install -y openssl openssl-devel
    [root@web03 nginx-1.19.10]# yum install perl-ExtUtils-Embed
    
    #编译并安装
    [root@web03 nginx-1.19.10]# make && make install
    
    #将nginx加入环境变量
    [root@web03 nginx-1.19.10]# vim /etc/profile
    shift + g  按 o
    NGINX_HOME=/usr/local/nginx/sbin
    PATH=$PATH:$NGINX_HOME
    export PATH
    [root@web03 nginx-1.19.10]# source /etc/profile
    
    #测试环境变量是否添加成功
    [root@web03 nginx-1.19.10]# nginx -V

解决启动nginx (Unit not found)问题

 [root@web03 nginx-1.19.10]# vim /etc/init.d/nginx
    #!/bin/sh
    # nginx - this script starts and stops the nginx daemin
    #
    # chkconfig:   - 85 15
    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
    #               proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config:      /usr/local/nginx/conf/nginx.conf
    # pidfile:     /usr/local/nginx/logs/nginx.pid
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Source networking configuration.
    . /etc/sysconfig/network
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    nginx="/usr/local/nginx/sbin/nginx"
    prog=$(basename $nginx)
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
    lockfile=/var/lock/subsys/nginx
    start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
    }
    stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
    }
    restart() {
    configtest || return $?
    stop
    start
    }
    reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
    }
    force_reload() {
    restart
    }
    configtest() {
    $nginx -t -c $NGINX_CONF_FILE
    }
    rh_status() {
    status $prog
    }
    rh_status_q() {
    rh_status >/dev/null 2>&1
    }
    case "$1" in
    start)
    rh_status_q && exit 0
    $1
    ;;
    stop)
    rh_status_q || exit 0
    $1
    ;;
    restart|configtest)
    $1
    ;;
    reload)
    rh_status_q || exit 7
    $1
    ;;
    force-reload)
    force_reload
    ;;
    status)
    rh_status
    ;;
    condrestart|try-restart)
    rh_status_q || exit 0
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    exit 2
    esac
    接下来就依次操作以下命令:
    [root@web03 nginx-1.19.10]# cd /etc/init.d
    [root@web03 init.d]# chmod 755 /etc/init.d/nginx
    [root@web03 init.d]# chkconfig --add nginx
    [root@web03 init.d]# service nginx start

二、配置nginx小游戏页面

1、nginx

   #修改配置文件内容
    [root@web03 init.d]# vim /usr/local/nginx/conf/nginx.conf
    
    
    user  www;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /usr/local/nginx/logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
        include /usr/local/nginx/conf.d/*.conf;
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    
    #创建网页配置文件存放目录
    [root@web03 init.d]# mkdir /usr/local/nginx/conf.d
    
    #自定义网站配置文件
    [root@web03 conf.d]# vim /usr/local/nginx/conf.d/zuoye.conf
    server{
        listen 80;
        server_name www.zuoye.com;
        location / {
        root /code/zuoye;
        index index.html;
        allow all;
        deny 192.168.1.8;
    
        }
    
    }
    
    #创建站点目录
    [root@web03 conf.d]# mkdir /code/zuoye -p
    
    #上传网站所需的html文件
    [root@web03 conf.d]# cd /code/zuoye/
    [root@web03 zuoye]# rz -E
    rz waiting to receive.
    [root@web03 zuoye]# unzip kaoshi.zip 
    
    #更改站点目录的属主属组
    [root@web03 zuoye]# chown -R www.www /code
    
    #测试重启
    [root@web03 zuoye]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@web03 zuoye]# systemctl restart nginx

2、因为nginx无法处理动态请求,所以这里我们需要安装php

#下载php源码包到opt目录,然后解压
[root@web03 ~]# cd /opt
[root@web03 opt]# wget https://www.php.net/distributions/php-5.6.40.tar.gz
[root@web03 opt]# tar -xf php-5.6.40.tar.gz 
[root@web03 opt]# cd php-5.6.40/ls 
#检查系统配置,并指定安装模块与用户
[root@web03 php-5.6.40]# ./configure --prefix=/usr/local/php --with-mysql --enable-fpm --with-pdo-mysql --with-fpm-user=www --with-fpm-group=www
#解决依赖
[root@web03 php-5.6.40]# yum install libxml2 libxml2-devel -y
#编译并安装
[root@web03 ~]# make -j && make install
#加入环境变量
[root@web03 ~]# vim /etc/profile
PATH=$PATH:/usr/local/php/sbin
export PATH
#启动php服务
[root@web03 ~]# systemctl start php-fpm
#通常源码安装无法直接用systemctl启动所以运行以下代码:
[root@web03 ~]# vi /lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=你的文件目录(我:/usr/local)/php/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#添加完成以后保存,使用systemctl list-unit-files --type=service查看有没有php-fpm.service
#如果没有就是用systemctl daemon-reload重新加载,在使用以上命令查看

#添加开机自启 
systemctl enable php-fpm.service
[root@web03 ~]# systemctl start php-fpm

详情可参考
https://www.cnblogs.com/ikai/p/13691706.html

3、nginx绑定php

[root@web03 ~]# vim /usr/local/nginx/conf.d/zuoye.conf 

server{
    listen 80;
    server_name www.zuoye.com;
    location / {
    root /code/zuoye;
    index index.html;
    }
    location ~* \.php$ {
        fastcgi_pass localhost:9000;
        #fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /code/zuoye/$fastcgi_script_name;
        include fastcgi_params;
    }
}
#重启nginx与php
[root@web03 ~]# systemctl restart nginx
[root@web03 ~]# systemctl restart php-fpm

#重启失败:cat/var/log/messages:发现配置文件名不对,改名
[root@web03 local]# cd /usr/local/php/etc/
[root@web03 etc]# mv php-fpm.conf.default php-fpm.conf

#更改配置文件
[root@web03 etc]# vim  /code/zuoye/upload_file.php 
<?php

header("Content-type:text/html;charset=utf-8");

ini_set('date.timezone','Asia/Shanghai');


//$wen="C:\wamp\www\linux-54-".date("Y-m-d");
$wen="/你自己的目录(我:/code/zuoye)/upload";
$dd=date("Y-m-d");
$pre=preg_match("/^([0-9])+_/",$_FILES['file']["name"][0]);
$size =$_FILES['file']["size"][0];

if (!is_dir($wen.'/')) {

  MKDIR($wen.'/', 0777);

}




// foreach($_FILES['file']['error'] as $k=>$v){

  if ($_FILES["file"]["error"][0] > 0 ) {
    echo "上传失败!请查看是否选择了需要上传的文件!";
    }else if($pre==0){
   
    echo "上传失败!文件名有误,请修改文件名为你的编号加下划线开头<br/>例如:1_老男孩_lnmp架构.mp4";


  }else if ($size<10) {

    echo "上传失败!文件为空文件!";
  }else{
   
    $tmp_name = $_FILES["file"]["tmp_name"][0];
    $name =$_FILES["file"]["name"][0];
	$name = iconv('utf-8','gb2312',$name);

    if (file_exists($wen."/" . $name))
      {
      echo "上传失败,文件".$_FILES["file"]["name"][0] . " 已经存在 ";
      }
    else
      {
      move_uploaded_file($tmp_name,$wen."/" . $name);
      echo "文件".$_FILES["file"]["name"][0]."上传成功";
      }
    
}

// }
?>

三、部署nfs+sersync

1、backup

  #安装rsync
    [root@backup ~]# yum install rsync -y
    [root@backup ~]# yum install -y nfs-utils rpcbind
    
    #编写rsync服务端配置文件
    [root@backup ~]# vim /etc/rsyncd.conf
    uid = rsync
    gid = rsync 
    port = 873 
    use chroot = no
    fake super = yes
    max connections = 200
    timeout = 600
    ignore errors     
    read only = false
    list = false      
    auth users = yzl
    secrets file = /etc/rsync.passwd
    log file = /var/log/rsyncd.log
    [backup]
    comment = welcome to oldboyedu backup!
    path = /backup
    #创建rsync服务需要使用的普通用户
    [root@backup ~]# useradd rsync
    #将用户名和密码写入rsync密码文件
    [root@backup ~]# echo "yzl:1" >/etc/rsync.passwd
    #更改密码文件权限
    [root@backup ~]# chmod 600 !$
    chmod 600 /etc/rsync.passwd
    #创建模块目录,并更改其属组属主
    [root@backup ~]# mkdir /backup
    [root@backup ~]# chown -R www.www !$
    #创建挂载点
    [root@backup ~]# vim /etc/exports
    /backup 172.16.1.0/24(rw,sync,all_squash)
    #开启nfs和rsync服务
    [root@backup ~]#  systemctl start nfs
    [root@backup ~]# showmount -e
    Export list for backup:
    /backup 172.16.1.0/24
    [root@backup ~]# systemctl start rsyncd

2、nfs

#将密码写入rsync客户端密码文件中,并更改权限为600
[root@nfs ~]# echo "1" >>/etc/rsync.passwd
[root@nfs ~]# chmod 600 !$
#创建挂载点
[root@nfs ~]#  vim /etc/exports
/sersync 172.16.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

#创建用户并修改同步目录属主属组
[root@nfs ~]# useradd www -u 1000
[root@nfs ~]#  chown -R www.www /sersync
#开启nfs服务
[root@nfs ~]# systemctl start nfs-server rpcbind
#上传sersync压缩包
[root@nfs sersync]# rz -E
rz waiting to receive.
[root@nfs sersync]# tar -xf sersync.gz
[root@nfs sersync]# mv GNU-Linux-x86/* ./
#编辑sersync配置文件
[root@nfs sersync]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
	<delete start="true"/>
	<createFolder start="true"/>
	<createFile start="true"/>
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="true"/>
	<modify start="true"/>
    </inotify>

    <sersync>
	<localpath watch="/sersync">
	    <remote ip="172.16.1.41" name="backup"/>
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-az"/>
	    <auth start="true" users="yzl" passwordfile="/etc/rsync.passwd"/>
	    <userDefinedPort start="false" port="873"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
	<crontab start="false" schedule="600"><!--600mins-->
	    <crontabfilter start="false">
		<exclude expression="*.php"></exclude>
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
	<plugin start="false" name="command"/>
    </sersync>

    <plugin name="command">
	<param prefix="/bin/sh" suffix="" ignoreError="true"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*)\.php"/>
	    <include expression="(.*)\.sh"/>
	</filter>
    </plugin>

    <plugin name="socket">
	<localpath watch="/opt/tongbu">
	    <deshost ip="192.168.138.20" port="8009"/>
	</localpath>
    </plugin>
    <plugin name="refreshCDN">
	<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
	    <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
	    <sendurl base="http://pic.xoyo.com/cms"/>
	    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
	</localpath>
    </plugin>
</head>

#运行测试
[root@nfs sersync]# ./sersync2 -dro confxml.xml 

四、测试:

将192.168.1.9   www.zuoye.com加入c:/windows/system32/drivers/etc/hosts文件中

在浏览器上传文件后,查看backup上/backup中是否有上传的文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值