Deploying PHP Applications: Local Development to Production Servers
The process of deploying web apps depends on many things. These are the types of servers, and the tools you use. It also depends on the preferences of your team. Here is a basic way to deploy
PHP apps:
Run Tests:
Check your PHP code to see if it works right.
Download Dependencies:
Use Composer to get any other stuff your app needs.
Compile Assets:
Get your JavaScript and CSS files ready with tools like Webpack or Gulp.
Upload to Server:
Put your app on the web server using FTP or a PHP deployment tool.
Remember these two important things:
I. Version Control:
∙Keep all your code and stuff like config files in Git.
∙But do not put your dependencies and built files in there.
II. Don't Store Dependencies:
∙Putting dependencies in Git makes your project big and messy.
∙Nobody likes fixing conflicts.
How to Deploy PHP Projects: 5 Methods
Deploy Repository without Dependencies and Artifacts and Build Your App on Server
∙First, we build and test the app.
∙We can do this on our own computer or using a cloud service.
∙After the tests pass, we upload the source code to the server using FTP, SFTP, or Rsync.
∙This is done without including dependencies and artifacts.
∙The final step is to download the dependencies and build the app by running Composer on the server.
Pros And Cons
∙The PHP app's build environment is exactly the same as the running environment.
∙Dependencies download faster because they come from the closest mirror.
∙If we don't use any deployment practices to minimize downtime, it may take longer to download dependencies and build the application.
∙Even small changes can result in long build times. This can affect the production server's
performance.
2. Deploy Repository with Dependencies and Artifacts
In this method, PHP applications are compiled and tested first. Then uploaded to the web server along with dependencies and artifacts using a deployment tool.
Pros And Cons
∙The production server doesn't have to handle the build process. This reduces stress.
∙The application on the production server is identical to the one on the test server.
∙No need for SSH access to run scripts; old-school FTP works fine.
∙You need to build environment that matches the running environment for the application.
∙Since everything is deployed from the source code repository, the upload time can be
lengthy.
3. Git Variation
∙In this method, similar to #1, Git needs to be installed on the production server.
∙Instead of uploading files directly, deployment from the source code repository is done
using git push.
∙Then, a post-receive hook in Git triggers the building of the application code.
Pros And Cons
∙Git deployment is faster because only changesets are deployed.
∙No need to run SSH scripts
∙The webhook calls them on the server.
∙Without a mechanism to minimize downtime (like atomic deployment), the time to download dependencies and build the application may extend downtime.
∙The build time may be lengthy and affect the performance of the production server.
4. Zero-Downtime / Atomic Deployment
∙The methods mentioned earlier have a drawback: downtime.
∙This means your website won't be accessible to users during the deployment.
∙The solution to this is simple: deploy and build the application in a different folder than
the one being used.
Pros And Cons
∙Downtime is reduced to almost zero (only the time to create a symbolic link).
∙Instant rollback is possible; the previous version remains on the server, and switching back the symlink reverts to it.
∙More server space is needed to store previous revisions.
∙Requires writing several scripts unless you use a preconfigured template.
5. Docker Deployment
∙The final method to deploy a PHP application involves containerization with Docker.
∙Docker allows you to define your application's working environment in a single text file called Dockerfile.
∙This file is then used to build a Docker image containing your app.
∙This can be launched in any environment supporting Docker (Linux/MacOS/Windows).
Pros And Cons
∙The application works on every type of setup, eliminating the "strange, it works for me" error.
∙Rolling back is easy; you just run the previous version of the Docker image.
∙Build configuration and application environment are documented in the Dockerfile in the repository.
∙However, Docker adds another technology to the software stack, and some users claim it's not fully ready for production.
This is how the deployment of PHP applications can be done. For more details contact Askme technologies experts.