Web Server
2023/5/29 Web組
Before we start
HTTP Request
GET http://www.csie.ntu.edu.tw/members/teacher.php?mclass=110
HTTP Response
Web Server
/var/www/html
www.csie.ntu.edu.tw
/var/www/html/nasa
www.csie.ntu.edu.tw/nasa
URL
File System
Client-Server model
Browser
Web Server
Backend Script / Framework
Database
Web Server Comparison
LAB
Linux VM
Your PC
(Client)
Web Server
Nginx
Dynamic Content PHP
Database
MariaDB
Set up a VM
Firewall Configuration
Install Nginx + MariaDB + php
$ sudo apt install nginx mariadb-server php8.1-fpm php-mysql
php-mysql 是 php 的 mysql 模組,需要這個才能連線、執行 SQL 查詢
Testing Nginx
$ curl localhost
What do you see?
Nginx Server Installation
Nginx Directory Structure
Nginx Config - nginx.conf
Configure Nginx
$ cd /etc/nginx
$ sudo cp sites-available/default sites-available/wordpress.conf
$ sudo unlink sites-enabled/default
$ sudo nano sites-available/wordpress
Configure Nginx
server {
listen 80 default_server;
root /var/www/wordpress;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}
Configure Nginx
$ sudo ln -s ../sites-available/wordpress sites-enabled/wordpress
$ sudo mkdir -p /var/www/wordpress
# 測試 nginx configuration 有沒有錯
$ sudo nginx -t
# 重新載入 nginx
$ sudo systemctl reload nginx
Configure Nginx - Testing Nginx with PHP
建立一個檔案測試 /var/www/wordpress/info.php,記得要刪掉(理由)
<?php
phpinfo();
?>
Configure MariaDB
基本上是認真讀
然後接受它的建議就好了
測試可以登入 mysql
$ sudo mysql
$ sudo mysql_secure_installation
Change the root password? [Y/n] n
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
Check your Apache
If you don’t see the page:
PHP Installation
You may now put `info.php` in /var/www/html/info.php and browse {client ip}/info.php to check if php is installed correctly
`info.php` content:
<?php
phpinfo();
MySQL Installation
=== Mysql Installation Process ===
Download and Configure WordPress
$ cd /tmp
$ wget https://wordpress.org/latest.tar.gz
$ tar xzvf latest.tar.gz
$ cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php
$ sudo cp -a /tmp/wordpress/. /var/www/wordpress
$ sudo chown -R www-data:www-data /var/www/wordpress
透過 Wordpress API 取得安全的 secret key,複製到 wp-config 中
$ wget -O - https://api.wordpress.org/secret-key/1.1/salt/
$ sudo nano /var/www/wordpress/wp-config.php
Configure WordPress - 設定 mariadb 帳號
$ sudo mysql
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY '{password}';
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';
Configure WordPress - 設定 mariadb 帳號
設定 DB_NAME, DB_USER, DB_PASSWORD。供 Wordpress 連線到 maria-db 使用。之後新增一行定義 FS_METHOD 為 direct。因為我們已經授予 Web 服務器寫入 /var/www/wordpress 的權限。編輯 /var/www/wordpress/wp-config.php 檔案
define( 'DB_NAME', 'wordpress' );
/** MySQL database username */
define( 'DB_USER', 'wordpressuser' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password' );
define( 'FS_METHOD', 'direct' );
Configure WordPress - Nginx 改設定
讓 Wordpress 可以顯示 404 頁面
server {
. . .
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
. . .
}
$ sudo nano /etc/nginx/sites-available/wordpress
Wordpress Installation
Demo (submit via COOL)
Thanks for your attention !
GL&HF :)