on  it road.com

安装 PHP

最后,要在我们的 LEMP 堆栈中添加应用服务器功能,我们必须安装 PHP。
我们使用以下命令从 apt 存储库安装 php-fpm。
在我们的例子中,它安装了 PHP 5.5.
不过,此时可以使用稳定版 PHP 7.
如果我们想使用最新版本,则可以从 http://www.php.net 下载

jackli@myserver:~$ sudo apt-get install php5-fpm
Reading package lists... Done
Building dependency tree

还要安装 php5-mysqlnd 包,如下所示。
这个包直接从 PHP 脚本提供 MySQL 数据库连接的模块。
它包括通用的 mysql 模块,可用于连接到所有版本的 MySQL

jackli@myserver:/etc/nginx/sites-enabled$ sudo apt-get install php5-mysqlnd
Reading package lists... Done
Building dependency tree

jackli@myserver:/etc/nginx/sites-enabled$ sudo service nginx restart
* Restarting nginx nginx               [ OK ]
jackli@myserver:/etc/nginx/sites-enabled$

将 php-fpm 添加到系统启动中。

jackli@myserver:/etc/nginx$ sudo update-rc.d php5-fpm defaults
Adding system startup for /etc/init.d/php5-fpm ...
   /etc/rc0.d/K20php5-fpm -> ../init.d/php5-fpm
   /etc/rc1.d/K20php5-fpm -> ../init.d/php5-fpm
   /etc/rc6.d/K20php5-fpm -> ../init.d/php5-fpm
   /etc/rc2.d/S20php5-fpm -> ../init.d/php5-fpm
   /etc/rc3.d/S20php5-fpm -> ../init.d/php5-fpm
   /etc/rc4.d/S20php5-fpm -> ../init.d/php5-fpm
   /etc/rc5.d/S20php5-fpm -> ../init.d/php5-fpm
jackli@myserver:/etc/nginx$

LEMP Stack 已在 Ubuntu 服务器上安装和配置。

安装 Nginx

我们使用 Nginx 作为我们 LEMP 服务器的 Web 服务器。
使用 apt-cache 搜索 Nginx 的可用包。
我们可能会注意到,Nginx 有多种可用的软件包,如 core、light、full、naxsi 和 extras。
这些软件包中的每一个都包含特定选择的可选模块和第三方模块,这些模块打包在一起以满足特定需求。

jackli@myserver:~$ apt-cache search nginx
gunicorn - Event-based HTTP/WSGI server
nginx - small, powerful, scalable web/proxy server
nginx-common - small, powerful, scalable web/proxy server - common files
nginx-core - nginx web/proxy server (core version)
nginx-core-dbg - nginx web/proxy server (core version) - debugging symbols
nginx-doc - small, powerful, scalable web/proxy server - documentation
 
jackli@myserver:~$

我们更喜欢安装 nginx-core 并在需要时安装其他模块。
但在安装 Nginx 之前,请确保我们已更新 apt-get 包索引文件。

jackli@myserver:~$ sudo apt-get update
Ign http://pk.archive.ubuntu.com trusty InRelease
 
jackli@myserver:~$

它给了我们所有的点击,因为我们的包索引文件已经更新。
在情况下,输出可能会有所不同。

现在安装 Nginx。

jackli@myserver:~$ sudo apt-get install nginx

在 Ubuntu 防火墙中打开端口 80。

jackli@myserver:~$ sudo ufw allow 80
Rule updated
Rule updated (v6)
jackli@myserver:~$

使用另一台机器上的浏览器检查 Nginx Web 服务器的响应。

在 Ubuntu 上安装 LEMP 堆栈

LEMP 堆栈是 LAMP 的一种变体,用于开发和部署 Web 应用程序。
LAMP 由 Linux、Apache、MySQL 和 PHP 组成。
由于 LAMP 的模块化特性,单个组件可以替换为其他组件。
在 LEMP 中,Apache Web 服务器被轻量级但功能强大的 Nginx Web 服务器取代。
其他组件如 Linux、MySQL 和 PHP 保持不变。

在本文中,我们将看到如何在 Ubuntu 14.04 LTS 服务器上安装和配置 MySQL、Nginx、PHP。
我们还将向我们展示如何在此 LEMP 服务器上部署 phpMyAdmin 应用程序。

部署 phpMyAdmin

为了测试我们的 LEMP 服务器的功能,我们在其上部署了一个 PHP 应用程序。

