Ubuntu Server 16.04.x (Xenial Xerus) 安装 LEMP / LNMP 教程

LEMP 指的是 Linux + Nginx (发音 engine x 因此这里是 E 而不是 N) + MySQL + PHP 的简称,国内有些地方叫作 LNMP (由于 LNMP 无法读出来,而 LEMP 能够直接发音,因此从此本站教程一概都会写 LEMP)php

如下操做推荐在 root 用户下完成,请使用 sudo -i 切换到 root 用户进行操做html

1、安装 Nginx 1.10.x 或 1.12.x

因为 Nginx 更新频繁,而 Ubuntu Server 长久以来一直更新缓慢无法支持新功能,因此咱们有两种方案。mysql

截止 2017 年 5 月 6 日,目前最新的稳定版是 1.12.x 固然次版 1.10.x 也是很稳定的。nginx

一、用 Nginx 官方的 PPA 代替默认的源安装

首先,加入 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 官方源

首先,下载并导入 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 稍有区别,不过足够平常使用。

2、安装 PHP 7.0.x 或 PHP 7.1.x 或 PHP 5.6.x

目前 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-fpmphp5.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=1cgi.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

3、更新 Nginx 配置

下面开始,咱们假设您的域名是 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 页面则说明安装成功,若是不成功,请仔细对比步骤查找哪里出错

4、安装 MySQL 5.7.x

通过多年生产环境的测试,咱们推荐使用 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 &gt;= 8
MEDIUM Length &gt;= 8, numeric, mixed case, and special characters
STRONG Length &gt;= 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]."&lt;br /&gt;\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 的教程,若有问题能够随时发评论留言讨论。

原文连接

Ubuntu Server 16.04.x (Xenial Xerus) 安装 LEMP / LNMP 教程

相关文章
相关标签/搜索