Ubuntu16.04部署LNMP環境

  • 2019 年 12 月 22 日
  • 筆記

LNMP就是相對於LAMP環境的開發環境: LAMP = Linux + Apache + MySQL + PHP LNMP = Linux + Nginx + MySQL + PHP

自己從網絡上找了許多的教程,但是馬虎的比較多,詳細的還是沒有幾個,並且有許多的Bug,現在將自己踩的坑做出來的教程呈上。

LNMP就是採用nginx服務器來進行作為服務器應用運行環境,具體怎麼設置,請看下文:

升級系統

sudo apt update

安裝Nginx

sudo apt install nginx

默認web路徑在/var/www/html, 訪問本地http://loaclhost/出現Welcome to Nginx!,即是安裝成功!

安裝Mysql

sudo apt install mysql-server mysql-client

安裝過程中需設置 mysql root 密碼 mysql_secure_installation #加強安全性,可選

安裝PHP

安裝環境檢測

# 如果之前有其他版本PHP,先卸載  sudo apt-get autoremove php5php5-fpmphp7

配置安裝源並且安裝

sudo apt-get install software-properties-common python-software-properties  sudo add-apt-repository ppa:ondrej/php && sudo apt-get update  # 安裝PHP7.2  sudo apt-get -y install php7.2

安裝PHP及常用擴展

sudo apt-get install php7.2-fpm php7.2-mysql php7.2-curl php7.2-json php7.2-mbstring php7.2-xml php7.2-intl -y 

安裝其他擴展(按需安裝)

sudo apt-get install php7.2-gd  sudo apt-get install php7.2-soap  sudo apt-get install php7.2-gmp  sudo apt-get install php7.2-odbc  sudo apt-get install php7.2-pspell  sudo apt-get install php7.2-bcmath  sudo apt-get install php7.2-enchant  sudo apt-get install php7.2-imap  sudo apt-get install php7.2-ldap  sudo apt-get install php7.2-opcache  sudo apt-get install php7.2-readline  sudo apt-get install php7.2-sqlite3  sudo apt-get install php7.2-xmlrpc  sudo apt-get install php7.2-bz2  sudo apt-get install php7.2-interbase  sudo apt-get install php7.2-pgsql  sudo apt-get install php7.2-recode  sudo apt-get install php7.2-sybase  sudo apt-get install php7.2-xsl  sudo apt-get install php7.2-cgi  sudo apt-get install php7.2-dba  sudo apt-get install php7.2-phpdbg  sudo apt-get install php7.2-snmp  sudo apt-get install php7.2-tidy  sudo apt-get install php7.2-zip

Nginx配置PHP環境

默認配置文件位置:/etc/nginx/sites-availble 文件夾下:

新建一個文件名為自己域名的文件,例如 「debuginn.cn.conf」,文件內容為:

server {      listen 443;      server_name www.debuginn.cn;      root /var/www/html/www.debuginn.cn;      index index.html index.php;        location / {          try_files $uri $uri/ /index.php?$args;      }        location ~ .php$ {          include snippets/fastcgi-php.conf;            # With php7.0-cgi alone:          # fastcgi_pass 127.0.0.1:9000;          # With php7.0-fpm:          fastcgi_pass unix:/run/php/php7.2-fpm.sock;      }  }    server {      listen 80;      server_name www.debuginn.cn;      return 301 http://$server_name$request_uri;  }

升級HTTPS服務文件配置(非必須)

server {      listen 443;        server_name www.debuginn.cn;      ssl on;      root /var/www/html/www.debuginn.cn;      index index.html index.php;      ssl_certificate  cert/1826431_www.debuginn.cn.pem;      ssl_certificate_key cert/1826431_www.debuginn.cn.key;      ssl_session_timeout 5m;      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;      ssl_prefer_server_ciphers on;        location / {          try_files $uri $uri/ /index.php?$args;      }        location ~ .php$ {          include snippets/fastcgi-php.conf;            # With php7.0-cgi alone:          # fastcgi_pass 127.0.0.1:9000;          # With php7.0-fpm:          fastcgi_pass unix:/run/php/php7.2-fpm.sock;      }  }    server {      listen 80;      server_name www.debuginn.cn;        return 301 https://$server_name$request_uri;  }

添加重定向 (非必須)

http重定向為https

server {      listen 80;      server_name www.debuginn.cn;      return 301 https://$server_name$request_uri;  }

return 301 https://$server_name$request_uri;

連接配置信息
ln -s /etc/nginx/sites-available /etc/nginx/sites-enabled

添加host文件

hosts文件位置在 /etc/hosts 添加一行記錄,如下:

127.0.0.1 www.debuginn.cn

OK 配置完畢,希望對你有用哦!