How upload a PHP project to github

If you're new to PHP and want to upload your code to GitHub, I’m going to show you a simple and effective way to do that. This guide will also teach you how to organize your project using Composer and environment variables with a .env file.

Whether you're building a large web application or a small tool, organizing your PHP project with Composer and .env variables is essential for cleaner, more secure, and scalable development. In this guide, I’ll walk you through how I set up my E-Notice Board application and how you can do the same.

Prerequisites

Before you start, make sure you have the following:

  • PHP installed (php -v)

  • Composer installed globally (composer -v)

  • A code editor (e.g., VS Code)

  • Git and GitHub set up

  • XAMPP or any local server

🛠️ Steps

1. Install Composer

If Composer is not yet installed, download it from https://getcomposer.org/ and follow the installation instructions for your OS.

To check if it’s installed:

composer -v

2. Install vlucas/phpdotenv

Use Composer to install the phpdotenv package, which allows you to use environment variables in your PHP app.

composer require vlucas/phpdotenv

3. Create a .env File

In the root folder of your project, create a .env file and add your sensitive data:

DB_HOST=localhost

DB_NAME=your_database

DB_USER=root

DB_PASS=password

⚠️ Make sure you do not upload this file to GitHub.

4. Update Your Database Connection File

Update connection.php or wherever you connect to your database like this:

require_once DIR . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);

$dotenv->load();

$conn = new mysqli(

    $_ENV['DB_HOST'],

    $_ENV['DB_USER'],

    $_ENV['DB_PASS'],

    $_ENV['DB_NAME']

);

5. Create a .gitignore File

Create a .gitignore file in your project root and add:

### PHP project .gitignore file


# Ignore system files
*.tmp
*.bak
*.swp
*.swo
*.swn
*.log
*.sql
*.err
*.out


# Ignore IDE and editor files
.idea/
.vscode/
.phpstorm*
*.iml
*.ipr
*.iws


# Ignore PHPStorm files
/.phpstorm/
*.phpstorm.meta.php
*_php.cs.svn.base
*_svn.svn.base


# Ignore vendor files
/vendor/


# Ignore composer files
/composer.lock

# Ignore environment files
/.env

# Ignore uploaded files and media
/uploads/
/media/


# Ignore logs and cache
/logs/
/cache/

This prevents your vendor folder and .env file from being uploaded to GitHub.

6. Upload to GitHub

Now push your project to GitHub:

git init

git add .

git commit -m "Initial commit"

git branch -M main

git remote add origin https://github.com/yourusername/your-repo-name.git

git push -u origin main

🧠 Final Notes

  • Using .env keeps your sensitive data safe and lets you switch configurations easily.

  • Composer makes your PHP project manageable and scalable.

  • Keeping your .gitignore clean ensures only necessary files are uploaded to GitHub.

Conclusion

Uploading your PHP project to GitHub doesn’t have to be complicated. By using Composer and environment variables, you not only make your application more secure but also more maintainable and professional.

This setup allows you to separate sensitive credentials from your codebase, keep your GitHub repository clean, and follow best practices in modern PHP development.

Whether you’re working on a personal project or collaborating with a team, taking these extra steps shows you're thinking ahead — and that’s what makes a great developer.

Now go ahead and share your work with the world — the right way! 🚀

👍 Found this helpful?

Feel free to follow me on GitHub
or connect with me on Twitter/X / LinkedIn
Got questions or feedback? I’d love to hear from you!

Happy coding! 💻✨

0
Subscribe to my newsletter

Read articles from Yusuf Abdulaziz Sani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yusuf Abdulaziz Sani
Yusuf Abdulaziz Sani

I'm a full-stack software engineer with a passion for building software solutions and sharing my Knowledge. Figuring a lot of things out. These are my personal notes on my tech exploit. As I learn, I share.When I'm not providing web services, I love providing guidance and mentorship to newbies starting their careers in tech. Let's have a conversation.