Ubuntu 18.04 LEMP(Linux+Nginx+Mysql+PHP)環境建立

Ping-Lun Liao
3 min readJan 31, 2019

--

底下紀錄如何在 Ubuntu 18.04 上建立 LEMP 的環境。 This note demonstrates how to create a LEMP environment on an Ubuntu 18.04 server.

步驟一:安裝 Nginx Web Server (Step One: Installing nginx web server)

Open a console and input the following commands to install nginx.

  1. sudo apt-get update
  2. sudo apt-get install nginx

接著查詢主機IP

You can find your server’s public IP address by the following command:
ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s/\/.*$//’

在瀏覽器鍵入 local host ip。 Type the address in your web browser(Google chrome or firefox .etc).

http://server_domain_name_or_IP

若在瀏覽器上看到下圖,表示 nginx 已安裝成功。If you see the below message, your nginx installation is completed.

可看到上圖時,nginx便已經可跑靜態網頁了,預設的html檔案存放位置為 /var/www/html

步驟二:安裝MySql(Step Two: Installing mysql

Open a console and input the following commands to install mysql.

sudo apt-get install mysql-server

安裝完後輸入以下指令進行安全性設定 Type the following command to secure the MySQL installation:

sudo mysql_secure_installation

步驟三:安裝php與設定 php.ini(Step Three: Installing php and configuring the php.ini)

Installing php:

sudo apt-get install php-fpm php-mysql

Modifying php.ini file:

sudo nano /etc/php/7.2/fpm/php.ini

Replace this line:
;cgi.fix_pathinfo=1 with

cgi.fix_pathinfo=0

Save and restart php service:

sudo service php7.2-fpm restart

步驟四:設定Nginx的php模組功能(The PHP Processor configuration for nginx)

建立一個nginx設定

Create the configuration file for nginx:

sudo nano /etc/nginx/sites-available/test.com

Input the below text to the configuration file:

server {  listen 80;
root /var/www/html;
index index.php index.html index.htm;
server_name test.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

# Creating a symbolic link to enable the new server block.

sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/

# Unlink the default configuration

sudo unlink /etc/nginx/sites-enabled/default

# Checking the new configuration file

sudo nginx -t

# Reload Nginx

sudo service nginx reload

步驟五:建立php測試檔案(Step Five: Create a php file to test)

# Creating the test php file

sudo nano /var/www/html/info.php

Input the following php code into the new file(info.php).
Open your browser to visit this page:
http://your_server_domain_or_IP/info.php
You should see some information about your server:

參考資料:

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04

Originally published at yunlinsong.blogspot.com on January 31, 2019.

--

--

No responses yet