Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Code Deployment Tips

Tags:

In the past, I have been developing in a very amateurish fashion, meaning I had a local machine where I developed and tested code and a production machine to which I copied the code when I was done. Recently I modified this slightly to where I developed locally, checked the code into SVN and then updated the production machine through SVN.

Now I would like to start a new project and improve my workflow. Ideally I had the following in mind:

  • Have one or more local dev environments
  • Develop and test on local machine(s)
  • Use SVN (or Git) as code repository
  • Use a build tool to set up new environments (either dev, staging or production) and deploy code

Since I am not very familiar with this process, I am looking for suggestions on how to best set this idea up and the tools to use, especially when it comes to the build tools. I was looking into Ant and Phing (possibly make), but I am so new to this that I would really like to get some guidance. Are there any good tutorials or books about PHP deployment, especially for beginners? What I am especially interested in are the following topics:

  • Deployment to different types of servers with different settings (e.g. dev uses different db, db passwords, PHP error reporting than production or staging).
  • Deployment that automatically pulls code from SVN.
  • Deployment that temporarily sets a "Maintenance" page for production environment.
  • Once I mastered the above, maybe even do some testing in the build process.

I know my question might sound quite confused... I admit, I am new to this and might be a little off the target in what I really need. That's why any help is greatly appreciated.

like image 770
Steve Avatar asked Jun 03 '11 23:06

Steve


People also ask

How are PHP applications deployed?

Click Servers > Server types > PHP servers. Select the servers that you chose as deployment targets, and click Start. You can also start the application from Applications > All applications in the administrative console. However, doing so from this panel will start all the servers associated with this application.

Where can I deploy PHP site for free?

000webhost is a free web host that support PHP and MySQL apps. It also comes with a control panel which has PhpMyAdmin and a web-based file manager. Although 000webhost enables deploying your web app via file upload and is free of charge, it also comes with great security risks.


1 Answers

I would suggest making your testing deployment strategy a production-ready install-script -- since you're going to need one of those anyway eventually.

A few tips that may seem obvious to some, but are worth pointing out:

  • Your config file saved in your VCS should be a template, and should be named differently from the file that will eventually contain the actual settings. E.g. config-dist.php or config-sample.conf or sample/config-mysql.php or something along those lines. Otherwise you will end up accidentally checking in a server-specific configuration file over your template.
  • For PHP deployment, anticipate that some users will not be able to run server-side scripts through any mechanism other than the web server itself. A PHP-based installer is almost non-negotiable.
  • You should include a consumer-friendly update mechanism, and for that, wordpress is a great example of a project to emulate. A PHP script can (a) download the latest build, (b) use the ftp functions to update your application's files, and (c) execute an update script which makes the appropriate changes to the database, etc.
  • For heaven's sake don't do like [redacted] and make your users download and install separate patches for each point release. Have them download the latest (final) release which contains all the updates to date, and applies the correct ALTER TABLE functions in sequence.

Whether the files are deployed via SVN or through FTP, the install/update mechanism should be the same: get the latest files, run the update script. The updater uses the version listed in the PHP script and the version listed in the DB, and uses that knowledge to apply the appropriate DB patches in order. As for how to generate those patches, there are other questions here that you can refer to for more info.

As for the "Maintenance" page, just use the version trick mentioned above to trigger it (compare the version in the DB against the version in the PHP code). It's also useful to be able to mark a site as "down" to the public but make it visible to admins (like Joomla does), which you can trigger through database or filesystem flags.

As for automatically pulling code from SVN, I'd say you're better off with either a cron script or with commit triggers than working that into your application, since it wouldn't be relevant to end users.

like image 184
tylerl Avatar answered Oct 21 '22 02:10

tylerl