1

i was wondering how i can, for example, overwrite my "index.php" so i can update my page automatically.

What I'm trying to do is communicate 2 servers, the server 1 tells the server 2 when there's a new update and the server 2 download it:

*Upload the new webpage "content.zip" to my webserver1: (i do this because this way i can backup my content)

form

<form action="uploader.php" method="post" enctype="multipart/form-data">
                <input type="file" name="file" />
                <input type="submit" value="Upload" />
            </form>

uploader.php

<?php 
$folder = "content/";
opendir($folder);
$destiny = $folder.$_FILES['file']['name'];

copy($_FILES['file']['tmp_name'],$destiny);
echo "Successful";
?>

*Then tell the webserver2 to download the new content and replace the old.

if(somevariable == true){
    file_put_contents("newcontent.zip", file_get_contents("http://someurl/fsadasd.zip"));
}

And then:

<?php
$zip = new ZipArchive;
$res = $zip->open('newcontent.zip');
if ($res === TRUE) {
  $zip->extractTo('var/www/');
  $zip->close();
  echo 'woot!';
} else {
  echo 'doh!';
}
?>

Code reference

Also, do i have to stop the LAMP process then modify the files and start it again?

If someone knows a better way to do this, please tell me. Thanks.

Community
  • 1
  • 1
Sebastian Tare B.
  • 542
  • 3
  • 5
  • 14
  • 1
    I Personally think bash is well suited for this. See SCP and rsync commands. If you are having issue ping me up :) – Kheshav Sewnundun Feb 20 '15 at 07:12
  • 1
    Your web server process probably does not (and should not) have write access to /var/www/ and so this probably will not work. What you should be doing instead is to use ssh/scp/rsync/sftp to update the files on the server, as a user that does have write access to where your files are stored. If you grant your web server user write access to these files then your server will be hacked very quickly. – delatbabel Feb 20 '15 at 07:13
  • >Kheshav - I'll do, thanks ;) >delatbabel - I see...yes, the security is really weak, but i just want make this run, and then make it secure. thanks btw. – Sebastian Tare B. Feb 20 '15 at 07:35
  • I ended using dropbox and .bash, it does what i wanted and its safer than what i intended to do – Sebastian Tare B. Feb 23 '15 at 09:11

1 Answers1

1

Your method is good start, but is not recommended because

  • It is not secure
  • It is not safe

It is not secure as to achieve this you need write permission on your root web, and that is big security concern for web process.

It is not safe, because you do not do any Hashing check on your copied file? What will happen when say your web connection break or your index file wasn't written properly due to any disk error? I know you might be thinking it won't happen, but it does happen to me, when the above method failed for me, and I was sleeping. Got angry client that day.

The best way is even if you want to write it in PHP, just schedule your download script as some other linux User that has write permission but not your web user. Then that schedule will check some DB value or file on remote server, if available download it, extract it temporarily, check file hash, replace each file, check hash again.

But, why you want to do that, when you can easily use rsync and other sync utility that do the same for you, try using Version control and then sync file from that to your root of web. that is safe and sound.

Edit:

What I do is, we have SVN configured for our live site, and we have a cron running once per day that execute svn update on our root of web. Simple !!! at most if needed I do ssh and do the same if needed.

Sumit Gupta
  • 2,152
  • 4
  • 29
  • 46
  • Well I'm actually a novice on this, the problem i have is that where the data will be shown is a remote place, so the speed of the connection is very poor for ssh, so it's like: in the night it downloads the content and shows it in the day, but yeah, i think I'm gonna try rsync or something else. For now the security is not one of my priorities. Thanks for your great answer ;) – Sebastian Tare B. Feb 20 '15 at 07:45
  • SSH is usually slow when you do telnet due to high level of encryption it use, however when you run PHP script and schedule it to download it will be much faster as it use Internet of server which in most generic case is good and can easily download changes. SVN solutions works best and allow you to control version of it. you can try GIT from various free service to try it out. – Sumit Gupta Feb 20 '15 at 15:17