Ubuntu 16.04 LTS (LEMP) 安装 Nginx/PHP 7/MySQL 5.7

原创 2017年01月03日 22:37:06
1、基本说明
我所安装的环境是ubuntu 16.04 LTS,安装nginx、PHP 7.0和MySQL,搭建Nginx服务器,后面我会加上php0-redis的扩展,在此仅供参考;我的IP地址是192.168.1.101,请大家稍微注意一下就行;我在root权限下面运行的,省去了每次sudo命令;

2、安装MySQL 5.7

运行命令:

apt-get -y install mysql-server mysql-client
按照提示输入设置密码

为了确保数据库服务器,并删除匿名用户和测试数据库,运行mysql_secure_installation命令。

mysql_secure_installation

root@cecotw-VirtualBox:/# mysql_secure_installation


Securing the MySQL server deployment.


Enter password for user root: 


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: Y


There are three levels of password validation policy:


LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file


Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: ^C
root@cecotw-VirtualBox:/# mysql_secure_installation


Securing the MySQL server deployment.


Enter password for user root: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.


Estimated strength of the password: 25 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 回车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!


3、安装Nginx

在你已经安装了Apache2的话,那么使用这些命令先删除再安装nginx:

service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2
Ubuntu16.04有Nginx安装包,我们可以安装。
apt-get -y install nginx
service nginx start
输入您的Web服务器的IP地址或主机名到浏览器(例如http://192.168.1.101),你应该看到如下页面:


在Ubuntu16.04的默认nginx的文档根目录为/var/www/html

4、安装PHP7

我们可以通过使nginx的PHP工作PHP-FPM(PHP-FPM(FastCGI进程管理器)是为任何规模的网站,尤其是繁忙的网站有用的一些附加功能的替代PHP的FastCGI实现),命令如下:

apt-get -y install php7.0-fpm
5、配置nginx

首先打开配置文件:

vim /etc/nginx/nginx.conf

调整keepalive_timeout到一个合理的值:(可以调节也可以不设定)

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

虚拟主机服务器{}容器定义。默认的虚拟主机是在文件中定义的/etc/nginx/sites-available/default – 让我们来修改它,如下所示:

vim /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 PHP
 index 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 _; 使这是一个默认捕捉所有虚拟主机(当然,你可以同时喜欢这里www.example.com指定主机名)。

根目录 /var/www/html;意味着文档根目录/var/www/html.

PHP的重要组成部分位置 ~ \.php$ {} stanza. 取消注释它来启用它。

现在保存文件并重新加载nginx:

nginx -s reload

配置php文件

vim /etc/php/7.0/fpm/php.ini

设置 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
[...]

重新加载 PHP-FPM:

service php7.0-fpm reload
建立php文件

vim /var/www/html/01.php
<?php
phpinfo();
?>

浏览器访问:http://192.168.1.101/01.php


6、使得MySQL获得PHP7支持

搜索PHP支持的模块:

apt-cache search php7.0
使用下面的命令安装php模块

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是随PHP7 PHP Opcache模块的扩展,它增加了一些兼容性功能的支持APC缓存(例如WordPress的插件缓存)软件。

APCu可以安装如下:

apt-get -y install php-apcu

重新加载 PHP-FPM:

service php7.0-fpm reload
7、使得PHP-FPM使用TCP链接

默认情况下PHP-FPM监听 /var/run/php/php7.0-fpm.sock. 另外,也可以使 PHP-FPM 试用 TCP 连接,打开文件 /etc/php/7.0/fpm/pool.d/www.conf

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

修改如下:

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

这将使PHP-FPM端口9000侦听的IP127.0.0.1(本地主机)。请确保您使用的端口,是不是在你的系统上使用。

然后重新加载 PHP-FPM:

service php7.0-fpm reload

接下来通过你的nginx的配置和所有的虚拟主机,并更改fastcgi_pass UNIX行:/var/run/php/php7.0-fpm.sock; tofastcgi_pass127.0.0.1:9000;,如下:

vim /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;
 }
