欢迎来到梦飞科技

服务器租用

当前优惠活动:

Ubuntu 16.04 LTS系统安装配置Nginx/PHP 7/MySQL 5.7环境

Nginx (读”engine x”) 是一款免费、开源的高机能 HTTP 处事。Nginx 不变、富厚的成果集、设置简朴、资源耗损低。本教程先容了如何通过PHP7支持(通过PHP-FPM)和MySQL5.7支持(LEMP= LINUX + nginx(发音为“engine x”)+ MySQL+ PHP)在Ubuntu 16.04处事器上安装Nginx处事器。

1 劈头说明

在本教程中,我利用的IP 地点192.168.1.100,主机名server1.example.com。这些配置大概与你的差异,所以你不得不在适当环境下改换他们。

我运行的所有步调在本教程中利用root权限,所以必然要确保你以root身份登录:

2 安装 MySQL 5.7

安装 MySQL 运行呼吁:

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

你会被要求提供MySQL的root用户暗码 :

New password for the MySQL “root” user: <– yourrootsqlpassword
Repeat password for the MySQL “root” user: <– yourrootsqlpassword

Ubuntu 16.04 LTS系统安装设置Nginx/PHP 7/MySQL 5.7情况

为了确保数据库处事器,并删除匿名用户和测试数据库,运行mysql_secure_installation呼吁。

mysql_secure_installation

你会问这些问题:

[email protected]:~# mysql_secure_installation

掩护MySQL处事器陈设。

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 安装 Nginx

在你已经安装了Apache2的话,那么利用这些呼吁先删除再安装nginx:

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

Ubuntu16.04有Nginx安装包,我们可以安装。

apt-get -y install nginx

Start nginx afterwards:

service nginx start

输入您的Web处事器的IP地点或主机名到欣赏器(譬喻http://192.168.1.100),你应该看到如下页面:

Ubuntu 16.04 LTS系统安装设置Nginx/PHP 7/MySQL 5.7情况

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

4 安装 PHP 7

我们可以通过使nginx的PHP事情PHP-FPM(PHP-FPM(FastCGI历程打点器)是为任何局限的网站,尤其是忙碌的网站有用的一些附加成果的替代PHP的FastCGI实现),我们安装如下:

apt-get -y install php7.0-fpm

5 设置 nginx

打开设置文件 /etc/nginx/nginx.conf:

nano /etc/nginx/nginx.conf

设置是很容易领略 (你可以点击官方教程: http://wiki.nginx.org/NginxFullExample 或:http://wiki.nginx.org/NginxFullExample2)

首先(这是可选)调解keepalive_timeout到一个公道的值:

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

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

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

service nginx reload

下一步打开 /etc/php/7.0/fpm/php.ini…

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

配置 cgi.fix_pathinfo=0:

梦飞科技 - 全球数据中心基础服务领先供应商

Copyright © 2003-2019 MFISP.COM. 国外服务器租用 IDC公司 版权所有 粤ICP备11019662号