---
# System prepended metadata

title: Ubuntu Server LEMP 環境架設
tags: [MySQL, Ubuntu, Linux, Nginx, LEMP, PHP]

---

# Ubuntu Server LEMP 環境架設


1.下載 Ubuntu Live Server .iso 檔案：[點我下載](https://releases.ubuntu.com/22.04.4/ubuntu-22.04.4-live-server-amd64.iso)。
2.安裝 Ubuntu Live Server GUI 介面 `非必要，新手或不熟悉Linux者建議安裝`

    #sudo apt install -y ubuntu-desktop
    
3.安裝 Nginx Web 伺服器，並設定開機自動啟用。

    #sudo apt install nginx -y
    #sudo systemctl enable nginx

4.修改 www 資料夾權限

    #sudo chown -R www-data:www-data /var/www
    
5.下載並透過指令解壓縮安裝 VS Code ：[點我下載](https://code.visualstudio.com/)

    #sudo dpkg -i 檔名.deb
    
6.安裝 MySQL：安裝資料庫系統，必且進行基本配置。

    sudo apt install mysql-server -y  
    #-->> Pi的話使用 sudo apt install mariadb-server

    #sudo mysql_secure_installation
    #ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
    
7.安裝 PHP：安裝 PHP 以及必要的 PHP 擴展。

    sudo apt install php-fpm php-mysql php-imagick php-gd php-zip php-intl
    sudo chmod -R 775 /var/www
    
8.安裝 phpMyAdmin後，配置 Nginx.conf（localhost 才可進入資料庫後台）

    sudo apt-get install phpmyadmin
    
    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    events {
        worker_connections 768;
    }

    http {
	    client_max_body_size 0; # Nginx 必加入，會影響可傳送資料的大小，0 表示無限制

	    sendfile on;
	    tcp_nopush on;
	    types_hash_max_size 2048;

	    include /etc/nginx/mime.types;
	    default_type application/octet-stream;

	    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	    ssl_prefer_server_ciphers on;

	    access_log /var/log/nginx/access.log;
	    error_log /var/log/nginx/error.log;

	    gzip on;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;

	#localhost + phpmyadmin
	server{
		    listen 80;
		    server_name localhost; #DomainName

		    root /var/www/html; # Website Folder
		    index index.php index.html index.htm index.nginx-debian.html;

		    location / {
			    try_files $uri $uri/ /index.php?$args;
		    }

		    location ~ \.php$ {
			    include snippets/fastcgi-php.conf;
			    fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
			    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
			    include fastcgi_params;
		    }

		    location /phpmyadmin {
			    allow 127.0.0.1; #限定本機才能進入確保安全
			    deny all;
			    alias /usr/share/phpmyadmin;
			    try_files $uri $uri/ =404;
			
			    location ~ ^/phpmyadmin/(.+\.php)$ {
				    try_files $uri = 404;
				    fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
				    include fastcgi_params;
				    fastcgi_param SCRIPT_FILENAME $request_filename;
			    }
		    }
	    }
    }	


9.將www資料夾權限暫時改為 777，將需要的網站檔案放入（如：WordPress）

    #sudo chmod -R 777 /var/www
    #sudo chown -R www-data:www-data /var/www

10.執行安裝步驟結束後，將 wp-config.php 加入以下程式碼，確保 https 讀取完整資源

    #if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        $_SERVER['HTTPS'] = 'on';
    }
    
11.依照需要調整 WordPress 的可存取記憶體
    
    #define('WP_MEMORY_LIMIT', '256M');
    
12.安裝 Redis Server 加快網站存取速度

    #sudo apt install redis-server

13.


Themes
---
- [Dark theme](/theme-dark?both)
- [Vertical alignment](/theme-vertical-writing?both)

###### tags: `Ubuntu` `LEMP` `Linux` `Nginx` `MySQL` `PHP` 
