Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.

Commit 365ceda

Browse files
committed
Initial commit
1 parent f34a8ec commit 365ceda

10 files changed

Lines changed: 313 additions & 0 deletions

File tree

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MYSQL_HOST=mysql
2+
MYSQL_DATABASE=
3+
MYSQL_USER=
4+
MYSQL_PASSWORD=
5+
MYSQL_ROOT_PASSWORD=

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# PhpStorm
2+
.idea/
3+
!.idea/codeStyles/
4+
!.idea/inspectionProfiles/
5+
6+
# Docker
7+
.env
8+
9+
# OS
10+
.DS_Store
11+
Thumbs.db
12+
13+
# Dependencies
14+
/vendor/

.idea/php.xml

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# WE4X-SI40 Project
2+
Group n°10.
3+
4+
## Getting Started
5+
The following tutorial applies on Windows 10 or later.
6+
7+
### Requirements
8+
- WSL 2
9+
- Docker Desktop
10+
- PhpStorm
11+
- Git
12+
13+
#### Install WSL
14+
1. Make sure hardware virtualization support is enabled in your BIOS
15+
2. Run the following command in PowerShell as Administrator `wsl --install` and restart your computer.
16+
Please make sure the following Windows features are being installed:
17+
- Virtual Machine Platform
18+
- Windows Subsystem for Linux (WSL)
19+
3. Install a Linux distro using the following command `wsl --install [Distro]` where `[Distro]`
20+
is the name. To view all distributions available for download, enter `wsl --list --online`
21+
4. Configure the Linux distro you chose by supplying a UNIX login and update the system
22+
23+
#### Install Docker Desktop
24+
1. Download the Docker Desktop installer either from the official [Docker website](https://www.docker.com/)
25+
or using winget (Windows Package Manager) in PowerShell `winget install Docker.DockerDesktop`.
26+
2. On the setup screen, please ensure you select `Use WSL 2` instead of `Hyper-V`
27+
3. When starting Docker for the first time, accept the subscription service agreement.
28+
You do not need to create an account, working locally is enough
29+
4. Finally Docker will initialize. This may take a few minutes on first launch
30+
31+
#### Install PhpStorm
32+
1. Go on the official [Jetbrains PhpStorm website](https://www.jetbrains.com/fr-fr/phpstorm/) and
33+
download the installer
34+
2. Proceed with installing PhpStorm then log into your account to activate your license
35+
36+
#### Install Git
37+
Trivial
38+
39+
### Project Setup
40+
1. Clone the repo: `git clone https://github.com/TheRefraction/WE4_Project.git`
41+
and open it in PhpStorm
42+
2. Copy the env file example `.env.example` to `.env` and fill in your values.
43+
This file should **NEVER** be committed to GitHub
44+
3. Start containers by running the following command from the project folder
45+
`docker compose up -d`. This may take a while the first time
46+
4. Open http://localhost:8080 to view the website
47+
48+
### PhpStorm Setup
49+
#### Set the PHP Interpreter
50+
1. Go to `Settings > PHP`
51+
2. Next to `CLI Interpreter`, click `...`
52+
3. Click `+ > From Docker, ...`
53+
4. Select `Docker Compose`
54+
5. Set:
55+
- Server: Docker
56+
- Configuration files: `./docker-compose.yml`
57+
- Service: `php`
58+
6. Apply and confirm
59+
60+
#### Database Link Configuration
61+
1. On the right panel, click on the `Database` icon
62+
2. Click on the `+` icon and select `MySQL` data source
63+
3. Set host to `localhost`, port to `3306`, and provide login information
64+
as well as the database name
65+
4. Install drivers if prompted
66+
5. Test the connection and save
67+
68+
You may now view the database using the right panel.
69+
70+
## Useful Notes
71+
If you have configured everything correctly, you should have `Connected to MySQL successfully!`
72+
appear on the `index.php` page. You may also check with the `info.php` page.
73+
74+
### Modifying the `.env` file
75+
- You may define a user login and a root password. The latter should not be empty for security
76+
reasons
77+
- You may define a database that will be created and populated accordingly.
78+
79+
### Local URLs
80+
- Open http://localhost:8080 to access the current website. All modifications
81+
made to the files are reloaded when refreshing the page in your
82+
Internet browser
83+
- Open http://localhost:8081 to access PhpMyAdmin. You may access
84+
the dashboard by providing `root` as a username and the password you
85+
defined in your `.env` file.
86+
- Sometimes albeit rare, a computer may fail to resolve `localhost`. Should that happen,
87+
please replace any occurrence of `localhost` by `127.0.0.1`.
88+
89+
### Database Edition
90+
The database is automatically loaded by Docker using your `.env` file.
91+
Although the latter is not accessed remotely, this repo provides 2 files
92+
under the `db/init` directory. SQL files are put in the lexicographic order,
93+
because that's the order it's initialized.
94+
95+
If you want to add another database, there should be two files, one for the model
96+
and another for data to use. Thus, make sure you're using the same
97+
database name in both files.
98+
99+
#### DB Schema `01_schema.sql`
100+
This part defines a given database and describes how it is structured.
101+
This is called the **schema**.
102+
103+
#### DB Seed `02_seed.sql`
104+
This part adds data to the database defined before. This may be useful
105+
to share some data with source control. This is called the **seed**.
106+
107+
#### What about the rest?
108+
All information or data you inject with PhpMyAdmin or with PhpStorm is
109+
stored locally in the Docker container. It will not be shared across
110+
other team members.
111+
112+
#### Soft reset
113+
This will remove all data in the database without destroying its structure.
114+
115+
Via PhpMyAdmin:
116+
1. Login as `root` user
117+
2. Select the database from the left panel
118+
3. Click the SQL tab
119+
4. Copy-paste the contents of `db/init/02_seed.sql` and run it
120+
121+
Via PhpStorm
122+
1. Open the Database panel on the right
123+
2. Connect to the DB if not already done
124+
3. Right-click the database > New > Query Console
125+
4. Copy-paste the contents of `db/init/02_seed.sql` and run it
126+
127+
**Important!** If your seed uses `INSERT` without `TRUNCATE` first, you
128+
may get duplicate data. Please add `TRUNCATE TABLE [NAME];` after `USER [DB];` accordingly,
129+
where `[NAME]` and `[DB]` are respectively the names of table to truncate, and of
130+
the database.
131+
132+
#### Hard reset
133+
This will wipe all data and re-run each `.sql` file under the `db/init`
134+
directory.
135+
136+
Run the following commands:
137+
- `docker compose down -v`
138+
- `docker compose up -d`
139+
140+
### Docker Commands
141+
Those commands should be ran in the project directory.
142+
143+
- Start and rebuild containers: `docker compose up -d`
144+
- Stop containers: `docker compose down`
145+
- Stop containers and wipes all volumes: `docker compose down -v`.
146+
This command will also destroy all local database data!
147+
- Get containers info: `docker ps`

db/init/01_schema.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE DATABASE IF NOT EXISTS test_db;
2+
USE test_db;
3+
4+
CREATE TABLE users (
5+
id INT AUTO_INCREMENT PRIMARY KEY,
6+
name VARCHAR(100) NOT NULL,
7+
email VARCHAR(100) UNIQUE NOT NULL,
8+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
9+
);
10+
11+
CREATE TABLE products (
12+
id INT AUTO_INCREMENT PRIMARY KEY,
13+
name VARCHAR(100) NOT NULL,
14+
price DECIMAL(10,2) NOT NULL
15+
);

db/init/02_seed.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
USE test_db;
2+
3+
INSERT INTO users (name, email) VALUES
4+
('Alice', 'alice@example.com'),
5+
('Bob', 'bob@example.com');
6+
7+
INSERT INTO products (name, price) VALUES
8+
('Widget A', 9.99),
9+
('Widget B', 19.99);

docker-compose.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
services:
2+
mysql:
3+
image: mysql:8.0
4+
restart: always
5+
env_file:
6+
- .env
7+
ports:
8+
- "3306:3306"
9+
volumes:
10+
- mysql_data:/var/lib/mysql
11+
- ./db/init:/docker-entrypoint-initdb.d
12+
13+
php:
14+
build: ./php
15+
restart: always
16+
ports:
17+
- "8080:80"
18+
volumes:
19+
- ./src:/var/www/html
20+
env_file:
21+
- .env
22+
depends_on:
23+
- mysql
24+
25+
phpmyadmin:
26+
image: phpmyadmin/phpmyadmin
27+
restart: always
28+
ports:
29+
- "8081:80"
30+
environment:
31+
PMA_HOST: mysql
32+
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
33+
34+
volumes:
35+
mysql_data:

php/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM php:8.2-apache
2+
RUN docker-php-ext-install mysqli pdo pdo_mysql
3+
#RUN pecl install xdebug && docker-php-ext-enable xdebug
4+
#COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini

src/index.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
$conn = new mysqli(
3+
$_ENV['MYSQL_HOST'],
4+
$_ENV['MYSQL_USER'],
5+
$_ENV['MYSQL_PASSWORD'],
6+
$_ENV['MYSQL_DATABASE']
7+
);
8+
9+
if ($conn->connect_error) {
10+
die("Connection failed: " . $conn->connect_error);
11+
}
12+
echo "Connected to MySQL successfully!";
13+
?>

src/info.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php phpinfo();

0 commit comments

Comments
 (0)