1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to convert a PHP site to a bunch of static files in Unix

Discussion in 'HTML & Website Design' started by badger_, Nov 22, 2016.

  1. #1
    Hi. I have managed to make an automated way to compile a simple PHP application to a bunch of static files and I share it, I hope some of you find it useful. This has some advantages:
    • Security: no code is best code.
    • Speed: faster response times and better scalability.
    • Price: hosting the site will cost less money.
    • Stability: less things running means less chance of something breaking.
    I have tried several static generators but in the end they shoot me in the foot. So the best for me is to run PHP locally, compile and upload to the server.

    Compile script

    I assume the PHP application has pretty urls, all of them ended in '/' character. First of all, set two entries in /etc/hosts pointing to 127.0.0.1 (example.home and example.sandbox). Once the PHP application is running on http:// example.home, I make a recursive download using wget with -r and --adjust-extension options. This download the full sites and attaches index.html termination to every page.

    Next thing to do is replace the domain name in the case you use base href HTML tag.Next thing to do is replace the domain name in the case you use base href HTML tag. Packaging all of this in a small script:
    
    rm -rf example.sandbox
    wget -r --adjust-extension http://example.home
    mv example.home example.sandbox
    for i in `find example.sandbox -name "*html"`; do sed -i 's/example.sandbox/example.com/g' $i; done
    
    Code (markup):
    Upload script

    After testing that example.sandbox works fine, change example.sandbox references to example.com and upload it to the server:
    
    for i in `find example.sandbox -name "*html"`; do sed -i 's/example.sandbox/example.com/g' $i ; done
    rsync -czrpgoD --rsh="ssh" example.sandbox/* server:/htdocs/example.com
    ssh server "sh ~/wwwsync.sh"
    
    Code (markup):
    Last line executes a script to sync the second server for redundancy.

    Related article: Go static or go home by Paul Vixie.
     
    badger_, Nov 22, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    While that is perfectly fine for very simple applications, why would you even need to have PHP for such simple websites? The minute you need database-access, dynamic content, etc., your solution won't work - and if you don't need any dynamic content, then you can just code them in plain HTML/CSS anyway.
     
    PoPSiCLe, Nov 23, 2016 IP
  3. badger_

    badger_ Greenhorn

    Messages:
    52
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    20
    #3
    With PHP I can do more things like templating, tags, pagination and add content very fast without having to edit tons of pages. Doing this in plain HTML/CSS is painful.
     
    badger_, Nov 23, 2016 IP
  4. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #4
    I am trying, and failing, to come up with a rational use case for your solution. SSI would handle low level templating. Or, for that matter, Emacs's Org Mode see here for includes. For more, see Org Manual, especially the export section.

    Org Mode is an integral part of the Emacs text editor.

    gary
     
    kk5st, Nov 23, 2016 IP
  5. asifkherani

    asifkherani Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    hi sir you there we do it this your ASAP because we are work in team form sir?thanks
     
    asifkherani, Nov 24, 2016 IP
  6. badger_

    badger_ Greenhorn

    Messages:
    52
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    20
    #6
    I have looked at the link but it seems too complicated for me (I never used emacs), I just want to write plain HTML with any plaintext editor and get the content formatted into a website automatically. One advantage of PHP is that it's designed for the web and there are tons of useful functions and libraries, it gives me freedom instead of restrictions like all static site generators I have tried. This made the trick for me, I'm surprised you don't find it useful (at least for certain use cases).

    I find very convenient to have locally a dynamic site and, from time to time, "freeze" and upload it into production.
     
    badger_, Nov 24, 2016 IP
  7. kk5st

    kk5st Prominent Member

    Messages:
    3,497
    Likes Received:
    376
    Best Answers:
    29
    Trophy Points:
    335
    #7
    It looks daunting if you don't realize you need only learn what you are using.

    I've used Emacs for about twenty years and still only know a small portion of its capabilities. Not a worry, since if I need to, I go to the manual and learn enough to get by this time and then forget it until the next time. What I use regularly, I do without having to think about it. The text editor is where you live and I understand if you don't want to change; it's like a cook and his knives, personal.

    gary
     
    kk5st, Nov 24, 2016 IP