Nginx(读”engine x”) 是一款免费、开源的高性能 HTTP 服务。Nginx 稳定、丰富的功能集、配置简单、资源消耗低。本教程介绍了如何通过PHP7支持(通过PHP-FPM)和MysqL5.7支持(LEMP= LINUX + Nginx(发音为“engine x”)+ MysqL+ PHP)在Ubuntu16.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
New password for the MysqL “root” user:<– yourrootsqlpassword
Repeat password for the MysqL “root” user:<– yourrootsqlpassword
为了确保数据库服务器,并删除匿名用户和测试数据库,运行MysqL_secure_installation命令。
MysqL_secure_installation
期间会被问及一些问题:
root@server1:~# MysqL_secure_installation
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,51); text-align:justify"> 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,51); text-align:justify"> 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
启动ngninx:
service Nginx start
输入您的Web服务器的IP地址或主机名到浏览器(例如http://192.168.1.100),你应该看到如下页面:
Ubuntu16.04的默认Nginx的文档根目录为/var/www/html
4 安装 PHP 7
安装如下:
apt-get -y install PHP7.0-fpm
5 配置 Nginx
配置很容易理解 (你可以点击官方教程: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. 取消注释它来启用它。
service Nginx reload
下一步打开/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
建立探针文件/var/www/html:
nano /var/www/html/info.PHP
<?PHP PHPinfo(); ?>
浏览器访问 (e.g.http://192.168.1.100/info.PHP):
6 让 MysqL 获得 PHP 7支持
apt-cache search PHP7.0
使用下面的命令安装:
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-xslPHP7.0-mbstringPHP-gettext
APCu是随PHP7 PHP Opcache模块的扩展,它增加了一些兼容性功能的支持APC缓存(例如wordpress的插件缓存)软件。
APCu可以安装如下:
apt-get -y install PHP-apcu
重新加载 PHP-FPM:
刷新http://192.168.1.100/info.PHP浏览器看看模块安装情况:
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…
nano /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:
PHP7.0-fpm reload
接下来通过你的Nginx的配置和所有的虚拟主机,并更改fastcgi_pass UNIX行:/var/run/PHP/PHP7.0-fpm.sock; tofastcgi_pass127.0.0.1:9000;,如下:
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; } [...]
最后,重新加载Nginx:
OK,Nginx的LEMP服务器安装完毕。