阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

147次阅读
没有评论

共计 8953 个字符,预计需要花费 23 分钟才能阅读完成。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

LEMP是一个操作系统和几个开源软件包的合称。缩写 LEMP 来自 Linux,Nginx(发音是 engine-x)HTTP 服务器,MySQL 数据库,和 PHP/ Perl/ Python 的首字母。

在这篇教程里,让我们看一下如何在 Ubuntu 14.10 上安装 Nginx,MySQL 或 MariaDB,PHP 和 phpMyAdmin。

LEMP 架构及应用部署——Nginx 延伸  http://www.linuxidc.com/Linux/2013-07/87548.htm

CentOS 5.6+Nginx 1.0+PHP 5.3.6+MySQL 5.5.11 构建 LEMP(X64)平台 http://www.linuxidc.com/Linux/2011-07/38107.htm

Linux 环境下 Nginx 搭建高性能 WEB 服务器 LEMP http://www.linuxidc.com/Linux/2009-11/22465.htm

LAMP 架构协同应用的实例——phpMyAdmin http://www.linuxidc.com/Linux/2013-07/87645.htm

LAMP 应用之 phpMyAdmin、Wordpress http://www.linuxidc.com/Linux/2013-04/82757.htm

phpMyAdmin 老出现登陆超时解决方法 http://www.linuxidc.com/Linux/2012-09/70715.htm

Ubuntu 安装 phpMyAdmin 与 Adminer http://www.linuxidc.com/Linux/2012-08/69419.htm

在 LAMP 基础上实现 SSL 功能并安装 phpMyAdmin http://www.linuxidc.com/Linux/2012-07/66905.htm

Ubuntu 14.04 配置 LAMP+phpMyAdmin PHP(5.5.9)开发环境 http://www.linuxidc.com/Linux/2014-10/107924.htm

安装 Nginx

Nginx (发音是 engine-x)是一个免费的、开源的、高性能 HTTP 服务器和反向代理,也可以用作 IMAP/POP3 代理服务器,它是由 Igor Sysoev 开发。

要安装 Nginx,在你的终端里输入下面的命令:

注意:如果你的系统里已经安装了 apache2,先卸载掉以避免冲突。要卸载 apache,运行下面的命令:

  1. sudo aptget purge apache2*
  2. sudo aptget autoremove y

现在,用下面的命令安装 nginx:

  1. sudo aptget install nginx

用下面的命令启用 Nginx 服务:

  1. sudo service nginx start

测试 nginx

打开你的浏览器访问 http://IP 地址 / 或者 http://localhost/。将可以看到类似下面的截图。

 在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

配置 Nginx

用任意文本编辑器打开文件/etc/nginx/nginx.conf

  1. sudo nano /etc/nginx/nginx.conf

设置 worker_processes(例如,你系统里 CPU 数目)。查看 CPU 数目,可以使用命令“lscpu”。在我这里是“1”。所以我把这个值设为 1。

  1. worker_processes 1;

重启 Nginx 服务:

  1. sudo service nginx restart

默认虚拟主机(服务器模块)定义在文件 /etc/nginx/sites-available/default 里。

用任意文本编辑器打开文件 /etc/nginx/sites-available/default。

  1. sudo nano /etc/nginx/sitesavailable/default

在 Server 区域里,按如下设置服务器 FQDN 或 IP 地址。确保你增加了 index.php 这一行。