转到 https://www.phpmyadmin.net/downloads/ 并根据 PHP 和 MySQL 版本选择一个版本。

我们选择了最新的 phpMyAdmin 4.6.0。

jackli@myserver:~$ wget https://files.phpmyadmin.net/phpMyAdmin/4.6.0/phpMyAdmin-4.6.0-english.tar.gz
 
jackli@myserver:~$ ls
phpMyAdmin-4.6.0-english.tar.gz  README.txt

jackli@myserver:~$

现在,创建一个用于部署 Web 应用程序的目录,将 phpMyAdmin 解压缩到该目录下的子目录中,并将该目录的所有权更改为 www-data(Nginx Web 服务器的默认用户)。

jackli@myserver:~$ sudo mkdir /websites
jackli@myserver:~$ sudo tar -xvzf phpMyAdmin-4.6.0-english.tar.gz
jackli@myserver:~$ sudo mv phpMyAdmin-4.6.0-english /websites/phpmyadmin
jackli@myserver:~$ sudo chown -R www-data:www-data /websites

现在,向 Nginx 添加一个新网站,如下所示:

jackli@myserver:~$ cd /etc/nginx/sites-available/
jackli@myserver:/etc/nginx/sites-available$ ls
default
jackli@myserver:/etc/nginx/sites-available$ sudo cp default phpmyadmin
jackli@myserver:/etc/nginx/sites-available$ sudo vi phpmyadmin

在 server 指令下,将侦听端口更改为 8081,因为我们要为我们的主网站保留端口 80。

listen 8081 default_server;
listen [::]:8081 default_server ipv6only=on;

将文档根位置更新到 phpMyAdmin 目录。

root /websites/phpmyadmin/;

将 index.php 添加到索引文件列表。

index index.php index.html index.htm;

并取消注释 php 部分,如下所示。

location ~ \.php$ {
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
       #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
       #
       #       # With php5-cgi alone:
       #       fastcgi_pass 127.0.0.1:9000;
       #       # With php5-fpm:
               fastcgi_pass unix:/var/run/php5-fpm.sock;
               fastcgi_index index.php;
               include fastcgi_params;
       }

为了启用这个网站,我们在 /etc/nginx/sites-enabled/ 创建一个 phpMyAdmin 配置文件的符号链接,然后重启 Nginx 服务。
还允许端口 8081 通过 Ubuntu 防火墙。

jackli@myserver:/etc/nginx/sites-available$ cd /etc/nginx/sites-enabled
jackli@myserver:/etc/nginx/sites-enabled$ sudo cp -s /etc/nginx/sites-available/phpmyadmin .
jackli@myserver:/etc/nginx/sites-enabled$ sudo service nginx restart
* Restarting nginx nginx                            [ OK ]
jackli@myserver:/etc/nginx/sites-enabled$ sudo ufw allow 8081
Rule added
Rule added (v6)
jackli@myserver:/etc/nginx/sites-enabled$

从另一台机器浏览网站以检查配置是否正确。

安装 MySQL

LEMP 堆栈的第三个字母/成员是 MySQL 数据库。
MySQL 5.5 数据库服务器可从 apt 存储库获得,因此我们正在安装它。
不过,我们也可以从 http://dev.mysql.com/downloads/ 下载并安装更高版本。

jackli@myserver:~$ sudo apt-get install mysql-server
[sudo] password for jackli:
Reading package lists... Done
Building dependency tree

系统启动时添加mysql服务。

jackli@myserver:~$ sudo update-rc.d mysql defaults
[sudo] password for jackli:
Adding system startup for /etc/init.d/mysql ...
   /etc/rc0.d/K20mysql -> ../init.d/mysql
   /etc/rc1.d/K20mysql -> ../init.d/mysql
   /etc/rc6.d/K20mysql -> ../init.d/mysql
   /etc/rc2.d/S20mysql -> ../init.d/mysql
   /etc/rc3.d/S20mysql -> ../init.d/mysql
   /etc/rc4.d/S20mysql -> ../init.d/mysql
   /etc/rc5.d/S20mysql -> ../init.d/mysql
jackli@myserver:~$

执行 MySQL 安装后脚本。

jackli@myserver:~$ /usr/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] n
... skipping.
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? [Y/n] Y
... Success!
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? [Y/n] Y
... 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? [Y/n] Y
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed!  Not critical, keep moving...
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
jackli@myserver:~$
日期:2020-09-17 00:12:56 来源:oir作者:oir