DEV Community

Cover image for Simple and Functional PHP Environment Setup with Windows
Rafael Rotiroti
Rafael Rotiroti

Posted on

Simple and Functional PHP Environment Setup with Windows

PHP was my first programming language that i worked, but in the past 8 years i've been working with another technologies such as Python, Java, Javascript. I never forgot PHP because this language helped me bills paid.

In the past, most of the time i used XAMPP, MAMP or any {x}AMP stack to setup my environment... so after i worked with many other languages and learned how to setup many kind of environments i thought how do that in PHP in the right way (at least for me).

So let's get started.

My proposal is use PHP, Composer and MySQL as database.

Lets go to PHP website and download any top version as Non Thread Safe zip.

Extract to your root C:/ in path php so everything should be inside C:/php folder.

Now download Composer, run your Composer-Setup.exe and probably he will identify your PHP in your Root C:/ folder.

Finishing Composer Setup and Running Laravel with Docker Compose

After installing Composer, open your terminal and verify the installation by running:

composer --version

If you see the version number, Composer is ready. Cause not, try do restart your Windows.

Now let's set up a Laravel project

Open your terminal and run the following command to create a new Laravel application:

composer create-project --prefer-dist laravel/laravel my-laravel-app

Navigate to the project folder:

cd my-laravel-app

Install Laravel dependencies:
composer install or composer i

Copy the .env.example to .env:

cp .env.example .env

Generate the application key:

php artisan key:generate

We should need a Database.. lets configure an Docker Compose for MySQL and phpMyAdmin for managing it

Inside your Laravel root directory, create a docker-compose.yml file and paste the following configuration:

version: '3.8'
services:
  db:
    image: mysql/mysql-server:latest
    container_name: mysql-db-container
    restart: always
    environment:
      - MYSQL_DATABASE=database
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - '3306:3306'
    volumes:
      - mysql-volume:/var/lib/mysql
    command: --sql_require_primary_key=0
    networks:
      - mysql-phpmyadmin

  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin
    restart: always
    ports:
      - "8090:80"
    environment:
      - PMA_HOST=db
      - MYSQL_USER=admin
      - MYSQL_ROOT_PASSWORD=password
    networks:
      - mysql-phpmyadmin

networks:
  mysql-phpmyadmin:

volumes:
  mysql-volume:
    driver: local
Enter fullscreen mode Exit fullscreen mode

Update Laravel's .env to connect to MySQL

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=admin
DB_PASSWORD=password
Enter fullscreen mode Exit fullscreen mode

Starting Docker Containers

Run the following command to start MySQL and phpMyAdmin:

docker compose up -d

Access phpMyAdmin at http://localhost:8090. You can use the username and password given at docker-compose.yml

Now, you can start Laravel's local development server by running:

php artisan serve

Access your Laravel application at http://127.0.0.1:8000.

And that’s it! You've successfully set up a PHP environment with an Laravel project with MySQL using Docker Compose. Happy coding!

Top comments (4)

Collapse
 
xwero profile image
david duymelinck

Why the choice for PHP on Windows and MySql in Docker?

Collapse
 
rotirotirafa profile image
Rafael Rotiroti

I think is more about nostalgic that i felt.. when i started to programming it was my first environment.. PHP + Mysql at my Windows SO..

This is post is not about recommendation

Collapse
 
xwero profile image
david duymelinck

It has been years that I used phpMyAdmin. And the last times were when a server didn't have ssh to connect with it. But I can't get that ugly blue interface out of my head.

Collapse
 
tkouleris profile image
Thodoris Kouleris

Adding docker just for mysql is just another pain that distance you from actual writing code. I prefer my database to be on my machine.

On the other hand, if you have a mysql docker ready and working (able to connect etc) than is a fine solution to keep your computer clean from db servers running on the background.