预览:

Installing Nginx with PHP 7 and MySQL 5.7 (LEMP) on Ubuntu 16.04 LTS

This tutorial exists for these OS versions

  • Ubuntu 15.10 (Wily Werewolf)

  • Ubuntu 14.04 LTS (Trusty Tahr)

  • Ubuntu 13.04 (Raring Ringtail)

  • Ubuntu 12.10 (Quantal Quetzal)

  • Ubuntu 12.04 LTS (Precise Pangolin)

On this page

  1. 1 Preliminary Note

  2. 2 Installing MySQL 5.7

  3. 3 Installing Nginx

  4. 4 Installing PHP 7

  5. 5 Configuring nginx

  6. 6 Getting MySQL Support In PHP 7

  7. 7 Making PHP-FPM use a TCP Connection

  8. 8 Links

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx on an Ubuntu 16.04 server with PHP 7 support (through PHP-FPM) and MySQL 5.7 support (LEMP = Linux + nginx (pronounced "engine x") + MySQL + PHP).

1 Preliminary Note

In this tutorial, I use the hostname server1.example.com with the IP address192.168.1.100. These settings might differ for you, so you have to replace them where appropriate.

I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:

sudo -s

2 Installing MySQL 5.7

In order to install MySQL, we run:

apt-get -y install mysql-server mysql-client

You will be asked to provide a password for the MySQL root user - this password is valid for the user root@localhost as well as root@server1.example.com, so we don't have to specify a MySQL root password manually later on:

New password for the MySQL "root" user: <-- yourrootsqlpassword
Repeat password for the MySQL "root" user: <-- yourrootsqlpassword

To secure the database server and remove  the anonymous user and test database, run the mysql_secure_installation command.

mysql_secure_installation

You will be asked these questions:

root@server1:~# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: <-- Enter the MySQL root password

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: <-- Press y if you want this function or press Enter otherwise.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : <-- Press enter

... 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? (Press y|Y for Yes, any other key for No) : <-- 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? (Press y|Y for Yes, any other key for No) : <-- 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? (Press y|Y for Yes, any other key for No) : <-- y
- Dropping test database...
Success.

- 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? (Press y|Y for Yes, any other key for No) : <-- y
Success.

All done!

MySQL is secured now.

3 Installing Nginx

In case that you have installed Apache2 already, then remove it first with these commands & then install nginx:

service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2

Nginx is available as a package for Ubuntu 16.04 which we can install.

apt-get -y install nginx

Start nginx afterwards:

service nginx start

Type in your web server's IP address or hostname into a browser (e.g. http://192.168.1.100), and you should see the following page:

The default nginx document root on Ubuntu 16.04 is /var/www/html.

4 Installing PHP 7

We can make PHP work in nginx through PHP-FPM (PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites) which we install as follows:

apt-get -y install php7.0-fpm

PHP-FPM is a daemon process (with the init script php7.0-fpm) that runs a FastCGI server on the socket /run/php/php7.0-fpm.sock.

5 Configuring nginx

The nginx configuration is in /etc/nginx/nginx.conf which we open now:

nano /etc/nginx/nginx.conf

