LEMP 指的是 Linux + Nginx (發音 engine x 因此這裏是 E 而不是 N) + MySQL + PHP 的簡稱,國內有些地方叫作 LNMP (由於 LNMP 無法讀出來,而 LEMP 能夠直接發音,因此從此本站教程一概都會寫 LEMP)php
如下操做推薦在 root 用戶下完成,請使用 sudo -i
切換到 root 用戶進行操做html
因爲 Nginx 更新頻繁,而 Ubuntu Server 長久以來一直更新緩慢無法支持新功能,因此咱們有兩種方案。mysql
截止 2017 年 5 月 6 日,目前最新的穩定版是 1.12.x 固然次版 1.10.x 也是很穩定的。nginx
首先,加入 Nginx 的 PPA並安裝一些必要的軟件git
sudo apt-get install software-properties-common sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt-get install curl vim wget unzip
若是喜歡體驗 Nginx 的新功能,您可使用 add-apt-repository ppa:nginx/development
安裝 Nginx 的 Mainline 版代替默認的 Stable 版github
接着安裝 Nginxsql
sudo apt-get install nginx
若是您須要 Nginx 更多的功能,您可使用 sudo apt-get install nginx-extras
代替默認的 Nginx數據庫
PPA 方式更新也未必很及時,若是您但願追求更新的版本,咱們可使用第二種方法apache
首先,下載並導入 Nginx 官方的 Key:json
wget http://nginx.org/keys/nginx_signing.key sudo apt-key add nginx_signing.key
修改 /etc/apt/sources.list
文件或新建一個 /etc/apt/sources.list.d/nginx.list
文件,內容以下:
deb http://nginx.org/packages/ubuntu/ xenial nginx deb-src http://nginx.org/packages/ubuntu/ xenial nginx
接着就能夠更新系統並安裝和官網同步的最新版本 Nginx:
sudo apt-get update sudo apt-get install nginx
須要注意的是,官網的這個版本,安裝的插件和 Nginx 配置文件寫法和 Ubuntu Server 默認安裝以及 PPA 安裝的 Nginx 稍有區別,不過足夠平常使用。
目前 PHP 官方開發只對這三個版本進行維護。
雖然一些老舊的程序、插件不支持 PHP 7.0.x 或 PHP 7.1.x,可是這貨速度確實快很多啊,強烈呼籲開發者漸漸的轉移到 PHP 7.0.x 或 PHP 7.1.x 的開發中,至於某些國產程序,就只能呵呵噠。
一樣,因爲 Ubuntu Server 官方更新實在太慢,致使不少時候無法打上最新的安全補丁,咱們這裏推薦 Ondřej Surý 的 PPA
sudo add-apt-repository ppa:ondrej/php sudo apt-get update
因爲 PHP 7.1.x 實在太新,不少程序沒辦法跟上步伐,可是大多數正常的程序的對 PHP 7.0.x 的支持是很好的,因此本文推薦安裝 PHP 7.0.x
若有 PHP 7.1.x 或 PHP 5.6.x 需求的朋友,能夠把下面的安裝包文件名相似 php7.0-fpm
改爲 php7.1-fpm
或 php5.6-fpm
執行完成後,安裝一些常見的軟件以及 PHP 7.0.x, 若是是 WordPress ,那麼下面的 PHP 包足夠知足大部分你需求:
sudo apt-get install php7.0-fpm php7.0-mysql php7.0-curl php7.0-gd php7.0-mbstring php7.0-mcrypt php7.0-xml php7.0-xmlrpc php7.0-zip
以上只安裝了大部分 WordPress 必須的 PHP 組件,若是您的程序須要額外的 PHP 組件,能夠經過 apt-cache search php7.0
命令來查找。
showfom@ubuntu:~$ sudo apt-cache search php7.0 libapache2-mod-php7.0 - server-side, HTML-embedded scripting language (Apache 2 module) php-all-dev - package depending on all supported PHP development packages php7.0 - server-side, HTML-embedded scripting language (metapackage) php7.0-cgi - server-side, HTML-embedded scripting language (CGI binary) php7.0-cli - command-line interpreter for the PHP scripting language php7.0-common - documentation, examples and common module for PHP php7.0-curl - CURL module for PHP php7.0-dev - Files for PHP7.0 module development php7.0-gd - GD module for PHP php7.0-gmp - GMP module for PHP php7.0-json - JSON module for PHP php7.0-ldap - LDAP module for PHP php7.0-mysql - MySQL module for PHP php7.0-odbc - ODBC module for PHP php7.0-opcache - Zend OpCache module for PHP php7.0-pgsql - PostgreSQL module for PHP php7.0-pspell - pspell module for PHP php7.0-readline - readline module for PHP php7.0-recode - recode module for PHP php7.0-snmp - SNMP module for PHP php7.0-sqlite3 - SQLite3 module for PHP php7.0-tidy - tidy module for PHP php7.0-xml - DOM, SimpleXML, WDDX, XML, and XSL module for PHP php7.0-xmlrpc - XMLRPC-EPI module for PHP libphp7.0-embed - HTML-embedded scripting language (Embedded SAPI library) php7.0-bcmath - Bcmath module for PHP php7.0-bz2 - bzip2 module for PHP php7.0-enchant - Enchant module for PHP php7.0-fpm - server-side, HTML-embedded scripting language (FPM-CGI binary) php7.0-imap - IMAP module for PHP php7.0-interbase - Interbase module for PHP php7.0-intl - Internationalisation module for PHP php7.0-mbstring - MBSTRING module for PHP php7.0-mcrypt - libmcrypt module for PHP php7.0-phpdbg - server-side, HTML-embedded scripting language (PHPDBG binary) php7.0-soap - SOAP module for PHP php7.0-sybase - Sybase module for PHP php7.0-xsl - XSL module for PHP (dummy) php7.0-zip - Zip module for PHP php7.0-dba - DBA module for PHP
安裝完成後,編輯 /etc/php/7.0/fpm/php.ini
並替換 ;cgi.fix_pathinfo=1
爲 cgi.fix_pathinfo=0
sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php/7.0/fpm/php.ini
而後重啓 PHP7.0-fpm
systemctl restart php7.0-fpm
下面開始,咱們假設您的域名是 example.com
您的服務器 IP 是 192.0.2.2
(RFC 5737),而且您已經解析 example.com
的 A 記錄到您的服務器 IP 192.0.2.2
不管用哪一種方法安裝 Nginx 他們默認的 nginx.conf 都指定了加載 /etc/nginx/conf.d/ 目錄下的 *.conf 文件,因此咱們直接新創建一個 /etc/nginx/conf.d/example.conf 並增長一些基本的配置
直接把下面一整行命令複製粘貼到你的終端
sudo cat >> /etc/nginx/conf.d/example.conf << EOF server { listen 80; listen [::]:80; # 指定網站目錄,可根據本身狀況更換,建議放在 /var/www 目錄下 root /var/www/example.com; index index.php index.html index.htm; # 默認第一個域名,替換 example.com 爲您的域名 server_name example.com; location / { try_files $uri $uri/ =404; } # 開啓 PHP7.0-fpm 模式 location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } } EOF
而後重啓 Nginx
sudo nginx -t && sudo nginx -s reload
咱們的目錄在 /var/www/example.com
, 建立一個 phpinfo.php
並輸入 phpinfo()
函數
sudo cat >> /var/www/example.com/phpinfo.php << EOF <?php phpinfo(); ?> EOF
好了,此時在瀏覽器輸入 http://example.com/phpinfo.php
,若是看到經典的 phpinfo 頁面則說明安裝成功,若是不成功,請仔細對比步驟查找哪裏出錯
通過多年生產環境的測試,咱們推薦使用 Percona Server 代替原生的 MySQL
按照官方教程 下載最新版的 .deb 文件
wget https://repo.percona.com/apt/percona-release_0.1-4.$(lsb_release -sc)_all.deb sudo dpkg -i percona-release_0.1-4.$(lsb_release -sc)_all.deb
此時會在 /etc/apt/sources.list.d/percona-release.list
文件加入官方源,在安裝以前能夠先檢查是否存在
更新系統並安裝 Percona Server 5.7
sudo apt-get update sudo apt-get install percona-server-server-5.7
安裝成功後系統會讓您輸入兩次 MySQL 的 root 密碼,請切記必定要使用隨機的、不可被人猜想的密碼
我親眼目測過不少新手第一次安裝用了弱密碼,後來服務器就被人日,因此這裏強烈推薦安裝完 MySQL 後,執行一次安全設置,很簡單的一條命令
mysql_secure_installation
執行後會讓您選擇密碼強度,通常狀況下選擇 1 或者 2
root@demo:~# mysql_secure_installation Securing the MySQL server deployment. Enter password for user root: VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No:y 請輸入 y 進行初始安全設置 There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 最強大的密碼固然要輸入 2 Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : n 若是以前設置了強密碼,則不須要從新更改 root 密碼,反之則按 y 回車後輸入兩次重置 By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y 移除匿名用戶,沒啥鳥用就直接移除吧 Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y 關閉 root 遠程登陸,不須要進行遠程登陸的話就關了吧 Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) :y 移除 test 數據庫 Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y 重置數據庫權限
再次提醒,密碼必定要隨機、不可猜想,使用弱密碼而致使服務器被日的例子實在是數不清楚
作好初始安全設置後,咱們就能夠進行建立數據庫操做,首先使用 root 登陸 MySQL
mysql -u root -p
會提示讓您輸入密碼,輸入密碼登錄後,建立一個名爲 example 的數據庫
CREATE DATABASE example DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
因爲手機端的流行,咱們已經再也不使用 utf-8
編碼,而改用 utf8mb4
這樣咱們就能夠在 MySQL 數據庫裏儲存 emoji 表情了,好比這樣 ??????
接着咱們建立一個叫作 example_user
的用戶,使用強大的密碼而且賦予 example_database
數據庫權限
GRANT ALL ON example.* TO 'example_user'@'localhost' IDENTIFIED BY '這裏改爲你要設置的強大的沒人能猜出來的隨機的密碼';
終端會提示 Query OK, 0 rows affected, 1 warning (0.00 sec)
不用去管它
而後咱們刷新權限
FLUSH PRIVILEGES;
沒問題就退出
EXIT;
而後咱們測試一下數據庫,在 /var/www/example.com
目錄下創建一個 mysql-test.php
sudo cat >> /var/www/example.com/mysql-test.php << EOF <?php \$dbname = 'example'; //MySQL 數據庫名 \$dbuser = 'example_user'; //MySQL 用戶名 \$dbpass = '你的強大的沒人能夠猜出來的密碼'; \$dbhost = 'localhost'; //安裝在本地就用 localhost \$link = mysqli_connect(\$dbhost, \$dbuser, \$dbpass) or die("Unable to Connect to '\$dbhost'"); mysqli_select_db(\$link, \$dbname) or die("Could not open the db '\$dbname'"); \$test_query = "SHOW TABLES FROM \$dbname"; \$result = mysqli_query(\$link, \$test_query); \$tblCnt = 0; while(\$tbl = mysqli_fetch_array(\$result)) { \$tblCnt++; #echo \$tbl[0]."<br />\n"; } if (!\$tblCnt) { echo "MySQL is working fine. There are no tables."; } else { echo "MySQL is working fine. There are \$tblCnt tables."; } ?> EOF
建立完畢後訪問 http://example.com/mysql-test.php
若是出現 MySQL is working fine. There are no tables.
則說明 MySQL 工做正常。
好了,以上就是基本的 Ubuntu Server 16.04 安裝 LEMP 的教程,若有問題能夠隨時發評論留言討論。
原文連接