[...]
server {
      listen 80 default_server;
      listen [::]:80 default_server ipv6only=on;
      root /usr/share/nginx/html;
      index index.php index.html index.htm;
      # Make site accessible from http://localhost/
      server_name server.unixmen.local;
[...]

这里面

  • listen 80; –> 监听 ipv4 端口
  • listen [::]:80 default_server ipv6only=on; –> 监听 ipv6 宽口
  • root /usr/share/nginx/html; –> 文件根目录
  • server_name server.unixmen.local; –> 服务器 FQDN

现在,向下滚动找到区域 #location ~ .php$。去掉注释并按如下修改:

location ~ \.php$ {
         try_files $uri =404;   ---------> Add this line
         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.conf;
    }

这里面,我增加了额外一行 ‘try_files $uri =404;’ 用于避免 0day 漏洞。

保存文件并退出。

测试 nginx 配置

使用下面的命令测试 nginx 配置是否存在语法错误:

  1. sudo nginx t

典型输出:

  1. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  2. nginx: configuration file /etc/nginx/nginx.conf test is successful

最后重启 nginx 服务

  1. sudo service nginx restart

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-11/109438p2.htm

安装 MySQL

MySQL是一个关系型数据库管理系统(RDBMS),作为服务启动提供给多用户访问多种数据库,尽管 SQLite 可能有更多的嵌入式部署。

  1. sudo aptget install mysqlserver mysqlclient

在安装过程中,会提示你设置 MySQL 超级用户密码。输入密码并按确认。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

重新输入密码。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

现在,MySQL 服务器就安装好了。

你可以用下面的命令检查 MySQL 服务器状态:

  1. sudo service mysql status

典型输出:

  1. mysql start/running, process 5671

注意:如果你希望使用 MariaDB 而不是 MySQL,可以参考下面的步骤。

安装 MariaDB

MariaDB是 MySQL 的一个直接替代软件。它是一个稳定、可扩展又可靠的 SQL 服务器,包含许多增强功能。

首先,如果有的话你得先卸载掉 MySQL。要完全卸载 MySQL 包括配置文件,输入如下命令:

  1. sudo aptget purge mysql*

运行如下命令清除不需要的软件包。

  1. sudo aptget autoremove

在卸载完 MySQL 后,运行如下命令安装 MariaDB。

  1. sudo aptget install mariadbserver mariadbclient

另外,如果你希望体验最新版的 MariaDB,可以从 MariaDB 仓库安装。运行下面的命令添加 PPA。在写这篇文章的时候,MariaDB PPA 还没有更新 Ubuntu 14.10。不过,我们还是可以使用 Ubuntu 14.04 的仓库来替代。

  1. sudo aptget install softwarepropertiescommon
  2. sudo aptkey adv recvkeys keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
  3. sudo addaptrepository ‘deb http://sgp1.mirrors.digitalocean.com/mariadb/repo/5.5/ubuntu trusty main’

用如下命令更新一下软件源列表,然后安装 MariaDB:

  1. sudo aptget update
  2. sudo aptget install mariadbserver mariadbclient y

在安装过程中,会提示你设置数据库‘root’用户密码。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

重新输入一次密码:

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

点击‘是’迁移到 MariaDB。注意一下,如果在安装 MariaDB 之前没有装过 MySQL 的话,不会提示你这个问题。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

你可以用如下命令检查 MariaDB 版本:

  1. sudo mysql v u root p

典型输出:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 5.5.39-MariaDB-2 (Ubuntu)

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Reading history-file /home/sk/.mysql_history
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

用如下命令检查 MariaDB 是否已经开始运行:

  1. sudo service mysql status

典型输出:

* /usr/bin/mysqladmin  Ver 9.0 Distrib 5.5.39-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Server version        5.5.39-MariaDB-2
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/run/mysqld/mysqld.sock
Uptime:            2 min 21 sec

Threads: 1  Questions: 566  Slow queries: 0  Opens: 330  Flush tables: 4  Open tables: 22  Queries per second avg: 4.014

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

LEMP是一个操作系统和几个开源软件包的合称。缩写 LEMP 来自 Linux,Nginx(发音是 engine-x)HTTP 服务器,MySQL 数据库,和 PHP/ Perl/ Python 的首字母。

在这篇教程里,让我们看一下如何在 Ubuntu 14.10 上安装 Nginx,MySQL 或 MariaDB,PHP 和 phpMyAdmin。

LEMP 架构及应用部署——Nginx 延伸  http://www.linuxidc.com/Linux/2013-07/87548.htm

CentOS 5.6+Nginx 1.0+PHP 5.3.6+MySQL 5.5.11 构建 LEMP(X64)平台 http://www.linuxidc.com/Linux/2011-07/38107.htm

Linux 环境下 Nginx 搭建高性能 WEB 服务器 LEMP http://www.linuxidc.com/Linux/2009-11/22465.htm

LAMP 架构协同应用的实例——phpMyAdmin http://www.linuxidc.com/Linux/2013-07/87645.htm

LAMP 应用之 phpMyAdmin、Wordpress http://www.linuxidc.com/Linux/2013-04/82757.htm

phpMyAdmin 老出现登陆超时解决方法 http://www.linuxidc.com/Linux/2012-09/70715.htm

Ubuntu 安装 phpMyAdmin 与 Adminer http://www.linuxidc.com/Linux/2012-08/69419.htm

在 LAMP 基础上实现 SSL 功能并安装 phpMyAdmin http://www.linuxidc.com/Linux/2012-07/66905.htm

Ubuntu 14.04 配置 LAMP+phpMyAdmin PHP(5.5.9)开发环境 http://www.linuxidc.com/Linux/2014-10/107924.htm

安装 Nginx

Nginx (发音是 engine-x)是一个免费的、开源的、高性能 HTTP 服务器和反向代理,也可以用作 IMAP/POP3 代理服务器,它是由 Igor Sysoev 开发。

要安装 Nginx,在你的终端里输入下面的命令:

注意:如果你的系统里已经安装了 apache2,先卸载掉以避免冲突。要卸载 apache,运行下面的命令:

  1. sudo aptget purge apache2*
  2. sudo aptget autoremove y

现在,用下面的命令安装 nginx:

  1. sudo aptget install nginx

用下面的命令启用 Nginx 服务:

  1. sudo service nginx start

测试 nginx

打开你的浏览器访问 http://IP 地址 / 或者 http://localhost/。将可以看到类似下面的截图。

 在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

配置 Nginx

用任意文本编辑器打开文件/etc/nginx/nginx.conf

  1. sudo nano /etc/nginx/nginx.conf

设置 worker_processes(例如,你系统里 CPU 数目)。查看 CPU 数目,可以使用命令“lscpu”。在我这里是“1”。所以我把这个值设为 1。

  1. worker_processes 1;

重启 Nginx 服务:

  1. sudo service nginx restart

默认虚拟主机(服务器模块)定义在文件 /etc/nginx/sites-available/default 里。

用任意文本编辑器打开文件 /etc/nginx/sites-available/default。

  1. sudo nano /etc/nginx/sitesavailable/default

在 Server 区域里,按如下设置服务器 FQDN 或 IP 地址。确保你增加了 index.php 这一行。

[...]
server {
      listen 80 default_server;
      listen [::]:80 default_server ipv6only=on;
      root /usr/share/nginx/html;
      index index.php index.html index.htm;
      # Make site accessible from http://localhost/
      server_name server.unixmen.local;
[...]

这里面

  • listen 80; –> 监听 ipv4 端口
  • listen [::]:80 default_server ipv6only=on; –> 监听 ipv6 宽口
  • root /usr/share/nginx/html; –> 文件根目录
  • server_name server.unixmen.local; –> 服务器 FQDN

现在,向下滚动找到区域 #location ~ .php$。去掉注释并按如下修改:

location ~ \.php$ {
         try_files $uri =404;   ---------> Add this line
         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.conf;
    }

这里面,我增加了额外一行 ‘try_files $uri =404;’ 用于避免 0day 漏洞。

保存文件并退出。

测试 nginx 配置

使用下面的命令测试 nginx 配置是否存在语法错误:

  1. sudo nginx t

典型输出:

  1. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  2. nginx: configuration file /etc/nginx/nginx.conf test is successful

最后重启 nginx 服务

  1. sudo service nginx restart

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-11/109438p2.htm

安装 PHP

PHP(PHP: Hypertext Preprocessor 的递归缩写)是一个应用广泛的开源通用脚本语言,特别适合于网页开发,可以直接嵌入到 HTML 中。

使用如下命令安装 PHP:

sudo apt-get install php5 php5-fpm php5-MySQL

配置 PHP

用任意文本编辑器打开 php.ini 文件:

  1. sudo nano /etc/php5/fpm/php.ini

找到这一行‘cgi.fix_pathinfo=1′,去掉注释并把值 1 改为 0。

cgi.fix_pathinfo=0

现在重启 php-fpm 服务。

  1. sudo service php5fpm restart

测试 PHP

在 nginx 文档根目录下创建一个测试文件“testphp.php”。

  1. sudo nano /usr/share/nginx/html/testphp.php

加入下面几行。

<?php
 phpinfo();
?>

保存文件并退出。

访问地址http://server-ip-address/testphp.php。将显示出所有关于 php 的信息,比如版本、构建日期以及命令等等。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

PHP-FPM 会默认监听套接字/var/run/php5-fpm.sock。如果你希望 PHP-FPM 使用 TCP 连接,打开文件/etc/php5/fpm/pool.d/www.conf

  1. sudo nano /etc/php5/fpm/pool.d/www.conf

找到这一行 listen = /var/run/php5-fpm.sock,

;listen = /var/run/php5-fpm.sock

把它改成listen = 127.0.0.1:9000

listen = 127.0.0.1:9000

保存退出。重启 php5-fpm 服务。

  1. sudo service php5fpm restart

现在打开 nginx 配置文件:

  1. sudo nano /etc/nginx/sitesavailable/default

找到这一行fastcgi_pass unix:/var/run/php5-fpm.sock;,参考下面把它改成 fastcgi_pass 127.0.0.1:9000;。

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;fastcgi_pass 127.0.0.1:9000;
        #       # 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.conf;
   }

保存退出。最后重启 nginx 服务。

  1. sudo service nginx restart

使用 phpMyAdmin 管理 MySQL 数据库(可选)

phpMyAdmin是一个免费的开源网页界面工具,用来管理你的 MySQL 数据库。

安装 phpMyAdmin

在 Debian 官方仓库里就有。所以可以用下面的命令安装:

  1. sudo aptget install phpmyadmin

选择一个网页服务器。默认情况下,这里不会显示 nginx。所以,选择 apache 或者 lighttpd,然后我们再把 phpMyAdmin 和 nginx 连接起来工作。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

选择‘是’通过 dbconfig-common 为 phpMyAdmin 配置数据库。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

输入数据库的管理员账号密码。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

输入 phpmyadmin 帐号的 MySQL 密码:

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

重新输入一次密码。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

phpMyAdmin 就安装完成了。

创建一个 phpMyAdmin 的软连接到网站根目录。这里我们的网站根文档目录是 /usr/share/nginx/html/。

  1. sudo ln s /usr/share/phpmyadmin/ /usr/share/nginx/html

重启 nginx 服务。

  1. sudo service nginx restart

访问 phpMyAdmin 网页控制台

现在你可以在浏览器中通过地址 http://server-ip-address/phpmyadmin/ 访问 phpMyAdmin 的控制台了。

输入你在前面步骤里留下的 MySQL 用户名和密码。在我这里是“root”和“Ubuntu”。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

就可以重定向到 phpMyAdmin 的网页管理首页。

在 Ubuntu 14.10/14.04/13.10 上安装 LEMP 服务和 phpMyAdmin

现在你就可以在 phpMyAdmin 网页里管理你的 MyQL 数据库了。

就这样。你的 LEMP 服务器已经配置完毕,可以使用了。

更多 Ubuntu 相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计8953字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中