Nginx (读”engine x”) 是一款免费、开源的高机能 HTTP 处事。Nginx 不变、富厚的成果集、设置简朴、资源耗损低。本教程先容了如何通过PHP7支持(通过PHP-FPM)和MySQL5.7支持(LEMP= LINUX + nginx(发音为“engine x”)+ MySQL+ PHP)在Ubuntu 16.04处事器上安装Nginx处事器。
1 劈头说明
序列号 | CPU | RAM | HDD | 带宽 | 售价(美元) | 免费试用 |
---|---|---|---|---|---|---|
香港服务器1 | E5-2620 | 32G | 1T HDD | 50M/无限流量 | $196.00 | 立即申请 |
香港服务器2 | E5-2650 | 32G | 1T HDD | 50M/无限流量 | $256.00 | 立即申请 |
香港服务器3 | E5-2680 | 32G | 1T HDD | 50M/无限流量 | $316.00 | 立即申请 |
香港服务器4 | E5-2690 | 32G | 1T HDD | 50M/无限流量 | $336.00 | 立即申请 |
香港服务器5 | E5-2697 | 32G | 1T HDD | 50M/无限流量 | $376.00 | 立即申请 |
香港服务器6 | E5-2620*2 | 32G | 1T HDD | 50M/无限流量 | $376.00 | 立即申请 |
香港服务器7 | E5-2650*2 | 32G | 1T HDD | 50M/无限流量 | $436.00 | 立即申请 |
香港服务器8 | E5-2680*2 | 32G | 1T HDD | 50M/无限流量 | $476.00 | 立即申请 |
香港服务器9 | E5-2690*2 | 32G | 1T HDD | 50M/无限流量 | $556.00 | 立即申请 |
香港服务器10 | E5-2697*2 | 32G | 1T HDD | 50M/无限流量 | $596.00 | 立即申请 |
香港服务器11 | E5-2680v4*2 | 32G | 1T HDD | 50M/无限流量 | $696.00 | 立即申请 |
香港服务器12 | E5-2698v4*2 | 32G | 1T HDD | 50M/无限流量 | $796.00 | 立即申请 |
在本教程中,我利用的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: <– yourrootsqlpasswordRepeat password for the MySQL “root” user: <– yourrootsqlpassword
为了确保数据库处事器,并删除匿名用户和测试数据库,运行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 passwordsand improve security. It checks the strength of passwordand allows the users to set only those passwords which aresecure 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 havea user account created for them. This is intended only fortesting, and to make the installation go a bit smoother.You should remove them before moving into a productionenvironment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : <– ySuccess.
Normally, root should only be allowed to connect from‘localhost’. This ensures that someone cannot guess atthe root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : <– ySuccess.
By default, MySQL comes with a database named ‘test’ thatanyone can access. This is also intended only for testing,and should be removed before moving into a productionenvironment.
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 changesmade so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : <– ySuccess.
All done!
MySQL is secured now.
3 安装 Nginx
在你已经安装了Apache2的话,那么利用这些呼吁先删除再安装nginx:
service apache2 stopupdate-rc.d -f apache2 removeapt-get remove apache2
Ubuntu16.04有Nginx安装包,我们可以安装。
apt-get -y install nginx
Start nginx afterwards:
service nginx start
输入您的Web处事器的IP地点或主机名到欣赏器(譬喻http://192.168.1.100),你应该看到如下页面:
在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_timeout2;[...]
虚拟主机处事器{}容器界说。默认的虚拟主机是在文件中界说的/etc/nginx/sites-available/default – 让我们来修改它,如下所示:
nano/etc/nginx/sites-available/default
[...]server{listen80default_server;listen[::]:80default_server;#SSLconfiguration##listen443ssldefault_server;#listen[::]:443ssldefault_server;##Note:YoushoulddisablegzipforSSLtraffic.#See:https://bugs.debian.org/773332##Readuponssl_cipherstoensureasecureconfiguration.#See:https://bugs.debian.org/765782##Selfsignedcertsgeneratedbythessl-certpackage#Don'tusetheminaproductionserver!##includesnippets/snakeoil.conf;root/var/www/html;#Addindex.phptothelistifyouareusingPHPindexindex.htmlindex.htmindex.nginx-debian.html;server_name_;location/{#Firstattempttoserverequestasfile,then#asdirectory,thenfallbacktodisplayinga404.try_files$uri$uri/=404;}#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000#location~.php${includesnippets/fastcgi-php.conf;#Withphp7.0-cgialone:#fastcgi_pass127.0.0.1:9000;#Withphp7.0-fpm:fastcgi_passunix:/run/php/php7.0-fpm.sock;}#denyaccessto.htaccessfiles,ifApache'sdocumentroot#concurswithnginx'sone#location~/.ht{denyall;}}[...]
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: