Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

Pushing to the Server while the Website is in Use

Hello,

I am trying to figure out with what method do web developer use in order to build the website while it is in use ?

This is what I am trying to think : Using gitlab.

So I put all the files in gitlab and pull the site from the terminal while to the staging.

After update - re-pull all the sites to the staging website with force so that it replace everything.

The same method also utilize when pull all the files to the production server. After another update pull it with force so that it replace everything to the production server. While for the database, you just have to run the migration one by one after the updates in order to make the database works while it is in use.

Is this how most developers works ?

0 likes
19 replies
tykus's avatar

There are tools like Envoyer to facilitate zero downtime deployments.

This article gives you some understanding of the technique using symlinks to serve a different release of the project; which has been fetched to a different directory on the server

davy_yg's avatar
Level 27

That's a good idea, but I don't think my client is willing to pay for the service. Maybe 30 minutes downtime is not a big deal.

Is there any git command like: pull -f

Pull with force ? So that it replace all the files after the pull ? Since, if normal pull will leave a lot of conflict and I don't want to debug the file from the server. Since I am the only developer for this current project.

Sinnbeck's avatar

@davy_yg 30 minutes?!?!?! Even without zero downtime, pulling and migrating shouldn't take 30 minutes. Maybe 30 seconds at the very max.

And no need to pull with force, as your production env, will never change any of the files managed by git. But you can do git reset --hard on production to ensure it is indeed clean

davy_yg's avatar
Level 27

@Sinnbeck What is git reset --hard for ? Will it replace everything in the current directory from with everything that I pulled from ?

Sinnbeck's avatar

@davy_yg It rolls back git to the last "commit". So it just discards any changes that would have been made on production

git reset --hard //reset to last time it pulled
git pull //pull newest code
davy_yg's avatar
Level 27

@Sinnbeck See I only worry if there are code conflicts after the pull since there would be extra codes that I added. I don't want having to fix all the conflicts on the server.

davy_yg's avatar
Level 27

@Sinnbeck Supposing that the owner of the website would like to add extra features while the website is being used. Just like updating the current website with extra features.

I would add the codes from localhost and push it to the gitlab. Then pull my gitlab codes to the production server after testing it in the staging server.

Will it not create any code conflicts in the production server ?

Sinnbeck's avatar

@davy_yg No. Only if you have changed code directly on the production server. Conflicts in git happen when the same file has been changed by two different persons/branches etc. So production should always be "clean"

Snapey's avatar

You can create a simple script to have near zero downtime;

#!/bin/sh

UNIX_TIME=$(date +%s)

DEPLOYMENT_DIRECTORY=$UNIX_TIME

mkdir -p $DEPLOYMENT_DIRECTORY
git clone --depth 1 --branch main [email protected]:repository/repositoryname.git $DEPLOYMENT_DIRECTORY

cd $DEPLOYMENT_DIRECTORY

cp ../master/.env .

composer install --no-dev

cd ..
echo removing storage
rm -Rf $DEPLOYMENT_DIRECTORY/storage

echo link master storage
ln -s -n -f -T ~/master/storage $DEPLOYMENT_DIRECTORY/storage

#link to the latest deployment from Live
ln -s -n -f -T $DEPLOYMENT_DIRECTORY Live

cd ~/Live

# echo link public to storage
php artisan storage:link

php artisan view:clear
php artisan cache:clear

echo route:cache command
php artisan route:cache

echo config:cache command
php artisan config:cache

echo migrations
php artisan migrate --force

cd ..
echo Done!

This

  • creates a deployment folder with the timestamp as the folder name
  • clones the repository (only 1 level deep)
  • composer installs all the vendor files
  • copies the .env file from the master folder
  • clears and sets cache and other stuff
  • creates a Symlink 'Live' that always points to the current deployment
  • symlinks the storage folder to one in master

The webhost is configured to serve Live/public as its document root.

The result is near zero downtime.

1 like
TobiasS's avatar

@Snapey Thx, for a great deployment script!

If I have assets that needs to be compiled. (Using laravel-mix)

Do you compile your assets locally (npm run production) and push the assets? (Or using GitHub actions or something similar)

Or would you advice to add that to the script? And compile on the server, before changing symlink? The problem I have when compiling on the server is that laravel-mix (devDependency) needs to be installed. And I do not want to install devDependencies on the production server.

Best regards, Tobias

Snapey's avatar

@TobiasS the assets are compiled locally, and the resulting files are on the git commit. So nothing needs to be compiled in production

davy_yg's avatar
Level 27

Anyone ever used - JENKINS ? https://docs.gitlab.com/ee/integration/jenkins.html

Is it a common practice ?

Like testing the codes in local and push it to gitlab and pull it from the server everytime will takes around 6 minutes before I get to see the debug code live on staging server again.

cosmeoes's avatar

@davy_yg For the production server I think you best option is the script that @snapey suggested.

For the staging server, if this is a server only used for your testing and you would like to see the changes immediately, you could use an extension for your code editor that copies the files to the server (through scp or rsync) every time you save the file.

I've never used jenkins, so I can't speak on that.

davy_yg's avatar
Level 27

@cosmeoes I am using sublime by the way for my code editor. Can you do that with sublime ?

Sinnbeck's avatar

@davy_yg yes I use jenkins for testing. But stick with learning development first.

Sinnbeck's avatar

For all of my projects (also my personal blog) I use ploi. Setting up zero downtime up is just checking a box. And setting up a staging site is also quite easy if you need one. They are quite cheap

https://ploi.io

Please or to participate in this conversation.