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

原文地址:https://sb.sb/ubuntu-server-16-04-install-nginx-php-mysql/ 

LEMP 指的是 Linux + Nginx (发音 engine x 所以这里是 E 而不是 N) + MySQL + PHP 的简称,国内有些地方叫做 LNMP (因为 LNMP 没法读出来,而 LEMP 可以直接发音,所以今后本站教程一律都会写 LEMP)

以下教程适用于 Ubuntu 16.04.x 或 Ubuntu 14.04.x 但是我们尽量推荐使用最新的操作系统,以免旧系统软件版本过低有漏洞的可能

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

1、安装 Nginx 1.13.x

由于 Nginx 更新频繁,而 Ubuntu Server 长久以来一直更新缓慢没法支持新功能,并且由于系统自带的 OpenSSL 版本过低,所以我们这里推荐 Ondřej Surý 的 PPA

首先,加入 Nginx 的 PPA 并安装一些必要的软件

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/nginx-mainline
sudo apt-get update
sudo apt-get install curl vim wget unzip apt-transport-https lsb-release ca-certificates

接着安装 Nginx

sudo apt-get install nginx-extras

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

目前 PHP 官方开发只对这三个版本进行维护。

虽然一些老旧的程序、插件不支持 PHP 7.1.x 或 PHP 7.0.x,但是这货速度确实快不少啊,强烈呼吁开发者渐渐的转移到 PHP 7.1.x 或 PHP 7.0.x 的开发中,至于某些国产程序,就只能呵呵哒。

同样,由于 Ubuntu Server 官方更新实在太慢,导致很多时候没法打上最新的安全补丁,我们这里仍然推荐 Ondřej Surý 的 PPA

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

如有 PHP 7.0.x 或 PHP 5.6.x 需求的朋友,可以把下面的安装包文件名类似 php7.1-fpm 改成 php7.0-fpm 或 php5.6-fpm

执行完成后,安装一些常见的软件以及 PHP 7.0.x, 如果是 WordPress ,那么下面的 PHP 包足够满足大部分你需求:

sudo apt-get install php7.1-fpm php7.1-mysql php7.1-curl php7.1-gd php7.1-mbstring php7.1-mcrypt php7.1-xml php7.1-xmlrpc php7.1-zip php7.1-opcache

以上只安装了大部分 WordPress 必须的 PHP 组件,如果您的程序需要额外的 PHP 组件,可以通过 apt-cache search php7.1 命令来查找。

安装完成后,编辑 /etc/php/7.1/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.1/fpm/php.ini

然后重启 PHP7.1-fpm

systemctl restart php7.1-fpm

3、更新 Nginx 配置

下面开始,我们假设您的域名是 example.com 您的服务器 IP 是 192.0.2.2 (RFC 5737),并且您已经解析 example.com 的 A 记录到您的服务器 IP 192.0.2.2

我们直接新建立一个 /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.1-fpm 模式,如果安装其他版本请自行修改 php7.1-fpm.sock
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }
}
EOF

然后重启 Nginx

sudo nginx -t && sudo service nginx restart

我们的目录在 /var/www/example.com, 创建一个 phpinfo.php 并输入 phpinfo() 函数

sudo cat >> /var/www/example.com/phpinfo.php << EOF
<?php phpinfo(); ?>
EOF

好了,此时在浏览器输入 <code>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 的教程,如有问题可以随时发评论留言讨论。

posted @ 2017-11-27 01:06  km ben  阅读(197)  评论(0编辑  收藏  举报