How can I apply code changes to my running website, if I have bug fixes or updates? The simplest way I can think of is to set up the same site in a different directory for testing the changes, and then put my website offline for a period of time to update the files.
Is there a bet开发者_开发技巧ter way?
Creating a copy of the live site is certainly a good step, applying changes to the copy before applying the same to the live site.
A common production environment would include a further set of steps.
- Run a local development copy
Have a copy of your website running on your development machine. This requires that you development machine is running a web server, database server and, if necessary, a mail server.
For PHP/Apache/MySQL environments, take a look at http://www.apachefriends.org/en/xampp.html.
You can safely develop, break, test and change a your local development environment - Source control
Use Subversion, Mercurial or Git to keep your code under source control. Ensure your local development environment is kept under source control. Ensure your live environments are under source control. Develop locally, test changes and commit the changes back. - Staging and live environments
Maintain more than one 'live' copy - the actual live public site and as close a replica as possible. Ensure both are under version control.
Once locally-tested changes have been tested, update your staging environment (usin your source control system) and test again. Once your staging site is stable, you can use your source control system to update your live site.
There will not generally be much of a need to take the real live site offline to apply updates, but be sure that you can safely do so if needed.
Jon Cram answered this really well, but I think there's a few more things that are really important when maintaining development environments:
- Evnironments: Make sure the 2 environments you have are as close as possible to each other. If you're running PHP 5.1, MySQL 5.0, and Apache 2.2 and RHEL in production, make sure you're using exactly the same versions of everything in your sandbox.
- Data: Make a copy of your database, and use that. DON'T EVAR write or test code against data that's in production. One
DELETE
without aWHERE
and you're in trouble. (You have a backup, right?) - Configuration: Keep all your variables for connecting to databases, email addresses for support, etc in their own file. This way, you can just swap out connection parameters in your different environments without actually altering code. Also, I've found it's helpful to commit it separate from your code.
- Build scripts: It's helpful to have a command line script that will handle applying all the changes to your site. It can as simple as
svn update
or complex enough to require it's own app and library. See http://phing.info/trac/ for a good example.
Also (shameless plug), here's a post I write a while ago on the importance of sandboxes: http://chr.ishenry.com/2010/02/22/sandboxes/
You can run new version at different subdomain (or subfolder), and simply change domains handling after testing.
Nope, there is no better way (duh!)
Well, okay, there might be some better practises, as in you should have a seperate machine for testing purposes. But other than that, don't expect enlightment. You just take the site offline when the load is low (aka during the night) and change/replace stuff.
Method 1
Create a Copy of the Live Site on the computer you use for developing the webstie. Follow these steps:
Setup a Local Testing Server
Using Xamp or WampServer you can easily setup a local server on the development computer. Just install all in one server package and copy your website files to htdocs folder. Then you can open your website by just visiting http://localhost/ in the browser. I personally recommend using Xamp
Source control
Use Subversion, Mercurial or Git to keep track of updates you make to your site. These softwares keep track of changes you make to every file. They will enable you to undo any error in your website which may occur during development.
Keeping a Staging live copy of your site
Keep a staging live copy of your website on any another location on your server which is exactly same to the publically available live site. Once you update and test the local copy of the site. use your source control software to update the staging live site. When the staging site turn out to be stable and working. Use source control software again to update the publically available live site. This way your website will remain online all the time while you update it.
Method 2
Its a rather expensive solution ( I personally do not recommended it as the first method will also ensure the same results). If there is no downtime available keep another copy of the site running on other secondary server that you can divert users to when you want to update the main site. After updates you can divert your users back to the actual site. You can also use State server Sitting on another server to ensure that every session is recorded and maintained when user switch form one to another site.
Method 3 (Easy method for those who do not understand the above two methods)
Install Xamp copy your site files to xamp\htdocs folder. This copied site will be your local copy. Update the site and check it locally by typing and visiting http://localhost/ in your browser. Once the site is checked. Update all the files with filezilla ftp client(select update new files when uploading.).
On very busy sites AND severe SLA for downtimes AND using apache webserver AND for updates involving a significant number of files (eg.: >10), over the suggestions of "Jon Cram" and "Chris Henry" you can use this simple trick:
in your live environment using 2 document root, 1st with your old files and the 2nd with new files (you have already testing in staging), and symbolic links to share what you cannot duplicate, then:
1. in the apache config file switch the document root from old to new
2. check the config file (apachectl -S)
3. graceful restart the apache (apachectl graceful)
the apache will finish to serve the old requests in the old environment and start the new requests in the new environment.
- Of course you have already done the patches to DB
- Of course you have to write code that avoid conflicts to DB (eg.: using new tables and new cols)
- Of course is more simple: stop the site, patch it and restart it
May be you don't use apache but many web server have this capabilities.
You should use a copy of your site on your computer. Then after making updates/code changes etc. Update the site with Filezilla FTP client. Filezilla will just replace the files that you have updated and during all this process your site will be online.
精彩评论