Ubuntu 14.04 安装 Nginx PHP5 MySQL

原创 2016年08月29日 18:05:15

Nginx Installation

Nginx is one of the robust web server in Linux world. Nginx is a free, open source, high performance HTTP server and reverse proxy, as weell as an IMAP/POP3 proxy server. Now, we are going to install Nginx web server.

First, make sure system is upto date:
$ sudo apt-get update
$ sudo apt-get upgrade

#1 - Download and Install Nginx

The easiest way to download and install Nginx is using apt-get command. Here is the command:
$ sudo apt-get install nginx

Fig.01: Download and Install Nginx on Ubuntu Linux

Fig.01: Download and Install Nginx on Ubuntu Linux


Just wait until installation is get done on the system.

#2 - Test Nginx

Once it get done, you can open your browser and type url http://localhost or http://your_ip_address to test it. If everything goes normal, you will see Nginx welcome page:

Fig.02: Welcome nginx page on Ubuntu Linux

Fig.02: Welcome nginx page on Ubuntu Linux

MySQL Installation On Ubuntu

MySQL is one of the most powerful database management system in Linux world. Next, we are going to install it with PHP support.

#1 - Install MySQL and PHP support

Type the following command:
$ sudo apt-get install mysql-server php5-mysql

Fig.03: Ubuntu Linux Install MySQL to Manage Site Data with PHP

Fig.03: Ubuntu Linux Install MySQL to Manage Site Data with PHP


During the installation, MySQL will ask you to enter MySQL root password, other wise the installation will not continue. Type the password you want, then press OK to continue the installation.

#2 - Test MySQL

Once mysql installation finished, we can test it. Open your console and type the following command:
$ mysql -u root -p

Fig.04: Ubuntu test Mysql installation

Fig.04: Ubuntu test Mysql installation


If you type your password correctly, the you will see the mysql prompt.

PHP Installation For Server Side Scripting

Since PHP is popular, a lot of websites is built using PHP language. As of January 2013, PHP was installed on more than 240 millions websites. Now we are going to install PHP on Ubuntu 14.04

#1 - Download and install PHP

As usual, we can download and install PHP using apt-get command. Just type the following command on your Ubuntu console or over the ssh based session:
$ sudo apt-get install php5-fpm

Fig.12: Install PHP for Server Side Processing on Ubuntu

Fig.12: Install PHP for Server Side Processing on Ubuntu


And wait for the installation to complete.

Configure Nginx to work with PHP and MySQL Server on Ubuntu

Now we have all components installed. The next step is we need to configure Nginx with PHP and MySQL. Let's start to configure them.

#1 - Configure PHP5-FPM

PHP5-FPM configuration file is located at /etc/php5/fpm/php.ini. Open it with your text editor
$ sudo vi /etc/php5/fpm/php.ini
Change this parameter, from:
cgi.fix_pathinfo=1
to:
cgi.fix_pathinfo=0
Save and close the file and then restart php5-fpm service, type:
$ sudo service php5-fpm restart

#2 - Configure Nginx

Nginx configuration file is located at /etc/nginx/nginx.conf. But basically, we don't need to touch it. The configuration of nginx website is located in /etc/nginx/sites-available/default file.

解决nginx打开php文件总是显示下载框的问题

ps: 不同版本路径可能有变:如下 nginx 配置路径 
sudo vi /etc/nginx/sites-available/default

Open it, and uncomment lines:

ubuntu14.04 :

 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;
 }

ubuntu15.04 :

 location ~ \.php$ {
         include snippets/fastcgi-php.conf;
	 # With php5-cgi alone:
         # fastcgi_pass 127.0.0.1:9000;
         # With php5-fpm:
         fastcgi_pass unix:/var/run/php5-fpm.sock;
 }

Then restart the services.
$ sudo service nginx restart
Please make sure, on Ubuntu 14.04, we cannot use fastcgi_pass 127.0.0.1:9000. So make sure that the line is commented. Otherwise, on my machine, I got this error :
$ tail /var/log/nginx/error.log
2014/11/04 16:57:04 [emerg] 3216#0: "fastcgi_pass" directive is not allowed here in /etc/nginx/sites-enabled/default:59
2014/11/04 16:58:21 [emerg] 3256#0: "fastcgi_pass" directive is not allowed here in /etc/nginx/sites-enabled/default:59

On Ubuntu 14.04, I use the following line:
fastcgi_pass unix:/var/run/php5-fpm.sock
When I restart the Nginx and I still got the following error message:

2014/11/04 17:02:05 [emerg] 3295#0: "fastcgi_pass" directive is not allowed here in /etc/nginx/sites-enabled/default:61
2014/11/04 17:04:58 [emerg] 3410#0: "fastcgi_pass" directive is not allowed here in /etc/nginx/sites-enabled/default:61