The configuration is easy to understand (you can learn more about it here: http://wiki.nginx.org/NginxFullExample and here:http://wiki.nginx.org/NginxFullExample2)

First (this is optional) adjust the keepalive_timeout to a reasonable value:

[...]keepalive_timeout   2;
[...]

The virtual hosts are defined in server {} containers. The default vhost is defined in the file /etc/nginx/sites-available/default - let's modify it as follows:

nano /etc/nginx/sites-available/default

[...]
server {listen 80 default_server;listen [::]:80 default_server;# SSL configuration## listen 443 ssl default_server;# listen [::]:443 ssl default_server;## Note: You should disable gzip for SSL traffic.# See: https://bugs.debian.org/773332## Read up on ssl_ciphers to ensure a secure configuration.# See: https://bugs.debian.org/765782## Self signed certs generated by the ssl-cert package# Don't use them in a production server!## include snippets/snakeoil.conf;root /var/www/html;# Add index.php to the list if you are using PHPindex index.html index.htm index.nginx-debian.html;server_name _;location / {# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files $uri $uri/ =404;}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~ \.php$ {include snippets/fastcgi-php.conf;# With php7.0-cgi alone:# fastcgi_pass 127.0.0.1:9000;# With php7.0-fpm:fastcgi_pass unix:/run/php/php7.0-fpm.sock;}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one#location ~ /\.ht {deny all;}
}
[...]

server_name _; makes this a default catchall vhost (of course, you can as well specify a hostname here like www.example.com).

root /var/www/html; means that the document root is the directory /var/www/html.

The important part for PHP is the location ~ \.php$ {} stanza. Uncomment it to enable it.

Now save the file and reload nginx:

service nginx reload

Next open /etc/php/7.0/fpm/php.ini...

nano /etc/php/7.0/fpm/php.ini

... and set cgi.fix_pathinfo=0:

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

Reload PHP-FPM:

service php7.0-fpm reload

Now create the following PHP file in the document root /var/www/html:

nano /var/www/html/info.php

<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.1.100/info.php):

As you see, PHP 7 is working, and it's working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP. MySQL is not listed there which means we don't have MySQL support in PHP yet.

6 Getting MySQL Support In PHP 7

To get MySQL support in PHP, we can install the php7.0-mysql package. It's a good idea to install some other PHP modules as well as you might need them for your applications. You can search for available PHP modules like this:

apt-cache search php7.0

Pick the ones you need and install them like this:

apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache  php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext

APCu is an extension for the PHP Opcache module that comes with PHP 7, it adds some compatibility features for software that supports the APC cache (e.g. Wordpress cache plugins).

APCu can be installed as follows:

apt-get -y install php-apcu

Now reload PHP-FPM:

service php7.0-fpm reload

Now reload http://192.168.1.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:

7 Making PHP-FPM use a TCP Connection

By default PHP-FPM is listening on the socket /var/run/php/php7.0-fpm.sock. It is also possible to make PHP-FPM use a TCP connection. To do this, open /etc/php/7.0/fpm/pool.d/www.conf...

nano /etc/php/7.0/fpm/pool.d/www.conf

... and make the listen line look as follows:

[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]

This will make PHP-FPM listen on port 9000 on the IP 127.0.0.1 (localhost). Make sure you use a port that is not in use on your system.

Then reload PHP-FPM:

php7.0-fpm reload

Next go through your nginx configuration and all your vhosts and change the line fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; tofastcgi_pass 127.0.0.1:9000;, e.g. like this:

nano /etc/nginx/sites-available/default

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

Finally, reload nginx:

service nginx reload

That's it. The Nginx LEMP server is installed.

8 Links

  • nginx: http://nginx.net/

  • nginx Wiki: http://wiki.codemongers.com/Main

  • PHP: http://www.php.net/

  • PHP-FPM: http://php-fpm.org/

  • MySQL: http://www.mysql.com/

  • Ubuntu: http://www.ubuntu.com/

转载于:https://blog.51cto.com/caochun/1783795

