2
0
mirror of https://github.com/LCTT/TranslateProject.git synced 2025-03-30 02:40:11 +08:00

[translated] 20141104 Install LEMP Server (Nginx, MySQL or MariaDB, PHP And phpMyAdmin) On Ubuntu 14.10 or 14.04 or 13.10.md

This commit is contained in:
Frank Zhang 2014-11-07 16:43:59 +08:00
parent 029426e7d6
commit 45858a7464
2 changed files with 348 additions and 349 deletions

View File

@ -1,349 +0,0 @@
zpl1025
Install LEMP Server (Nginx, MySQL or MariaDB, PHP And phpMyAdmin) On Ubuntu 14.10/14.04/13.10
================================================================================
**LEMP** is a combination of the operating system and open-source software stack. The acronym LEMP comes from the first letters of **L**inux, Nginx(**e**ngine-x) HTTP Server, **M**ySQL database, and **P**HP/**P**erl/**P**ython.
In this tutorial, let us see how to install Nginx, MySQL or MariaDB, PHP and phpMyAdmin on Ubuntu 14.10.
### Install Nginx ###
**Nginx** (pronounced as engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server written by Igor Sysoev.
To install Nginx enter the following command in your terminal:
**Note**: If you have already installed apache2 in your system, remove it first to avoid conflicts. To uninstall apache, run the following commands:
sudo apt-get purge apache2*
sudo apt-get autoremove -y
Now, install nginx using command:
sudo apt-get install nginx
Start Nginx service using the command:
sudo service nginx start
### Test nginx ###
Open up your web browser and navigate to http://ip-address/ or http://localhost/. You will see a screen something like below.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/10/Welcome-to-nginx-Mozilla-Firefox_005.png)
### Configure Nginx ###
Open the file **/etc/nginx/nginx.conf** in any editor:
sudo nano /etc/nginx/nginx.conf
Set the worker_processes (i.e No. of CPUs in your system). To see the no. of CPUs, use the command “lscpu”. In my case its “1″. So I set this as 1.
worker_processes 1;
Restart Nginx service:
sudo service nginx restart
The default vhost(server block) is defined in the **/etc/nginx/sites-available/default** file.
Open the file /etc/nginx/sites-available/default in any editor.
sudo nano /etc/nginx/sites-available/default
Under the Server section, set the server FQDN or IP address as shown below. Make sure youve added a index.php line.
[...]
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;
[...]
Here,
- **listen 80;** > listen for ipv4
- **listen [::]:80 default_server ipv6only=on;** > listen for ipv6
- **root /usr/share/nginx/html;** > document root directory.
- **server_name server.unixmen.local;** > Server FQDN.
Now, scroll down further and find the section #location **~ \.php$**. Uncomment and modify the following lines as shown below.
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;
}
Here, I added an extra line **try_files $uri =404;** to prevent zero day exploits.
Save and exit the file.
### Test nginx configuration ###
Test the nginx configuration for any syntax errors using command:
sudo nginx -t
Sample output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Finally restart nginx service
sudo service nginx restart
### Install MySQL ###
**MySQL** is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases, though SQLite probably has more total embedded deployments
sudo apt-get install mysql-server mysql-client
During installation, youll be asked to setup the MySQL root user password. Enter the password and click Ok.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_001.png)
Re-enter the password.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_0021.png)
Now, MySQL server has been installed.
You can verify the MySQL server status using command:
sudo service mysql status
Sample output:
mysql start/running, process 5671
**Note**: If you want to use MariaDB instead of MySQL, then follow the below steps.
### Install MariaDB ###
**MariaDB** is a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.
First you have to remove existing MySQL packages if any. To completely uninstall MySQL with configuration files, enter the following command:
sudo apt-get purge mysql*
Run the following command to remove unwanted packages.
sudo apt-get autoremove
After removing MySQL, run the following command to install MariaDB.
sudo apt-get install mariadb-server mariadb-client
Alternatively, you can install it from [MariaDB repository][1] if you want to try most recent version of MariaDB. Run the following commands to add PPA. As of writing this, MariaDB PPA is not yet updated to Ubuntu 14.10. However, we can use the repository of Ubuntu 14.04 instead.
sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://sgp1.mirrors.digitalocean.com/mariadb/repo/5.5/ubuntu trusty main'
Update the software sources list and install MariaDB using following commands:
sudo apt-get update
sudo apt-get install mariadb-server mariadb-client -y
During installation you will be asked to set database root user password.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_0031.png)
Re-enter password:
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_004.png)
Click Yes to migrate to MariaDB. Note that youll not be asked this question if you install MariaDB before MySQL.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_005.png)
You can check the MariaDB version using command:
sudo mysql -v -u root -p
Sample output:
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.
Check if mariadb is running or not, using the following command:
sudo service mysql status
Sample output:
* /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
### Install PHP ###
**PHP** (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open-source general purpose scripting language that is especially suited for web development and can be embedded into HTML.
Install PHP with following command:
sudo apt-get install php5 php5-fpm php5-mysql
### Configure PHP ###
Open **php.ini** file in any editor:
sudo nano /etc/php5/fpm/php.ini
Find the line cgi.fix_pathinfo=1, uncomment it and change the value 1 to 0.
cgi.fix_pathinfo=0
Now restart php-fpm service.
sudo service php5-fpm restart
### Test PHP ###
Create a sample “testphp.php” file in nginx document root folder.
sudo nano /usr/share/nginx/html/testphp.php
Add the following lines in it.
<?php
phpinfo();
?>
Save and exit the file.
Navigate to **http://server-ip-address/testphp.php**. It will display all the details about php such as version, build date and commands etc.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/10/phpinfo-Mozilla-Firefox_0012.png)
PHP-FPM listens on the socket **/var/run/php5-fpm.sock** by default. If you want to make PHP-FPM use a TCP connection, open the file **/etc/php5/fpm/pool.d/www.conf**,
sudo nano /etc/php5/fpm/pool.d/www.conf
Find the line listen = /var/run/php5-fpm.sock,
;listen = /var/run/php5-fpm.sock
and modify it to **listen = 127.0.0.1:9000**.
listen = 127.0.0.1:9000
Save and exit the file. Restart php5-fpm service.
sudo service php5-fpm restart
Now open the nginx configuration file:
sudo nano /etc/nginx/sites-available/default
Find the line **fastcgi_pass unix:/var/run/php5-fpm.sock;** and change it to fastcgi_pass 127.0.0.1:9000; as shown below.
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;
}
Save and exit the file. Finally restart nginx service.
sudo service nginx restart
### Manage MySQL Databases Using phpMyAdmin (Optional) ###
**phpMyAdmin** is a free open-source web interface tool used to manage your MySQL databases.
### Install phpMyAdmin ###
It is available in the Official Debian repositories. So install it with command:
sudo apt-get install phpmyadmin
Select any webserver. By default, nginx will not be displayed here. So, select apache or lighttpd, and we will link phpmyadmin to work with nginx webserver later.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_007.png)
Select Yes to configure database for phpmyadmin with dbconfig-common.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_008.png)
Enter password of the databases administrative user.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_009.png)
Enter MySQL application password phpmyadmin.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_010.png)
Re-enter the password.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_011.png)
The phpMyAdmin installation has been completed.
Create a symbolic link between phpMyAdmin and the website root directory. Here our website root document directory is /usr/share/nginx/html/.
sudo ln -s /usr/share/phpmyadmin/ /usr/share/nginx/html
Restart nginx server.
sudo service nginx restart
### Access phpMyAdmin Web Console ###
Now you can access the phpmyadmin console by navigating to **http://server-ip-address/phpmyadmin/** from your browser.
Enter your MySQL username and password which you have given in previous steps. In my case its “root” and “ubuntu”.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/10/phpMyAdmin-Mozilla-Firefox_0021.png)
You will be redirected to PhpMyAdmin main web interface.
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/10/192.168.1.100-localhost-phpMyAdmin-4.2.6deb1-Mozilla-Firefox_0031.png)
Now you can manage your MySQL databases from phpMyAdmin web interface.
Thats it. Your LEMP server is now up and ready to use.
--------------------------------------------------------------------------------
via: http://www.unixmen.com/install-lemp-server-nginx-mysql-mariadb-php-phpmyadmin-ubuntu-14-1014-0413-10/
作者:[SK][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://www.unixmen.com/author/sk/
[1]:https://downloads.mariadb.org/mariadb/repositories/#mirror=tsinghua

View File

@ -0,0 +1,348 @@
在Ubuntu 14.10/14.04/13.10上安装LEMP服务NginxMySQL或MariaDBPHP和phpMyAdmin
================================================================================
**LEMP**是一个操作系统和几个开源软件包的合称。缩写LEMP来自**L**inuxNginx发音是**e**ngine-xHTTP服务器**M**ySQL数据库和**P**HP/**P**erl/**P**ython的首字母。
在这篇教程里让我们看一下如何在Ubuntu 14.10上安装NginxMySQL或MariaDBPHP和phpMyAdmin。
### 安装Nginx ###
**Nginx** (发音是engine-x)是一个免费的、开源的、高性能HTTP服务器和反向代理也可以用作IMAP/POP3代理服务器由Igor Sysoev开发。
要安装Nginx在你的终端里输入下面的命令
**注意**如果你的系统里已经安装了apache2先卸载掉以避免冲突。要卸载apache运行下面的命令
sudo apt-get purge apache2*
sudo apt-get autoremove -y
现在用下面的命令安装nginx
sudo apt-get install nginx
用下面的命令启用Nginx服务
sudo service nginx start
### 测试nginx ###
打开你的浏览器访问http://IP地址/或者http://localhost/。将可以看到类似下面的截图。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/10/Welcome-to-nginx-Mozilla-Firefox_005.png)
### 配置Nginx ###
用任意文本编辑器打开文件**/etc/nginx/nginx.conf**
sudo nano /etc/nginx/nginx.conf
设置worker_processes例如你系统里CPU数目。查看CPU数目可以使用命令“lscpu”。在我这里是“1”。所以我把这个值设为1。
worker_processes 1;
重启Nginx服务
sudo service nginx restart
默认虚拟主机(服务器模块)定义在文件**/etc/nginx/sites-available/default**里。
用任意文本编辑器打开文件/etc/nginx/sites-available/default。
sudo nano /etc/nginx/sites-available/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配置是否存在语法错误
sudo nginx -t
典型输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
最后重启nginx服务
sudo service nginx restart
### 安装MySQL ###
**MySQL**是一个关系型数据库管理系统RDBMS作为服务启动提供给多用户访问多种数据库尽管可能大多集成布置的SQLite。
sudo apt-get install mysql-server mysql-client
在安装过程中会提示你设置MySQL超级用户密码。输入密码并按确认。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_001.png)
重新输入密码。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_0021.png)
现在MySQL服务器就安装好了。
你可以用下面的命令检查MySQL服务器状态
sudo service mysql status
典型输出:
mysql start/running, process 5671
**注意**如果你希望使用MariaDB而不是MySQL可以参考下面的步骤。
### 安装MariaDB ###
**MariaDB**是MySQL的一个直接替代软件。它是一个稳定的、可扩展的和可靠的SQL服务器包含许多增强功能。
首先如果有的话你得先卸载掉MySQL。要完全卸载MySQL包括配置文件输入如下命令
sudo apt-get purge mysql*
运行如下命令清除不需要的软件包。
sudo apt-get autoremove
在卸载完MySQL后运行如下命令安装MariaDB。
sudo apt-get install mariadb-server mariadb-client
另外如果你希望体验最新版的MariaDB的话可以从[MariaDB仓库][1]安装。运行下面的命令添加PPA。在写这篇文章的时候MariaDB PPA还没有更新Ubuntu 14.10。不过我们还是可以使用Ubuntu 14.04的仓库。
sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://sgp1.mirrors.digitalocean.com/mariadb/repo/5.5/ubuntu trusty main'
用如下命令更新一下软件源列表然后安装MariaDB
sudo apt-get update
sudo apt-get install mariadb-server mariadb-client -y
在安装过程中会提示你设置数据库root用户密码。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_0031.png)
重新输入一次密码:
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_004.png)
点击迁移到MariaDB。注意一下如果在安装MariaDB之前没有装过MySQL的话不会提示你这个问题。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_005.png)
你可以用如下命令检查MariaDB版本
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是否已经开始运行
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
### 安装PHP ###
**PHP**PHP: Hypertext Preprocessor的递归缩写是一个应用广泛的开源通用脚本语言特别适合于网页开发可以直接嵌入到HTML中。
使用如下命令安装PHP
sudo apt-get install php5 php5-fpm php5-mysql
### 配置PHP ###
用任意文本编辑器打开**php.ini**文件:
sudo nano /etc/php5/fpm/php.ini
找到这一行cgi.fix_pathinfo=1去掉注释并把值1改为0。
cgi.fix_pathinfo=0
现在重启php-fpm服务。
sudo service php5-fpm restart
### 测试PHP ###
在nginx文档根目录下创建一个测试文件“testphp.php”。
sudo nano /usr/share/nginx/html/testphp.php
加入下面几行。
<?php
phpinfo();
?>
保存文件并退出。
访问地址**http://server-ip-address/testphp.php**。将打印出所有关于php的信息比如版本、构建日期以及命令等等。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/10/phpinfo-Mozilla-Firefox_0012.png)
PHP-FPM会默认监听套接字**/var/run/php5-fpm.sock**。如果你希望PHP-FPM使用TCP连接打开文件**/etc/php5/fpm/pool.d/www.conf**
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服务。
sudo service php5-fpm restart
现在打开nginx配置文件
sudo nano /etc/nginx/sites-available/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服务。
sudo service nginx restart
### 使用phpMyAdmin管理MySQL数据库可选 ###
**phpMyAdmin**是一个免费的开源网页界面工具用来管理你的MySQL数据库。
### 安装phpMyAdmin ###
在Debian官方仓库里就有。所以可以用下面的命令安装
sudo apt-get install phpmyadmin
选择一个网页服务器。默认情况下这里不会显示nginx。所以选择apache或者lighttpd然后我们再把phpMyAdmin和nginx连接起来工作。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_007.png)
选择通过dbconfig-common为phpMyAdmin配置数据库。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_008.png)
输入数据库的管理员账号密码。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_009.png)
输入phpmyadmin帐号的MySQL密码
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_010.png)
重新输入一次密码。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/06/sk@server-_011.png)
phpMyAdmin的安装就完成了。
创建一个phpMyAdmin的软连接到网站根目录。这里我们的网站跟文档目录是/usr/share/nginx/html/。
sudo ln -s /usr/share/phpmyadmin/ /usr/share/nginx/html
重启nginx服务。
sudo service nginx restart
### 访问phpMyAdmin网页控制台 ###
现在你可以在浏览器中通过地址**http://server-ip-address/phpmyadmin/**访问phpMyAdmin的控制台了。
输入你在前面步骤里留下的MySQL用户名和密码。在我这里是“root”和“ubuntu”。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/10/phpMyAdmin-Mozilla-Firefox_0021.png)
就可以重定向到phpMyAdmin的网页管理首页。
![](http://180016988.r.cdn77.net/wp-content/uploads/2014/10/192.168.1.100-localhost-phpMyAdmin-4.2.6deb1-Mozilla-Firefox_0031.png)
现在你就可以在phpMyAdmin网页里管理你的MyQL数据库了。
就这样。你的LEMP服务器已经配置完毕可以投入使用了。
--------------------------------------------------------------------------------
via: http://www.unixmen.com/install-lemp-server-nginx-mysql-mariadb-php-phpmyadmin-ubuntu-14-1014-0413-10/
作者:[SK][a]
译者:[zpl1025](https://github.com/zpl1025)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[a]:http://www.unixmen.com/author/sk/
[1]:https://downloads.mariadb.org/mariadb/repositories/#mirror=tsinghua