[...]

最后,重新加载nginx:

service nginx reload

在此,Nginx的LEMP服务器安装完毕。

8、安装PHP 7.0 Redis扩展

git clone -b php7 https://github.com/phpredis/phpredis.git
mv phpredis/ /etc/
cd /etc/phpredis
apt install php7.0-dev
phpize
./configure
make && make install
vim /etc/php/7.0/fpm/php.ini
加入 extension=/etc/phpredis/modules/redis.so
重启php7.0-fpm环境
service php7.0-fpm reload

在此,我们已经安装完毕。



















版权声明:本文为博主原创文章,未经博主允许不得转载。

Ubuntu环境编译安装Nginx+PHP7

已经在 Mac 上配置过 Nginx+PHP7的环境了Mac环境配置Nginx+PHP7 ,在 Ubuntu环境下大致相同,但是遇到一些不一样的问题安装Nginx./configure 按照提示,找到...
  • wozaixiaoximen
  • wozaixiaoximen
  • 2017年02月15日 11:52
  • 1267

Ubuntu编译安装PHP和Nginx

编译安装nginx切换目录到工作文件夹:cd /usr/local/src 下载pcre源代码并安装 wget ftp://ftp.csx.cam.ac.uk/pub/software/program...
  • koastal
  • koastal
  • 2016年04月01日 17:24
  • 758

Ubuntu16.04编译PHP7.0,并开发PHP扩展

准备先下载PHP源文件使用: wget https://downloads.php.net/~ab/php-7.0.11RC1.tar.bz2下载完成后解压: tar jxf php-7.0.11...
  • u014359108
  • u014359108
  • 2016年09月13日 10:50
  • 2870

如何在Ubuntu上安装Linux,Apache,MySQL,PHP(LAMP)

本教程解释如何安装Apache,如何安装MySQL,如何更改根MySQL密码,如何安装PHP,以及如何查看可用的库。 LAMP是一组用于获取Web服务器和ru的开源软件...
  • hanshileiai
  • hanshileiai
  • 2017年01月16日 16:23
  • 1042

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

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

Mac下使用brew搭建PHP7+nginx+mysql开发环境

由于新换了个macbook,原来的mac上的环境搭建也已经很久没搭建过了,于是重新整理一下搭建过程。 Mac下自带了php,但是最近想新尝试一下php7的相关特性,于是就重新搭建个php吧。mac下...
  • mysteryhaohao
  • mysteryhaohao
  • 2016年08月17日 14:42
  • 5959

Ubuntu 16.04 LAMP(PHP7.0) 环境搭建并测试

#PS:要转载请注明出处,本人版权所有#PS:这个只是 《 我自己 》理解,如果和你的 #原则相冲突,请谅解,勿喷安装环境: Linux 4.4.0-31-generic #50-...
  • u011728480
  • u011728480
  • 2016年08月26日 16:58
  • 8041

Ubuntu-16.04 三步搞定Nginx安装

Ubuntu-16.04 三步搞定Nginx安装租了服务器,开始搞事情。然而,网上现有的教程实在不忍直视。因此写点东西留给后面的童鞋。环境是阿里云Ubuntu16.04 32bit,其他发行版本请移...
  • chuanlong99
  • chuanlong99
  • 2017年08月08日 18:44
  • 196

Linux下Nginx+PHP+Mysql环境搭建过程(图文)

转自:http://junstar.blog.51cto.com/4551565/1677140 一.使用yum命令,安装所需的程序库         1.命令内容           ...
  • long712905
  • long712905
  • 2016年10月28日 15:49
  • 1095

阿里云ECS服务器搭建Nginx+PHP+MySql+Redis环境详细步骤(CentOS7环境)

一、安装Nginx 1、下载nginx-release包 wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent...
  • kenkao
  • kenkao
  • 2016年11月25日 14:27
  • 3306
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Ubuntu 16.04 LTS (LEMP) 安装 Nginx/PHP 7/MySQL 5.7
举报原因:
原因补充:

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