# HW06 LNMP安裝 ## 安裝Nginx - 首先,先安裝Nginx ```bash sudo apt install nginx ``` ![image](https://hackmd.io/_uploads/BJBDL0Q41l.png) - 接著,查看nginx運行狀態 ```bash systemctl status nginx ``` ![image](https://hackmd.io/_uploads/H1uYIAm4Jx.png) - 正在跑,太棒了 - 確認一下 ![image](https://hackmd.io/_uploads/rykRUCQ4ke.png) ## 安裝PHP - 先安裝PHP的PPA ```bash sudo add-apt-repository ppa:ondrej/php ``` ![image](https://hackmd.io/_uploads/HJ3gOC7Nkg.png) - 安裝PHP的直譯器 - 先看版本: 最新的是8.4 ![image](https://hackmd.io/_uploads/r1wIdAQEJx.png) - 安裝PHP ```bash sudo apt install php8.4 ``` ![image](https://hackmd.io/_uploads/S1rzYRQ4Jx.png) - 安裝PHP-fpm: FastCGI Process Manager ```bash sudo apt install php8.4-fpm ``` ![image](https://hackmd.io/_uploads/r1aOtCX41g.png) ## 設定PHP與nginx之間的參數 - 先用文字編輯器更改`sudo nano /etc/nginx/sites-available/default` (要有root權限才能改) - 改成這樣 ```perl ## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # https://www.nginx.com/resources/wiki/start/ # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ # https://wiki.debian.org/Nginx/DirectoryStructure # # In most cases, administrators will remove this file from sites-enabled/ and # leave it as reference inside of sites-available where it will continue to be # updated by the nginx packaging team. # # This file will automatically load configuration files provided by other # applications, such as Drupal or Wordpress. These applications will be made # available underneath a path with that package name, such as /drupal8. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # With php-fpm (or other unix sockets): fastcgi_pass unix:/run/php/php8.4-fpm.sock; # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } } # Virtual Host configuration for example.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # #server { # listen 80; # listen [::]:80; # # server_name example.com; # # root /var/www/example.com; # index index.html; # # location / { # try_files $uri $uri/ =404; # } #} ``` - 確定更改成功 ```bash sudo nginx -t ``` ![image](https://hackmd.io/_uploads/HyGGp07NJe.png) - 測試PHP是否成功連結到Nginx - 首先先寫`info.php`: 在`/var/www/html` ```php <?php phpinfo(); ?> ``` ![image](https://hackmd.io/_uploads/rJkYRA7EJg.png) - 接著restart nginx ```bash sudo systemctl restart nginx ``` - 打開瀏覽器測試 ![image](https://hackmd.io/_uploads/BJsgkkVEyx.png) - 看一下內容 - 首先是Server的設定 ![image](https://hackmd.io/_uploads/rJ1v1-4Ekx.png) - 接著是伺服器狀態 ![image](https://hackmd.io/_uploads/H1Id1b4Eyl.png) ## 安裝MariaDB - 先安裝相關套件 ```bash sudo apt install wget software-properties-common dirmngr ca-certificates apt-transport-https ``` ![image](https://hackmd.io/_uploads/H1zoJJ44yg.png) - 接著安裝MariaDB ```bash sudo apt install mariadb-server mariadb-client ``` ![image](https://hackmd.io/_uploads/B1v-gkE4ke.png) - 確定是不是可以正常使用 ```bash mariadb --version systemctl status mariadb ``` ![image](https://hackmd.io/_uploads/SJ4BeJEEkg.png) - 不放心用nmap炸一次我的主機 ```bash nmap -sT 127.0.0.1 -T5 ``` ![image](https://hackmd.io/_uploads/Sk0qbJ44Jl.png) - 可以看到DB是真的有安裝的 ## 測試MariaDB與PHP - 先去mysql改使用者 - 進mysql ```bash sudo mysql ``` - 輸入SQL命令 ```sql ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; ``` - 接著因為我們沒有安裝php與mysql連接的函數庫,因此要去安裝 ```bash sudo apt-get install php8.4-mysql ``` ![image](https://hackmd.io/_uploads/Hy0zYJ4Eyg.png) - 然後我的PHP就可以做連線測試了 ```php= <?php ini_set("display_errors", 'on'); $servername = "localhost"; $username = "root"; $password = "123456"; $conn = mysqli_connect($servername, $username, $password); if (!$conn){ die("<h1>"."connect error".mysqli_connect_error()."</h1>"); } echo "<h1>connect successful</h1>"; ?> ``` - 連上Server看看 ![image](https://hackmd.io/_uploads/rkIFY1NEkx.png)