LNMP/LEMP(PHP7.0.04+mysql5.7.12+nginx1.10.0)相关推荐

  1. CentOS7.0下编译安装Nginx 1.10.0

    2019独角兽企业重金招聘Python工程师标准>>> 准备工作 安装编译工具.依赖包 $ yum -y install gcc gcc-c++ autoconf automake ...

  2. FL Studio 10.0.9 水果音乐制作软件 10.0.9

    FL Studio 10.0.9  水果音乐制作软件 10.0.9 FL Studio是知名的音乐制作软件.它让你的计算机就像是全功能的录音室,漂亮的大混音盘,先进的创作工具,让你的音乐突破想象力的限 ...

  3. centos6.5 nginx1.8 php mysql,CentOS6.5 源码安装Nginx1.8 + PHP7.0.6 + MySQL5.7.12

    准备工作 使用yum安装相关依赖 yum install -y gcc gcc-c++ autoconf automake libtool make cmake libjpeg libjpeg-dev ...

  4. 编译安装LNMP Centos 6.5 x64(6.6 x64) + Nginx1.6.0 + PHP5.5.13 + Mysql5.6.19

    环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G) 系统版本:CentOS-6.5-x86_64-minimal.iso 安装步骤: 0.虚拟系统安装 0.1 使用VMwa ...

  5. 抓取从源地址为10.0.0.111主机访问目标主机10.0.0.222的80/tcp端口的流量?

    tcpdump tcp port 80 -nn icmp and src host 10.0.0.111 and dst host 10.0.0.222

  6. Flutter Channel stable, 0.0.0-unknown, on Microsoft Windows [Version 10.0.18362.1016]

    重装以及 到flutter的安装目录执行 git clean -xfd git stash save --keep-index git stash drop git pull flutter doct ...

  7. mysql 5.6 64 位安装 缺少libai.so_CentOS6.7安装部署LNMP(nginx1.8.0+php5.6.10+mysql5.6.12) 法外狂徒...

    IP-10.0.0.8 1.安装nginx mkdir -p /server/tools cd /server/tools yum install -y pcre pcre-devel openssl ...

  8. Ubuntu18.04+RTX 2080Ti+CUDA 10.0 +cuDNN+PyTorch搭建深度学习环境

    SSH无密码登陆 安装SSH Server Ubuntu 默认已安装了 SSH client,此外还需要安装 SSH server: sudo apt-get install openssh-serv ...

  9. 【WebLogic】Windows系统下WebLogic 12.1.3.0的安装和补丁

    Oracle WebLogic 12.1.3.0是Oracle发布的WebLogic 12c的早期版本,目前已经停止补丁技术支持. 下载地址: https://www.oracle.com/middl ...

最新文章

  1. go语言csv包_golang 写入 csv 文件
  2. 18. 编写FTP客户端程序
  3. linux自动执行top,Linux top 命令使用
  4. mysql join on 索引_连接查询,表关联查询join on,索引,触发器,视图
  5. 【独家】手环新玩法,北京一卡通推出“刷刷手环”每天5000步每月返10元
  6. Android Animation动画(很详细)
  7. linux源码_从linux源码看epoll及epoll实战揭秘
  8. 苹果mac矢量图形设计软件:Illustrator
  9. JavaScript比较是否在某时间段内
  10. 推荐几款好用的CRM
  11. 为什么要在信号线上串联一些小电阻
  12. 南阳oj入门题-兰州烧饼
  13. ASP.NET负压测试
  14. 【CC0素材网站大全】100个国外高品质免费可商用CC0的图片素材网站!包含大量摄影素材、创意素材、壁纸素材、图标网站等...
  15. 1688-cat_get - 获得1688商品分类
  16. Excel 和 python 使用梯度下降法分别求【极小值点】【线性回归问题】
  17. 抖音运营技巧都有哪些?如何让爆粉?
  18. Lightswitch中使用LINQ
  19. 王者荣耀头像大小怎么调?调整图片尺寸大小工具分享
  20. ipynb转化py文件

热门文章

  1. java比较时间的先后utc时间,日期、时间与UTC
  2. 2020笔记本性价比之王_什么笔记本性价比高?2020性价比最高的笔记本电脑
  3. 整活插件 炉石传说_酒馆战旗整活插件 免安装版
  4. ubuntu 自动加载ko_linux驱动模块开机自动加载,以及应用程序开机自启动
  5. 分布式光伏补贴_四川:2020年起工商业分布式光伏已无补贴
  6. grub linux rootfs,rootfs文件系统(笔记)(草稿)
  7. mysql dump工具升级_MySQL数据库升级
  8. full gc JAVA_java触发full gc的几种情况概述
  9. asp绑定gridview属性_如何在ASP.NET Core中自定义Azure Storage File Provider
  10. 《软件需求分析(第二版)》第 2 章——客户眼中的需求 重点部分总结