Code Monkey home page Code Monkey logo

php-sandbox's Introduction

php sandbox

A lightweight php / MySQL sandbox for learning, testing and debugging code.

php sandbox screenshot

features

  • Alpine Linux, Apache, php-fpm and MariaDB (LAMP)
  • php 8.3, 8.2, 8.1, 8.0, 7.4 along with the most commonly used extensions
  • Just works with any domain name and https is configured out of the box
  • Support for multiple virtual hosts
  • php code cleanup using php cs fixer
  • php code static analysis using phpstan
  • profile php code with SPX profiler or Xdebug
  • Apache and php configuration files are exposed on the host for easy edit
  • all changes to configuration files are automatically applied inside the container (hot reload)
  • Xdebug is configured for step by step debugging and profiling in Visual Studio Code
  • javascript step by step debugging in Visual Studio Code

architecture

The setup consists of 2 Docker images with a combined size of approximately 110 MB.

  • web server Docker Image Size (latest semver)

    • Apache 2.4.59 with SSL
    • php-fpm 8.3.7
    • Xdebug 3.3.2 - debugger and profiler
    • SPX prolifer dev-master
    • composer 2.6.5
    • zsh 5.9
    • Alpine 3.19.1 with edge repositories
  • database server Docker Image Size (latest semver)

    • MariaDB 10.11.6
    • zsh 5.9
    • Alpine 3.19.1

install

You can either download the latest version or git clone the repository.

git clone --depth 1 https://github.com/8ctopus/php-sandbox.git

For php 8.2 or 8.1, select the image in docker-compose.yml. For older php version, you need to download an older version of php-sandbox and then choose the correct php version in docker-compose.yml as the architecture changed since.

start coding

Start Docker Desktop then:

cd php-sandbox

# start containers in detached mode on Windows in cmd
start /B docker-compose up

# start containers in detached mode on linux and mac in shell
docker-compose up &

Note: On Windows file changes notification to the container doesn't work with the WSL 2 engine, you need to use the Hyper-V engine. Uncheck Use WSL 2 based engine. What this means, is that files you update on Windows are not updated inside the container unless you use Hyper-V.

access sites

By default, there are 2 sites you can access from your browser

http(s)://localhost/
http(s)://(www.)test.com/

The source code is located inside the sites/*/html/public/ directories.

domain names

Setting a domain name is done by using virtual hosts. The virtual hosts configuration files are located in sites/config/vhosts/. By default, localhost and test.com are already defined as virtual hosts.

For your browser to resolve test.com, add this line to your system's host file. Editing the file requires administrator privileges.

On Windows: C:\Windows\System32\drivers\etc\hosts
Linux and Mac: /etc/hosts

127.0.0.1 test.com www.test.com

https

A self-signed https certificate is already configured for localhost and test.com.
To remove "Your connection is not private" nag screens, import the certificate authority file sites/config/ssl/certificate_authority.pem to your computer's Trusted Root Certification Authorities then restart your browser.

In Windows, open certmgr.msc > click Trusted Root Certification Authorities, then right click on that folder and select Import... under All Tasks.

On Linux and Mac: [fill blank]

For newly created domains, you will need to create the SSL certificate:

docker-exec -it web zsh
selfsign certificate /sites/domain/ssl domain.com,www.domain.com,api.domain.com /sites/config/ssl

Note: Importing the certificate authority creates a security risk since all certificates issued by this new authority are shown as perfectly valid in your browsers.

Xdebug debugger

This repository is configured to debug php code in Visual Studio Code. To start debugging, open the VSCode workspace then select Run > Start debugging then open the site in the browser.
The default config is to stop on entry which stops at the first line in the file. To only stop on breakpoints, set stopOnEntry to false in .vscode/launch.json.

For other IDEs, set the Xdebug debugging port to 9001.

To troubleshoot debugger issues, check the sites/localhost/logs/xdebug.log file.

If host.docker.internal does not resolve within the container, update the xdebug client host within docker/etc/php/conf.d/xdebug.ini to the docker host ip address.

xdebug.client_host = 192.168.65.2

code cleanup

PHP Coding Standards Fixer is a tool to automatically fix PHP coding standards issues.

cd sites/localhost/html
composer fix(-risky)

check code for issues

PHPStan is a PHP static analysis tool, it can discover bugs in your code without running it

cd sites/localhost/html
composer phpstan

code profiling

Code profiling comes in 2 variants: Xdebug and SPX.

Note: Disable Xdebug debugger xdebug.remote_enable for accurate measurements.

Xdebug

To start profiling, add the XDEBUG_PROFILE variable to the request as a GET, POST or COOKIE.

http://localhost/?XDEBUG_PROFILE

Profiles are stored in the log directory and can be analyzed with tools such as webgrind.

SPX

  • Access the SPX control panel
  • Check checkbox Whether to enable SPX profiler for your current browser session. No performance impact for other clients.
  • Run the script to profile
  • Refresh the SPX control panel tab and the report will be available at the bottom of the screen. Click it to show the report in a new tab.

database access

You can connect to the database using your favorite MySQL client (on Windows, HeidiSQL for example)

hostname: localhost / sandbox-db
user: root
password: 123
port: 3306

access container

# web container
docker exec -it sandbox zsh

# database container
docker exec -it sandbox-db zsh

more docker commands

# view logs
docker-compose logs -f

# stop containers
docker-compose stop

# delete containers
docker-compose down

# delete containers and volume (deletes database)
docker-compose down -v

## get shell access to container
docker exec -it sandbox zsh
docker exec -it sandbox-db zsh

extend the docker image

In this example, we add the php-curl extension.

docker-compose up --detach
docker exec -it sandbox zsh
apk add php-curl
exit

docker-compose stop
docker commit sandbox sandbox-curl:dev

To use this image, update the reference in docker-compose.yml.

Sublime Text

If you like Sublime Text, checkout the configuration I'm using.

credits

neatcss

php-sandbox's People

Contributors

8ctopus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sher1

php-sandbox's Issues

Each Request tooks a Baseload of 30ms

Hi there,

im a hobbyiest and my English is not the Best, sorry for that :)

Im writing a Rest-Api that tooks/return JSON-Data. For performance reasons, each Request is tested for Time its took.
Before i choose your Docker Image i used XAMPP for local testing. The Client testing my Restapi is Postmen.

Now i saw, that every Request tooks a minimum of 30ms for finishing, even, if the php script contains only a "exit()" call. I saw that before, only when xdebug is uesed in a special case. When the server has a medium idle-Time (a few minutes) the next Request tooks 2-4 Times longer than normal.

How i can prevent this ? Nice to hear from you. Thx.

Greets, Matthias

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.