├── srcs
├── switch_autoindex.sh
├── init_docker.sh
├── nginx_config
├── wp-config.php
└── config.inc.php
├── Dockerfile
└── README.md
/srcs/switch_autoindex.sh:
--------------------------------------------------------------------------------
1 | if grep "autoindex on" /etc/nginx/sites-enabled/nginx_config; then
2 | echo "turning off autoindex"
3 | sed -i 's/autoindex on/autoindex off/' /etc/nginx/sites-enabled/nginx_config
4 | fi
5 | nginx -s reload
6 |
--------------------------------------------------------------------------------
/srcs/init_docker.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | #Start the server, php and the database in the docker
4 | service nginx start
5 | service php7.3-fpm start
6 | service mysql start
7 |
8 | #Create a wordpress database and add a new user
9 | echo "CREATE DATABASE wordpress;" | mysql --user root
10 | echo "GRANT ALL ON wordpress.* TO 'user42'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION" | mysql --user root
11 |
12 | service nginx restart
13 | /bin/bash
14 |
--------------------------------------------------------------------------------
/srcs/nginx_config:
--------------------------------------------------------------------------------
1 | #First case: port 80
2 | server {
3 | listen 80;
4 | listen [::]:80;
5 | server_name localhost;
6 |
7 | return 301 https://$server_name$request_uri;
8 | }
9 | #Second case: port 443
10 | server {
11 | listen 443 ssl;
12 | listen [::]:443 ssl;
13 | server_name localhost;
14 |
15 | # SSL configuration
16 | ssl on;
17 | ssl_certificate /etc/ssl/localhost.pem;
18 | ssl_certificate_key /etc/ssl/localhost.key;
19 |
20 | root /var/www;
21 |
22 | # Add index.php to the list if you are using PHP
23 | autoindex on;
24 | index index.html index.htm index.nginx-debian.html index.php;
25 |
26 | location / {
27 | try_files $uri $uri/ =404;
28 | }
29 |
30 | location /wordpress {
31 | alias /var/www/wordpress;
32 | }
33 |
34 | location /phpmyadmin {
35 | alias /var/www/phpmyadmin;
36 | }
37 |
38 | # pass PHP scripts to FastCGI server
39 | location ~ \.php$ {
40 | include snippets/fastcgi-php.conf;
41 | fastcgi_pass unix:/run/php/php7.3-fpm.sock;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | #Tell Docker where to start
2 | FROM debian:buster
3 |
4 | #Install the needed tools and make sure they are updated
5 | RUN apt-get update && apt-get -y install \
6 | wget \
7 | nginx \
8 | mariadb-server \
9 | openssl \
10 | php7.3 php-mysql php-fpm php-pdo php-gd php-cli php-mbstring
11 |
12 | #Set nginx config
13 | COPY srcs/nginx_config /etc/nginx/sites-available
14 | RUN ln -s /etc/nginx/sites-available/nginx_config /etc/nginx/sites-enabled
15 | RUN rm -rf /etc/nginx/sites-enabled/default
16 | RUN rm -rf /etc/nginx/sites-available/default
17 |
18 | #Generate a SSL certificate
19 | RUN openssl req -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out /etc/ssl/localhost.pem -keyout /etc/ssl/localhost.key -subj "/C=FR/ST=Paris/L=Paris/O=42 School/OU=llescure/CN=localhost"
20 |
21 | #Set the directory where to download phpmyadmin and wordpress
22 | WORKDIR /var/www/
23 |
24 | #Download phpmyadmin
25 | RUN wget https://files.phpmyadmin.net/phpMyAdmin/4.9.2/phpMyAdmin-4.9.2-english.tar.gz
26 | RUN tar -xzvf phpMyAdmin-4.9.2-english.tar.gz
27 | RUN rm -rf phpMyAdmin-4.9.2-english.tar.gz
28 | RUN mv phpMyAdmin-4.9.2-english phpmyadmin
29 | COPY srcs/config.inc.php /var/www/phpmyadmin
30 | RUN chown -R www-data: /var/www/phpmyadmin
31 |
32 | #Download wordpress
33 | RUN wget https://wordpress.org/latest.tar.gz
34 | RUN tar -xzvf latest.tar.gz
35 | RUN rm -rf latest.tar.gz
36 | COPY srcs/wp-config.php /var/www/wordpress
37 | RUN chown -R www-data: /var/www/wordpress
38 |
39 | #Back to the main directory
40 | WORKDIR /.
41 |
42 | #Specify the port
43 | EXPOSE 80 443
44 |
45 | #Run my .sh with bash that interprets shell command
46 | COPY srcs/*.sh ./
47 | CMD bash init_docker.sh
48 |
--------------------------------------------------------------------------------
/srcs/wp-config.php:
--------------------------------------------------------------------------------
1 | -3TWRQxB9Z{64Xs?:t6');
52 | define('NONCE_KEY', 'hJYS|+Y=qlHFPf92e=2y-&*^h_g8v|BBJm;oP#WL|,$RqL8{;bm{51;Z@y*FOZ8t');
53 | define('AUTH_SALT', '2j=.)|FAT-kA;!`Np[`1w8?eWrP}_NuKSHN3+Kq>{ui},`-%ojJ_B)g(y8nEx.l-');
54 | define('SECURE_AUTH_SALT', 'WtbV9Gn+<_M~+[=_GvIc wK_uo(-Lo|EyjAr4kHiww:CYY4&-!8w:zzl69Be6c%4');
55 | define('LOGGED_IN_SALT', '=F`qb-Hq|Iu9+E#&|xS8g7}v>GvBy~QeK<$pPvqq:^dMKt<.q$5!u+f=>~;M{ ');
56 | define('NONCE_SALT', ';X|`!tpV LK[BL(Yszv5IDz&I#&d;+[p>i^+t]`a#u>+LUII{xVg?Rva#Wcz}[V5');
57 | /**#@-*/
58 |
59 | /**
60 | * WordPress Database Table prefix.
61 | *
62 | * You can have multiple installations in one database if you give each
63 | * a unique prefix. Only numbers, letters, and underscores please!
64 | */
65 | $table_prefix = 'wp_';
66 |
67 | /**
68 | * For developers: WordPress debugging mode.
69 | *
70 | * Change this to true to enable the display of notices during development.
71 | * It is strongly recommended that plugin and theme developers use WP_DEBUG
72 | * in their development environments.
73 | *
74 | * For information on other constants that can be used for debugging,
75 | * visit the documentation.
76 | *
77 | * @link https://wordpress.org/support/article/debugging-in-wordpress/
78 | */
79 | define( 'WP_DEBUG', false );
80 |
81 | /* That's all, stop editing! Happy publishing. */
82 |
83 | /** Absolute path to the WordPress directory. */
84 | if ( ! defined( 'ABSPATH' ) ) {
85 | define( 'ABSPATH', __DIR__ . '/' );
86 | }
87 |
88 | /** Sets up WordPress vars and included files. */
89 | require_once ABSPATH . 'wp-settings.php';
90 |
91 |
--------------------------------------------------------------------------------
/srcs/config.inc.php:
--------------------------------------------------------------------------------
1 | .
8 | */
9 |
10 | declare(strict_types=1);
11 |
12 | /**
13 | * This is needed for cookie based authentication to encrypt password in
14 | * cookie. Needs to be 32 chars long.
15 | */
16 | $cfg['blowfish_secret'] = 'uNM3;Js2lF:ryGFeg7]/}0i/dvG1RA//';
17 |
18 | /**
19 | * Servers configuration
20 | */
21 | $i = 0;
22 |
23 | /**
24 | * First server
25 | */
26 | $i++;
27 | /* Authentication type */
28 | $cfg['Servers'][$i]['auth_type'] = 'cookie';
29 | /* Server parameters */
30 | $cfg['Servers'][$i]['host'] = 'localhost';
31 | $cfg['Servers'][$i]['compress'] = false;
32 | $cfg['Servers'][$i]['AllowNoPassword'] = true;
33 |
34 | /**
35 | * phpMyAdmin configuration storage settings.
36 | */
37 |
38 | /* User used to manipulate with storage */
39 | // $cfg['Servers'][$i]['controlhost'] = '';
40 | // $cfg['Servers'][$i]['controlport'] = '';
41 | // $cfg['Servers'][$i]['controluser'] = 'pma';
42 | // $cfg['Servers'][$i]['controlpass'] = 'pmapass';
43 |
44 | /* Storage database and tables */
45 | // $cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
46 | // $cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
47 | // $cfg['Servers'][$i]['relation'] = 'pma__relation';
48 | // $cfg['Servers'][$i]['table_info'] = 'pma__table_info';
49 | // $cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
50 | // $cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
51 | // $cfg['Servers'][$i]['column_info'] = 'pma__column_info';
52 | // $cfg['Servers'][$i]['history'] = 'pma__history';
53 | // $cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
54 | // $cfg['Servers'][$i]['tracking'] = 'pma__tracking';
55 | // $cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
56 | // $cfg['Servers'][$i]['recent'] = 'pma__recent';
57 | // $cfg['Servers'][$i]['favorite'] = 'pma__favorite';
58 | // $cfg['Servers'][$i]['users'] = 'pma__users';
59 | // $cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
60 | // $cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
61 | // $cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches';
62 | // $cfg['Servers'][$i]['central_columns'] = 'pma__central_columns';
63 | // $cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings';
64 | // $cfg['Servers'][$i]['export_templates'] = 'pma__export_templates';
65 |
66 | /**
67 | * End of servers configuration
68 | */
69 |
70 | /**
71 | * Directories for saving/loading files from server
72 | */
73 | $cfg['UploadDir'] = '';
74 | $cfg['SaveDir'] = '';
75 |
76 | /**
77 | * Whether to display icons or text or both icons and text in table row
78 | * action segment. Value can be either of 'icons', 'text' or 'both'.
79 | * default = 'both'
80 | */
81 | //$cfg['RowActionType'] = 'icons';
82 |
83 | /**
84 | * Defines whether a user should be displayed a "show all (records)"
85 | * button in browse mode or not.
86 | * default = false
87 | */
88 | //$cfg['ShowAll'] = true;
89 |
90 | /**
91 | * Number of rows displayed when browsing a result set. If the result
92 | * set contains more rows, "Previous" and "Next".
93 | * Possible values: 25, 50, 100, 250, 500
94 | * default = 25
95 | */
96 | //$cfg['MaxRows'] = 50;
97 |
98 | /**
99 | * Disallow editing of binary fields
100 | * valid values are:
101 | * false allow editing
102 | * 'blob' allow editing except for BLOB fields
103 | * 'noblob' disallow editing except for BLOB fields
104 | * 'all' disallow editing
105 | * default = 'blob'
106 | */
107 | //$cfg['ProtectBinary'] = false;
108 |
109 | /**
110 | * Default language to use, if not browser-defined or user-defined
111 | * (you find all languages in the locale folder)
112 | * uncomment the desired line:
113 | * default = 'en'
114 | */
115 | //$cfg['DefaultLang'] = 'en';
116 | //$cfg['DefaultLang'] = 'de';
117 |
118 | /**
119 | * How many columns should be used for table display of a database?
120 | * (a value larger than 1 results in some information being hidden)
121 | * default = 1
122 | */
123 | //$cfg['PropertiesNumColumns'] = 2;
124 |
125 | /**
126 | * Set to true if you want DB-based query history.If false, this utilizes
127 | * JS-routines to display query history (lost by window close)
128 | *
129 | * This requires configuration storage enabled, see above.
130 | * default = false
131 | */
132 | //$cfg['QueryHistoryDB'] = true;
133 |
134 | /**
135 | * When using DB-based query history, how many entries should be kept?
136 | * default = 25
137 | */
138 | //$cfg['QueryHistoryMax'] = 100;
139 |
140 | /**
141 | * Whether or not to query the user before sending the error report to
142 | * the phpMyAdmin team when a JavaScript error occurs
143 | *
144 | * Available options
145 | * ('ask' | 'always' | 'never')
146 | * default = 'ask'
147 | */
148 | //$cfg['SendErrorReports'] = 'always';
149 |
150 | /**
151 | * You can find more configuration options in the documentation
152 | * in the doc/ folder or at .
153 | */
154 |
155 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 42_ft_server
2 |
3 | Discover Docker, Nginx, MySQL
4 |
5 | Useful command in shell:
6 |
7 | * Build a docker image from a Dockerfile
8 |
9 | docker build -t
10 |
11 | * Start an instance of a docker image
12 |
13 | docker run -it
14 |
15 | * Really important if you want to bind some ports on the container to your own computer, use -p option.
16 |
17 | docker run -it -p 80:80
18 |
19 | * See all images
20 |
21 | docker images
22 |
23 | * Remove images
24 |
25 | docker rmi
26 |
27 | * Delete all images
28 |
29 | docker rmi $(docker images -a -q)
30 |
31 | * Remove a container
32 |
33 | docker rm
34 |
35 | * See running containers
36 |
37 | docker ps
38 |
39 | * List all docker containers (running and stopped).
40 |
41 | docker ps -a
42 |
43 | * Delete all unused Docker images and cache and free space
44 |
45 | docker system prune
46 |
47 | ------------------------------------------
48 |
49 | ## Step 1: Make sure docker is installed and that you have the correct permissions
50 | If you get the following error:
51 | > Got permission denied while trying to connect to the Docker daemon socket.
52 |
53 | Use: `sudo chmod 666 /var/run/docker.sock`
54 |
55 | If you have downloaded 42 vm use the following command: `sudo systemctl stop ngnix`
56 | A nginx docker is already running when you download 42 VM
57 |
58 | ## Step 2: Specify where the docker should run
59 | The subject specifies that the container should run with Debian Buster.
60 | To do so we have to specifiy in the DockerFile the following command : `FROM debian:buster`
61 |
62 | ## Step 3 : Install the necessary package
63 | The -y flag stands for yes and will automatically accept the installation
64 |
65 | `RUN apt-get update && apt-get install -y
66 | wget \ #to be able to download phpmyadmin and wordpress
67 | nginx \ #for the server
68 | mariadb-server \ #to launch a sql database
69 | openssl \ #to generate a ssl certificate
70 | php7.3 php-mysql php-fpm php-pdo php-gd php-cli php-mbstring \ #to launch php`
71 |
72 | ## Step 4: Set nginx
73 | Copy the file default that you can find at etc/nginx/sites-available, in your repository srcs
74 |
75 | Edit it following the [official documentation](http://nginx.org/en/docs/beginners_guide.html)
76 |
77 | Copy the new file in srcs in you docker: `COPY srcs/[name of the nginx config file] /etc/nginx/sites-enabled`
78 |
79 | Create a symbolic link between /etc/nginx/sites-enabled and /etc/nginx/sites-available: `RUN ln -s /etc/nginx/sites-available/nginx_config /etc/nginx/sites-enabled`
80 |
81 | Remove the old files default : `RUN rm -rf /etc/nginx/sites-enabled/default` and `RUN rm -rf /etc/nginx/sites-enabled/default`
82 |
83 | ## Step 5: Set the ssl certificate
84 | Use openssl dowloaded precedently to generate a certificate
85 | `RUN openssl req -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out /etc/ssl/localhost.pem -keyout /etc/ssl/localhost.key -subj "/C=FR/ST=Paris/L=Paris/O=42 School/OU=llescure/CN=localhost"`
86 |
87 | Don't forget to update your nginx config file
88 |
89 | # SSL configuration
90 | ssl on;
91 | ssl_certificate /etc/ssl/localhost.pem;
92 | ssl_certificate_key /etc/ssl/localhost.key;
93 |
94 | ## Step 6: Download and set phpmyadmin
95 | Download a phpmyadmin version. I chose 4.9.2
96 | `RUN wget https://files.phpmyadmin.net/phpMyAdmin/4.9.2/phpMyAdmin-4.9.2-english.tar.gz
97 | RUN tar -xzvf phpMyAdmin-4.9.2-english.tar.gz
98 | RUN rm -rf phpMyAdmin-4.9.2-english.tar.gz
99 | RUN mv phpMyAdmin-4.9.2-english phpmyadmin`
100 |
101 | Edit the config file config.inc.php as done [here](https://forhjy.medium.com/42-ft-server-how-to-install-lemp-wordpress-on-debian-buster-by-using-dockerfile-2-4042adb2ab2c)
102 |
103 | Don't forget to generate a blobfish secret [here](https://phpsolved.com/phpmyadmin-blowfish-secret-generator/)
104 |
105 | Copy the edited files in /var/www/phpmyadmin: `COPY srcs/config.inc.php /var/www/phpmyadmin`
106 |
107 | Give the rights of the directory : `RUN chown -R www-data: /var/www/phpmyadmin`
108 |
109 | ## Step 7: Download and set wordpress
110 | Download the latest version of wordpress
111 | `RUN wget https://wordpress.org/latest.tar.gz
112 | RUN tar -xzvf latest.tar.gz
113 | RUN rm -rf latest.tar.gz`
114 |
115 | Edit the config file wp-config.php as done [here](https://forhjy.medium.com/42-ft-server-how-to-install-lemp-wordpress-on-debian-buster-by-using-dockerfile-2-4042adb2ab2c)
116 |
117 | Don't forget to generate the unique keys [here](https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service)
118 |
119 | Copy the edited files in /var/www/wordpress: `COPY srcs/wp-config.php /var/www/wordpress`
120 |
121 | Give the rights of the directory : `RUN chown -R www-data: /var/www/wordpress`
122 |
123 | ## Step 8: Run the services and create a mysql database
124 |
125 | Create a file.sh in srcs where you will ask the docker to run nginx service, php service and mysql service
126 | `service nginx start
127 | service php7.3-fpm start
128 | service mysql start`
129 |
130 | Use this [tuto] to understand mysql
131 |
132 | Don't forget to add this line at the beggining of your file: `#!/bin/bash` and this one at the end of your file.sh : `/bin/bash`
133 |
134 | It will tell the program to interpet the following code with bash.
135 |
136 | ## Step 9: Set a file.sh which can disable autoindex while the docker is running
137 | We will edit the nginx config file by setting autoindex to off. Remember we set it to on previously.
138 |
139 | `if grep "autoindex on" /etc/nginx/sites-enabled/nginx_config; then
140 | echo "turning off autoindex"
141 | sed -i 's/autoindex on/autoindex off/' /etc/nginx/sites-enabled/nginx_config
142 | fi
143 | nginx -s reload`
144 |
145 | Please note you can use either sites-enabled or sites-available since they are linked by a symbolic link.
146 |
147 | If you turn off the autoindex when you type http://localhost you should see a 403 error
148 |
149 | Please check the following docs on Docker:
150 | https://www.cloudbees.com/blog/docker-basics-linking-and-volumes/ to understand the basics
151 | https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ fo best practices
152 | https://docs.docker.com/engine/reference/builder/#expose for command line
153 |
--------------------------------------------------------------------------------