{"id":16893,"date":"2025-04-04T20:27:18","date_gmt":"2025-04-04T18:27:18","guid":{"rendered":"https:\/\/tremhost.com\/blog\/?p=16893"},"modified":"2025-04-04T20:27:18","modified_gmt":"2025-04-04T18:27:18","slug":"how-to-deploy-a-website-from-git-on-your-server","status":"publish","type":"post","link":"https:\/\/tremhost.com\/blog\/how-to-deploy-a-website-from-git-on-your-server\/","title":{"rendered":"How to deploy a website from Git on your server"},"content":{"rendered":"<div id=\"bsf_rt_marker\"><\/div><h1>How to Deploy a Website from Git on Your Server<\/h1>\n<p>Deploying your website directly from a Git repository can streamline updates and version control. Follow these steps to deploy your website on your server:<\/p>\n<hr \/>\n<h2>1. Prepare Your Server<\/h2>\n<ul>\n<li><strong>Install Git:<\/strong><br \/>\nEnsure Git is installed on your server. On Ubuntu\/Debian:<\/p>\n<pre><code class=\"language-bash\">sudo apt update &amp;&amp; sudo apt install git -y\r\n<\/code><\/pre>\n<p>On CentOS\/AlmaLinux:<\/p>\n<pre><code class=\"language-bash\">sudo yum install git -y\r\n<\/code><\/pre>\n<\/li>\n<li><strong>Set Up Your Web Directory:<\/strong><br \/>\nDecide where your website files will live (e.g., <code>\/var\/www\/yourwebsite<\/code>). Create the directory if it doesn\u2019t exist:<\/p>\n<pre><code class=\"language-bash\">sudo mkdir -p \/var\/www\/yourwebsite\r\nsudo chown -R $USER:$USER \/var\/www\/yourwebsite\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>2. Clone Your Git Repository<\/h2>\n<ul>\n<li><strong>Navigate to Your Web Directory:<\/strong><br \/>\nChange to your website directory:<\/p>\n<pre><code class=\"language-bash\">cd \/var\/www\/yourwebsite\r\n<\/code><\/pre>\n<\/li>\n<li><strong>Clone the Repository:<\/strong><br \/>\nClone your Git repository into the directory:<\/p>\n<pre><code class=\"language-bash\">git clone https:\/\/github.com\/yourusername\/yourrepository.git .\r\n<\/code><\/pre>\n<p>Replace the URL with your repository\u2019s URL. The dot (<code>.<\/code>) at the end clones the contents into the current directory.<\/li>\n<\/ul>\n<hr \/>\n<h2>3. Set Up Deployment Options<\/h2>\n<h3>a. Manual Deployment<\/h3>\n<ul>\n<li><strong>Pull Updates:<\/strong><br \/>\nWhenever you update your repository, log in to your server, navigate to your web directory, and run:<\/p>\n<pre><code class=\"language-bash\">git pull origin main\r\n<\/code><\/pre>\n<p>Adjust the branch name (<code>main<\/code> or <code>master<\/code>) as needed.<\/li>\n<\/ul>\n<h3>b. Automated Deployment (Optional)<\/h3>\n<ul>\n<li><strong>Using Webhooks:<\/strong><br \/>\nSet up a webhook in your Git repository (on platforms like GitHub or GitLab) to trigger a deployment script on your server.<\/li>\n<li><strong>Create a Deployment Script:<\/strong><br \/>\nCreate a simple script (e.g., <code>deploy.sh<\/code>) in your web directory:<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/bash\r\ncd \/var\/www\/yourwebsite || exit\r\ngit pull origin main\r\n# Add any additional commands, like clearing cache or restarting services\r\n<\/code><\/pre>\n<p>Make it executable:<\/p>\n<pre><code class=\"language-bash\">chmod +x deploy.sh\r\n<\/code><\/pre>\n<\/li>\n<li><strong>Configure a Listener:<\/strong><br \/>\nUse a tool like a simple PHP script or a continuous integration service to trigger the <code>deploy.sh<\/code> script when a webhook is received.<\/li>\n<\/ul>\n<hr \/>\n<h2>4. Configure Your Web Server<\/h2>\n<ul>\n<li><strong>Set Up Virtual Hosts\/Server Blocks:<\/strong><br \/>\nEnsure your web server (Apache or Nginx) is configured to serve files from <code>\/var\/www\/yourwebsite<\/code>.<br \/>\nFor Apache, you might create a virtual host file:<\/p>\n<pre><code class=\"language-apache\">&lt;VirtualHost *:80&gt;\r\n    ServerName yourdomain.com\r\n    DocumentRoot \/var\/www\/yourwebsite\r\n\r\n    &lt;Directory \/var\/www\/yourwebsite&gt;\r\n        AllowOverride All\r\n        Require all granted\r\n    &lt;\/Directory&gt;\r\n\r\n    ErrorLog ${APACHE_LOG_DIR}\/yourwebsite_error.log\r\n    CustomLog ${APACHE_LOG_DIR}\/yourwebsite_access.log combined\r\n&lt;\/VirtualHost&gt;\r\n<\/code><\/pre>\n<p>Enable the site and restart your web server.<\/li>\n<\/ul>\n<hr \/>\n<h2>5. Secure Your Deployment<\/h2>\n<ul>\n<li><strong>Set Appropriate Permissions:<\/strong><br \/>\nEnsure that your web server user (e.g., <code>www-data<\/code> for Apache on Ubuntu) can read the files:<\/p>\n<pre><code class=\"language-bash\">sudo chown -R www-data:www-data \/var\/www\/yourwebsite\r\nsudo find \/var\/www\/yourwebsite -type d -exec chmod 755 {} \\;\r\nsudo find \/var\/www\/yourwebsite -type f -exec chmod 644 {} \\;\r\n<\/code><\/pre>\n<\/li>\n<li><strong>Protect Sensitive Files:<\/strong><br \/>\nMake sure your <code>.git<\/code> directory isn\u2019t accessible from the web. For Apache, you can add this to your <code>.htaccess<\/code>:<\/p>\n<pre><code class=\"language-apache\">RedirectMatch 404 \/\\.git\r\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<hr \/>\n<h2>Final Thoughts<\/h2>\n<p>Deploying your website from Git on your server not only streamlines your development process but also provides a robust method to manage code versions and updates. Whether you choose manual pulls or automate the process with webhooks, this approach keeps your deployment efficient and organized.<\/p>\n<p>Ready to deploy your website? Set up your server, clone your repository, and configure your web server to enjoy a seamless, Git-driven deployment process!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Deploy a Website from Git on Your Server Deploying your website directly from a Git repository can streamline updates and version control. Follow these steps to deploy your website on your server: 1. Prepare Your Server Install Git: Ensure Git is installed on your server. On Ubuntu\/Debian: sudo apt update &amp;&amp; sudo apt [&hellip;]<\/p>\n","protected":false},"author":1772,"featured_media":16894,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"tdm_status":"","tdm_grid_status":"","footnotes":""},"categories":[163],"tags":[],"class_list":{"0":"post-16893","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-hosting"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/posts\/16893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/users\/1772"}],"replies":[{"embeddable":true,"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/comments?post=16893"}],"version-history":[{"count":1,"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/posts\/16893\/revisions"}],"predecessor-version":[{"id":16895,"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/posts\/16893\/revisions\/16895"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/media\/16894"}],"wp:attachment":[{"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/media?parent=16893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/categories?post=16893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tremhost.com\/blog\/wp-json\/wp\/v2\/tags?post=16893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}