Please make sure that you already uncomment this line:
location ~ \.php$ {

#3 - Configure MySQL

After the configuration section is done, now we need to test them to make sure that our configuration is working as required. On Ubuntu 14.04 the root document folder is located in /usr/share/nginx/html. So create a file called /usr/share/nginx/html/phpinfo.php with the following code:

 <?phpinfo();?> 

After restarting PHP-FPM and Nginx, open the browser and browse to the php file, we got only a blank screen. No error message on the screen. No error message on PHP-FPM and Nginx log file.

Then we changed /usr/share/nginx/html/phpinfo.php file code on the root document of Nginx, as follows:

 <?phpphpinfo();?> 

And then open the browser again and type url http://your_ip_address/phpinfo.php
php5_configuration
It seem that by default, we can not use short php-tag like this:

 <? ... your code ...?> 

To enable short php tag, we need to change the value of short_open_tag parameter on php.ini file:
php-fpm_short_open_tag
Change the value from Off to On. Then restart your php5-fpm :
sudo service php5-fpm restart
Then try again to test your phpinfo file. Next, we will see if the MySQL support is enabled or not. Scroll down the php configuration screen on your browser, if you see MySQL block there, then MySQL support already enabled.
mysql_php5_enabled
You are now ready to use Nginx, PHP5 and MySQL on Ubuntu server. I hope this quick article help anyone who wish to install Linux, Nginx, PHP and MySQL on Ubuntu 14.04.

ubuntu 14.04服务器上使用nginx搭建wordpress博客详解

Ubuntu 14.04服务器上使用nginx搭建WordPress博客详解。
  • YhL_Leo
  • YhL_Leo
  • 2016年01月14日 23:28
  • 4518

ubuntu离线安装mysql

测试环境:ubuntu12.04-amd64 Mysql离线包:mysql-5.5.25-linux2.6-x86_64.tar.gz 官方安装步骤如下: 注...
  • futudeniaodan
  • futudeniaodan
  • 2016年02月25日 20:19
  • 413

阿里云主机Ubuntu 14.04下安装php5.5.9+mysql+Apache配置多主机

平时用Ubuntu 14.04系统用的非常多,经常配置环境,但是因为自己的记性不太好,每次配置都还需要百度,所以整理好配置过程发到自己博客,下次需要的时候,直接拿出来看。首先可以使用命令更新一下软件源...
  • u011120720
  • u011120720
  • 2016年04月08日 11:21
  • 1211

如何在Ubuntu上安装Apache,MySQL,PHP,Nginx,HAProxy,以及如何在docker上安装LAMP

如何在Ubuntu上安装Apache,MySQL,PHP,Nginx,HAProxy,以及如何在docker上安装LAMP
  • yangzhenping
  • yangzhenping
  • 2015年02月09日 15:10
  • 4353

ubuntu 14.04中安装phpmyadmin即mysql图形管理界面

由于学习的需要,我将网站开发环境从windows转移到了ubuntu,ubuntu下之前并没有发现什么难的地方,只要百度一般都有解决方案。但是总所周知ubuntu是一系列开源软件的集合,由于版本的问题...
  • kingszelda
  • kingszelda
  • 2014年08月24日 10:25
  • 3474

ubuntu下的mysql5.7主从复制配置

环境: 宿主机系统:windows7 虚拟机:Oracle VMVirtualBox Linux: ubuntukylin-14.04.1-amd64.iso mysql:5.7.* 虚拟主机2台 ...
  • lanonola
  • lanonola
  • 2016年09月14日 11:17
  • 1235

Ubuntu 14 下使用离线包安装mysql

Ubuntu14 下安装mysql最新没事想在Ubuntu上搞个mysql玩玩,本人最习惯使用的Linux系统是Ubuntu,但是发现,在mysql官网下载的老版本的(5.6之前)mysql没有提供对...
  • qiuxiao630320
  • qiuxiao630320
  • 2015年10月15日 11:42
  • 2047

ubuntu 14.04 server 下安装ZABBIX 及增加客户机配置过程

#首先安装apache、php5、mysql 这个几个软件(在已经默认装好的机器上没有配过,改天尝试一下) #apt-get 方式安装起来方便是方便就是没有办法人为控制 这点比较 su...
  • oushengm
  • oushengm
  • 2015年07月13日 14:59
  • 2184

Ubuntu 14.04中文输入法的安装

Ubuntu默认自带的中文输入法是IBUS框架的ibus-pinyin,IBUS-Bopomofo等。对于习惯于搜狗,紫光华宇,谷歌拼音的我们可能有点使用不习惯。下面就是安装常用的IBUS中文输入法。...
  • maverick1990
  • maverick1990
  • 2014年11月21日 16:56
  • 2093

https搭建:ubuntu nginx配置 SSL证书

一、HTTPS 是什么?根据维基百科的解释:超文本传输安全协议(缩写:HTTPS,英语:Hypertext Transfer Protocol Secure)是超文本传输协议和SSL/TLS的组合,用...
  • hanshileiai
  • hanshileiai
  • 2017年01月10日 20:05
  • 2437
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Ubuntu 14.04 安装 Nginx PHP5 MySQL
举报原因:
原因补充:

(最多只允许输入30个字)