簡(jiǎn)要
????????Nginx (讀”engine x”) 是一款免費(fèi)、開源的高性能 HTTP 服務(wù)琅摩。Nginx 穩(wěn)定、豐富的功能集锭硼、配置簡(jiǎn)單房资、資源消耗低。本教程介紹了如何通過PHP7支持(通過PHP-FPM)和MySQL5.7支持(LEMP= LINUX + nginx(發(fā)音為“engine x”)+ MySQL+ PHP)在Ubuntu16.04服務(wù)器上安裝Nginx服務(wù)器檀头。
1.初步說(shuō)明
在本教程中轰异,我使用的IP 地址192.168.40.129,主機(jī)名server1.example.com暑始。這些設(shè)置可能與你的不同搭独,所以你不得不在適當(dāng)情況下更換他們。
我運(yùn)行的所有步驟在本教程中使用root權(quán)限廊镜,所以一定要確保你以root身份登錄牙肝。
2.更換國(guó)內(nèi)源
如果是自己下載的鏡像,請(qǐng)更換更新源嗤朴。如果是購(gòu)買的VPS配椭、云服務(wù)器等請(qǐng)直接忽略此步驟。
vi /etc/apt/sources.list
將以下內(nèi)容復(fù)制到sources.list內(nèi)
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricteddeb-src http://archive.ubuntu.com/ubuntu xenial main restricted#Added by software-propertiesdeb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe#Added by software-propertiesdeb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe#Added by software-propertiesdeb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse#Added by software-propertiesdeb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe#Added by software-propertiesdeb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
執(zhí)行更新
apt-get update
2.安裝Mysql
安裝 MySQL?運(yùn)行命令:
apt-get -y install mysql-server mysql-client
你會(huì)被要求提供MySQL的root用戶密碼 :
????New password for the MySQL “root” user:<– yourrootsqlpassword
????Repeat password for the MySQL “root” user:<– yourrootsqlpassword
為了確保數(shù)據(jù)庫(kù)服務(wù)器雹姊,并刪除匿名用戶和測(cè)試數(shù)據(jù)庫(kù)股缸,運(yùn)行mysql_secure_installation命令。
mysql_secure_installation
你會(huì)問這些問題:
????root@server1:~# mysql_secure_installation
保護(hù)MySQL服務(wù)器部署吱雏,一直按Enter即可敦姻。
3.安裝Nginx
在你已經(jīng)安裝了Apache2的話瘾境,那么使用這些命令先刪除再安裝nginx:
service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2
Ubuntu16.04有Nginx安裝包,我們可以安裝替劈。
apt-get -y install nginx
輸入您的Web服務(wù)器的IP地址或主機(jī)名到瀏覽器(例如http://192.168.40.129/)寄雀,你應(yīng)該看到如下頁(yè)面:
在Ubuntu16.04的默認(rèn)nginx的文檔根目錄為/var/www/html
4 安裝 PHP 7
我們可以通過使nginx的PHP工作PHP-FPM(PHP-FPM(FastCGI進(jìn)程管理器)是為任何規(guī)模的網(wǎng)站,尤其是繁忙的網(wǎng)站有用的一些附加功能的替代PHP的FastCGI實(shí)現(xiàn))陨献,我們安裝如下:
apt-get -y install php7.0-fpm
5 配置 nginx
打開配置文件/etc/nginx/nginx.conf:
vi? /etc/nginx/nginx.conf
配置是很容易理解 (你可以點(diǎn)擊官方教程:http://wiki.nginx.org/NginxFullExample或:http://wiki.nginx.org/NginxFullExample2)
首先(這是可選)調(diào)整keepalive_timeout到一個(gè)合理的值:
????[...]
????keepalive_timeout? 2;
????[...]
虛擬主機(jī)服務(wù)器{}容器定義盒犹。默認(rèn)的虛擬主機(jī)是在文件中定義的/etc/nginx/sites-available/default – 讓我們來(lái)修改它,如下所示:
vi /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 _;使這是一個(gè)默認(rèn)捕捉所有虛擬主機(jī)(當(dāng)然眨业,你可以同時(shí)喜歡這里www.example.com指定主機(jī)名)急膀。
根目錄 /var/www/html;意味著文檔根目錄/var/www/html.
PHP的重要組成部分位置 ~ \.php$ {}stanza. 取消注釋它來(lái)啟用它。
現(xiàn)在保存文件并重新加載nginx:
service nginx reload
下一步打開/etc/php/7.0/fpm/php.ini…
vi /etc/php/7.0/fpm/php.ini
設(shè)置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:
vi /var/www/html/info.php
<?php
phpinfo();
?>
瀏覽器訪問 (e.g.http://192.168.40.129/info.php):
6 .讓 MySQL 獲得 PHP 7支持
先搜索一下PHP支持的模塊:
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-xsl?php7.0-mbstring?php-gettext
APCu是隨PHP7 PHP Opcache模塊的擴(kuò)展龄捡,它增加了一些兼容性功能的支持APC緩存(例如WordPress的插件緩存)軟件卓嫂。
APCu可以安裝如下:
?apt-get -y install php-apcu
重新加載 PHP-FPM:
service php7.0-fpm reload
刷新http://192.168.1.100/info.php瀏覽器看看模塊安裝情況:
7. 讓 PHP-FPM 使用 TCP 連接
默認(rèn)情況下PHP-FPM監(jiān)聽 /var/run/php/php7.0-fpm.sock. 另外,也可以使 PHP-FPM 試用 TCP 連接聘殖,打開文件/etc/php/7.0/fpm/pool.d/www.conf…
vi /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(本地主機(jī))晨雳。請(qǐng)確保您使用的端口,是不是在你的系統(tǒng)上使用奸腺。
然后重新加載 PHP-FPM:
????php7.0-fpm reload
接下來(lái)通過你的nginx的配置和所有的虛擬主機(jī)餐禁,并更改fastcgi_pass UNIX行:/var/run/php/php7.0-fpm.sock; tofastcgi_pass127.0.0.1:9000;,如下:
????vi /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
OK突照,Nginx的LEMP服務(wù)器安裝完畢帮非。
如果部署時(shí)wordpress會(huì)發(fā)現(xiàn)各種權(quán)限問題(如無(wú)法創(chuàng)建文件,上傳安裝主題插件等)讹蘑。是由于www目錄權(quán)限問題所致末盔。
chown -R www-data.www-data /var/www
即可搞定。