diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a068a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +srcs/.env diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b3d036e --- /dev/null +++ b/Makefile @@ -0,0 +1,23 @@ +NAME = inception + +all: prune reload + +domain: + echo "127.0.0.1 narnaud.42.fr" >> /etc/hosts + +start: + BUILDKIT_PROGRESS=plain docker compose -f srcs/docker-compose.yml up --build + +stop: + docker compose -f srcs/docker-compose.yml down + +prune: stop + docker system prune -f + +fclean: prune + rm -rf /home/narnaud/data + +re: fclean start + + +.PHONY: domain start stop fclean prune all diff --git a/README.md b/README.md index 83acea7..e16b307 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,43 @@ # Inception -Projet 42: -Sujet: Conteneuriser un serveur wordpress à l'aide de docker, alpine/debian, nginx et mariadb \ No newline at end of file +Projet 42: +Sujet: Conteneuriser un serveur web qui hebergera wordpress localement à l'aide de docker, alpine/debian, nginx et mariadb + + +## Todo: +- use variables folders name for volumes and domain +- add a bonus +- add a ftp server + +## Instructions: + +### Setup it: +Create a `.env` file in `srcs` folder whith this content and fullfill empty variables. +``` +MYSQL_HOST=mariadb +MYSQL_ROOT_PWD= + +WP_DATABASE_NAME=wordpress +WP_DATABASE_USR=maria +WP_DATABASE_PWD= + +WP_URL=localhost/wordpress + +WP_TITLE=Inception +WP_ADMIN_USR=admin +WP_ADMIN_PWD= +WP_ADMIN_EMAIL= +WP_USR= +WP_PWD= +WP_EMAIL= +``` + +### Start it: +If you want to use a custom domain as local host, modify the Makefile HOST variable and `make domain`. +To custom static page content, just edit `srcs/bonus/hexo/*_conf.yml` files with what you what to use before build. +To build and start, `make` into git root. + +### Use it: +For acces to homepage, visit `localhost` or your domain url (`DOMAIN_URL`) from your browser. +For acces to wordpress, click on blog in homepage or acces `DOMAIN_URL/wordpress`. +For acces to adminer, acces `DOMAIN_URL/wordpress/adminer.php` diff --git a/srcs/bonus/hexo/Dockerfile b/srcs/bonus/hexo/Dockerfile new file mode 100644 index 0000000..605ea28 --- /dev/null +++ b/srcs/bonus/hexo/Dockerfile @@ -0,0 +1,12 @@ +FROM alpine:3.15 +RUN apk update && apk upgrade && apk add --no-cache \ + wget git npm + +RUN adduser -S nginx && addgroup -S nginx +COPY academia_config.yml /tmp/academia_config.yml +COPY hexo_config.yml /tmp/hexo_config.yml + +WORKDIR /var/www/html + +COPY init.sh /tmp/init.sh +ENTRYPOINT [ "sh", "/tmp/init.sh" ] diff --git a/srcs/bonus/hexo/academia_config.yml b/srcs/bonus/hexo/academia_config.yml new file mode 100644 index 0000000..8c6b4c8 --- /dev/null +++ b/srcs/bonus/hexo/academia_config.yml @@ -0,0 +1,69 @@ +favicon: /img/favicon.png +# Change to your profile picture +avatar: /img/avatar.jpg +# if author sets in hexo_config, this string doesn't work +author: 'Nicolas Arnaud' +author_bio: "I'm a 28 y.o developer still learning at 42Nice school." + +since: 2022 + +# Top Menu 顶部菜单 +# #title 为页内跳转,单页模式 +# 可以选择外链或其他页面 +menu: + Publications: /#Publications + About: / + Blog: https://localhost/wordpress + +# flink picture will load if avator link calls on error +loading_bg: + flink: /img/profile.png + +# Social Links; 社交链接,不需要的链接直接注释掉 +# 可以调整显示顺序 +# 图标调用 font-awesome 4.7 +social: + enable: true +social_links: + fab fa-twitter: / + fab fa-facebook-square: / + fab fa-github: / + fab fa-stack-overflow: / + fab fa-linkedin: / + fab fa-weibo: / + fab fa-weixin: / + fab fa-qq: / + fas fa-envelope: / #E-mail + fas fa-rss: / #rss + +# Additional social_links,额外添加自定义链接,可显示链接名称 +# name: url || icon_name +e_social: + enable: true +e_social_links: + Google Scholar: / || fas fa-graduation-cap + ORCID: / || fab fa-orcid + +# CV download link +cv_dl: + enable: true + dir: /attaches/CV.pdf + description: My Detail CV. + +# Box-shadow +# display box-shadow on page or not +box_shadow: + enable: false + +# CDN +# 根据需要自行添加js/css +# 自定义css可以直接写在user.styl中 +CDN_USE: + css: + - /css/index.css +# - https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css + - https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13.0/css/all.min.css # fontawesome 5 + js: + - https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js #/js/third-party/jquery.min.js + - https://cdn.jsdelivr.net/npm/jquery-pjax@latest/jquery.pjax.min.js + - /js/main.js diff --git a/srcs/bonus/hexo/hexo_config.yml b/srcs/bonus/hexo/hexo_config.yml new file mode 100644 index 0000000..c9a298e --- /dev/null +++ b/srcs/bonus/hexo/hexo_config.yml @@ -0,0 +1,105 @@ +# Hexo Configuration +## Docs: https://hexo.io/docs/configuration.html +## Source: https://github.com/hexojs/hexo/ + +# Site +title: N.Arnaud's homepage +subtitle: '' +description: '' +keywords: +author: Nicolas Arnaud +language: en +timezone: 'Europe/Paris' + +# URL +## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project' +url: https://localhost +permalink: :year/:month/:day/:title/ +permalink_defaults: +pretty_urls: + trailing_index: true # Set to false to remove trailing 'index.html' from permalinks + trailing_html: true # Set to false to remove trailing '.html' from permalinks + +# Directory +source_dir: source +public_dir: public +tag_dir: tags +archive_dir: archives +category_dir: categories +code_dir: downloads/code +i18n_dir: :lang +skip_render: + +# Writing +new_post_name: :title.md # File name of new posts +default_layout: post +titlecase: false # Transform title into titlecase +external_link: + enable: true # Open external links in new tab + field: site # Apply to the whole site + exclude: '' +filename_case: 0 +render_drafts: false +post_asset_folder: false +relative_link: false +future: true +highlight: + enable: true + line_number: true + auto_detect: false + tab_replace: '' + wrap: true + hljs: false +prismjs: + enable: false + preprocess: true + line_number: true + tab_replace: '' + +# Home page setting +# path: Root path for your blogs index page. (default = '') +# per_page: Posts displayed per page. (0 = disable pagination) +# order_by: Posts order. (Order by date descending by default) +index_generator: + path: '' + per_page: 10 + order_by: -date + +# Category & Tag +default_category: uncategorized +category_map: +tag_map: + +# Metadata elements +## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta +meta_generator: true + +# Date / Time format +## Hexo uses Moment.js to parse and display date +## You can customize the date format as defined in +## http://momentjs.com/docs/#/displaying/format/ +date_format: YYYY-MM-DD +time_format: HH:mm:ss +## updated_option supports 'mtime', 'date', 'empty' +updated_option: 'mtime' + +# Pagination +## Set per_page to 0 to disable pagination +per_page: 10 +pagination_dir: page + +# Include / Exclude file(s) +## include:/exclude: options only apply to the 'source/' folder +include: +exclude: +ignore: + +# Extensions +## Plugins: https://hexo.io/plugins/ +## Themes: https://hexo.io/themes/ +theme: Academia + +# Deployment +## Docs: https://hexo.io/docs/one-command-deployment +deploy: + type: '' diff --git a/srcs/bonus/hexo/init.sh b/srcs/bonus/hexo/init.sh new file mode 100644 index 0000000..90eb559 --- /dev/null +++ b/srcs/bonus/hexo/init.sh @@ -0,0 +1,13 @@ +#!/bin/sh +if [ ! -f "/var/www/html/index.html" ]; then + npm install -g hexo-cli + hexo init && npm install + git clone https://github.com/PhosphorW/hexo-theme-academia.git themes/Academia + npm install hexo-renderer-pug hexo-renderer-stylus --save + sed -i "/theme: .*/c\theme: Academia" _config.yml + + cp /tmp/hexo_config.yml _config.yml + cp /tmp/academia_config.yml themes/Academia/_config.yml +fi + +hexo generate diff --git a/srcs/bonus/redis/Dockerfile b/srcs/bonus/redis/Dockerfile new file mode 100644 index 0000000..59ed143 --- /dev/null +++ b/srcs/bonus/redis/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine:3.15 + +RUN apk update && apk upgrade && apk add --no-cache redis + +CMD ["redis-server", "--protected-mode", "no"] diff --git a/srcs/docker-compose.yml b/srcs/docker-compose.yml new file mode 100644 index 0000000..a1cd75b --- /dev/null +++ b/srcs/docker-compose.yml @@ -0,0 +1,67 @@ +version: '3.8' + +networks: + local: + +services: + mariadb: + build: mariadb/ + container_name: mariadb + ports: + - 3306:3306 + volumes: + - "/home/narnaud/data/mariadb:/var/lib/mysql" + networks: + - local + restart: always + env_file: .env + + redis: + build: bonus/redis + container_name: redis + ports: + - 6379:6379 + networks: + - local + restart: always + + hexo: + build: bonus/hexo/ + container_name: hexo + volumes: + - '/home/narnaud/data/www:/var/www' + networks: + - local + env_file: .env + + wordpress: + build: wordpress/ + container_name: wordpress + depends_on: + - mariadb + - redis + ports: + - 9000:9000 + - 4000:4000 + volumes: + - '/home/narnaud/data/www:/var/www' + networks: + - local + restart: always + env_file: .env + + nginx: + build: nginx/ + container_name: nginx + depends_on: + - wordpress + - hexo + ports: + - 80:80 + - 443:443 + volumes: + - '/home/narnaud/data/www:/var/www' + - '/home/narnaud/data/logs:/var/log/nginx' + networks: + - local + restart: always diff --git a/srcs/mariadb/Dockerfile b/srcs/mariadb/Dockerfile new file mode 100644 index 0000000..fe5b244 --- /dev/null +++ b/srcs/mariadb/Dockerfile @@ -0,0 +1,8 @@ +FROM alpine:3.15 + +RUN apk update && apk upgrade && apk add --no-cache \ + mariadb mariadb-client + +COPY init.sh /tmp/init.sh + +ENTRYPOINT ["sh", "/tmp/init.sh"] diff --git a/srcs/mariadb/init.sh b/srcs/mariadb/init.sh new file mode 100644 index 0000000..4252a42 --- /dev/null +++ b/srcs/mariadb/init.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +if [ ! -d "/run/mysqld" ]; then + mkdir -p /run/mysqld + chown -R mysql:mysql /run/mysqld +fi + +if [ ! -d "/var/lib/mysql/mysql" ]; then + + chown -R mysql:mysql /var/lib/mysql + + mysql_install_db --basedir=/usr --datadir=/var/lib/mysql --user=mysql --rpm > /dev/null + + tfile=`mktemp` + if [ ! -f "$tfile" ]; then + return 1 + fi + + cat << EOF > $tfile +USE mysql; +FLUSH PRIVILEGES; + +DELETE FROM mysql.user WHERE User=''; +DROP DATABASE test; +DELETE FROM mysql.db WHERE Db='test'; +DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1'); + +ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PWD'; +CREATE DATABASE $WP_DATABASE_NAME CHARACTER SET utf8 COLLATE utf8_general_ci; +CREATE USER '$WP_DATABASE_USR'@'%' IDENTIFIED by '$WP_DATABASE_PWD'; +GRANT ALL PRIVILEGES ON $WP_DATABASE_NAME.* TO '$WP_DATABASE_USR'@'%'; + +FLUSH PRIVILEGES; +EOF + /usr/bin/mysqld --user=mysql --bootstrap < $tfile + rm -f $tfile +fi +sed -i "s|skip-networking|# skip-networking|g" /etc/my.cnf.d/mariadb-server.cnf +sed -i "s|.*bind-address\s*=.*|bind-address=0.0.0.0|g" /etc/my.cnf.d/mariadb-server.cnf + +exec /usr/bin/mysqld --user=mysql --console diff --git a/srcs/nginx/Dockerfile b/srcs/nginx/Dockerfile new file mode 100644 index 0000000..74fe3d2 --- /dev/null +++ b/srcs/nginx/Dockerfile @@ -0,0 +1,15 @@ +FROM alpine:3.15 + +RUN apk update && apk upgrade && apk add --no-cache \ + nginx \ + openssl + +RUN mkdir /etc/nginx/ssl +RUN openssl req -newkey rsa:4096 -x509 -sha256 -days 365 -nodes \ + -out /etc/nginx/ssl/inception.pem -keyout /etc/nginx/ssl/inception.key \ + -subj "/C=FR/ST=Nice/L=Nice/O=42/OU=student/CN=inception_narnaud/" + +RUN mkdir -p /run/nginx +COPY nginx.conf /etc/nginx/http.d/default.conf + +ENTRYPOINT ["nginx", "-g", "daemon off;"] diff --git a/srcs/nginx/nginx.conf b/srcs/nginx/nginx.conf new file mode 100644 index 0000000..4fa7d77 --- /dev/null +++ b/srcs/nginx/nginx.conf @@ -0,0 +1,38 @@ +server { + listen 80; + listen [::]:80; + server_name localhost; + + return 301 https://$host$request_uri; + +} + +server { + listen 443 ssl; + listen [::]:443 ssl; + server_name localhost; + + ssl_certificate /etc/nginx/ssl/inception.pem; + ssl_certificate_key /etc/nginx/ssl/inception.key; + ssl_protocols TLSv1.2 TLSv1.3; + + root /var/www/html/public; + index index.php index.html; + + location / { + try_files $uri $uri/ =404; + } + + location /wordpress { + root /var/www; + try_files $uri $uri/ /wordpress/index.php?$args; + location ~ /wordpress/(.+\.php)$ { + include fastcgi_params; + fastcgi_pass wordpress:9000; + fastcgi_index index.php; + fastcgi_intercept_errors on; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param HTTPS on; + } + } +} diff --git a/srcs/wordpress/Dockerfile b/srcs/wordpress/Dockerfile new file mode 100644 index 0000000..fcfb88e --- /dev/null +++ b/srcs/wordpress/Dockerfile @@ -0,0 +1,25 @@ +FROM alpine:3.15 + +RUN apk update && apk upgrade && apk add --no-cache \ + wget tar mariadb-client php8 php8-fpm \ + php8-bcmath php8-bz2 php8-calendar php8-ctype php8-curl php8-dom php8-exif php8-fileinfo \ + php8-gd php8-gmp php8-iconv php8-pecl-imagick php8-imap php8-intl php8-json php8-mbstring \ + php8-pecl-mcrypt php8-pecl-memcache php8-mysqli php8-mysqlnd php8-openssl php8-pcntl php8-pdo \ + php8-pdo_mysql php8-pdo_pgsql php8-pdo_sqlite php8-pgsql php8-phar php8-posix php8-session \ + php8-shmop php8-simplexml php8-soap php8-sockets php8-sodium php8-sqlite3 php8-sysvsem \ + php8-sysvshm php8-tokenizer php8-xml php8-xmlreader php8-xmlwriter php8-xsl php8-zip php8-zlib + +RUN adduser -S nginx && addgroup -S nginx +COPY php-fpm.conf /etc/php8/php-fpm.conf +COPY www.conf /etc/php8/php-fpm.d/www.conf + +RUN wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar +RUN chmod +x wp-cli.phar +RUN cp wp-cli.phar /usr/bin/wp +RUN ln -s /usr/bin/php8 /usr/bin/php + +WORKDIR /var/www/wordpress/ + + +COPY init.sh /tmp/init.sh +ENTRYPOINT [ "sh", "/tmp/init.sh" ] diff --git a/srcs/wordpress/init.sh b/srcs/wordpress/init.sh new file mode 100644 index 0000000..e27bfd4 --- /dev/null +++ b/srcs/wordpress/init.sh @@ -0,0 +1,23 @@ +#!/bin/sh +while ! mariadb -h$MYSQL_HOST -u$WP_DATABASE_USR -p$WP_DATABASE_PWD $WP_DATABASE_NAME &>/dev/null; do + sleep 5 +done + +if [ ! -f "/var/www/wordpress/index.php" ]; then + wp core download --allow-root + wp config create --dbname=$WP_DATABASE_NAME --dbuser=$WP_DATABASE_USR --dbpass=$WP_DATABASE_PWD --dbhost=$MYSQL_HOST --dbcharset="utf8" --dbcollate="utf8_general_ci" --allow-root + wp core install --url=$WP_URL --title=$WP_TITLE --admin_user=$WP_ADMIN_USR --admin_password=$WP_ADMIN_PWD --admin_email=$WP_ADMIN_EMAIL --skip-email --allow-root + wp user create $WP_USR $WP_EMAIL --role=author --user_pass=$WP_PWD --allow-root + + sed -i "90i define('WP_REDIS_HOST', 'redis');" wp-config.php + sed -i "91i define('WP_REDIS_PORT', 6379);" wp-config.php + sed -i "92i define('WP_REDIS_TIMEOUT', 1);" wp-config.php + sed -i "93i define('WP_REDIS_READ_TIMEOUT', 1);" wp-config.php + sed -i "94i define('WP_REDIS_DATABASE', 0);" wp-config.php + + wp plugin install redis-cache --activate --allow-root + wp plugin update --all --allow-root + wget -O /var/www/wordpress/adminer.php https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1-mysql-en.php +fi +wp redis enable --allow-root +/usr/sbin/php-fpm8 -F -R diff --git a/srcs/wordpress/php-fpm.conf b/srcs/wordpress/php-fpm.conf new file mode 100644 index 0000000..fc4e869 --- /dev/null +++ b/srcs/wordpress/php-fpm.conf @@ -0,0 +1,10 @@ +[global] +pid = run/php-fpm8.pid + +emergency_restart_threshold = 10 + +emergency_restart_interval = 10m + +daemonize = yes + +include=/etc/php8/php-fpm.d/*.conf \ No newline at end of file diff --git a/srcs/wordpress/www.conf b/srcs/wordpress/www.conf new file mode 100644 index 0000000..5cfac25 --- /dev/null +++ b/srcs/wordpress/www.conf @@ -0,0 +1,10 @@ +[www] +user = nginx +group = nginx +listen = 9000 + +pm = dynamic +pm.max_children = 5 +pm.start_servers = 2 +pm.min_spare_servers = 1 +pm.max_spare_servers = 3 \ No newline at end of file