爲了防止出現意外狀況,建議先卸載現有的全部web服務器資源,如apache、mysql、phpphp
yum remove httpd
yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel
因官方已經支持yum安裝,因此採用yum安裝(一是由於快,二是由於我懶,最重要的是編譯安裝好麻煩)html
一、添加官方的nginx資源庫,我是centos,其它系統去:http://nginx.org/packages/本身找mysql
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
二、此時能夠查看nginx包信息 nginx
yum info nginx
三、安裝nginxc++
yum install nginx
如不出現錯誤,則安裝成功,出錯了就本身解決唄(通常不會出錯的,請相信我)。web
配置nginx支持php,修改default.conf配置文件(我裝的是nginx1.8.0版本。還有,如今還沒裝php呢,先配置好而已)sql
vi /etc/nginx/conf.d/default.conf
修改成如下內容,將如下內容前面的「#」去掉,而後改一下fastcgi_param後面的目錄便可,改成default.conf的web目錄地址:/usr/share/nginx/html,或者是其它的其它目錄也能夠,可是必定要是root(默認)的目錄shell
location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;#這裏寫web服務器的目錄地址 include fastcgi_params; }
整個default.conf配置文件以下,本文只是讓php運行,其它的配置沒作任何修改
apache
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; index index.php index.html index.htm; location / { root /usr/share/nginx/html; #index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #把下面的location的全部「#」所有刪除 location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
而後啓動並設置爲開機啓動centos
systemctl start nginx #啓動nginx服務 systemctl enable nginx #設置nginx爲開機啓動
由於centos7已用mariadb替換mysql,因此安裝的是mariadb(反正mysql和mariadb沒啥區別)
yum install mariadb mariadb-server
如不出錯,則安裝成功
而後啓動並設置爲開機啓動
systemctl start mariadb.service systemctl enable mariadb.service
而後配置mariadb
mysql_secure_installation
除了讓你輸入新密碼和確認新密碼,其它一路回車便可,博主英文很差。
由於nginx是用FastCGI模式運行php,php-fpm是一個FastCGI管理器,因此安裝的時候要選擇php-fpm模塊
yum install php lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap php-tidy php-common php-devel php-fpm
安裝完成後啓動php-fpm,而且設置未開機啓動,由於我這裏是centos7,因此我就用7的命令操做了
systemctl start php-fpm #啓動php-fpm systemctl enable php-fpm #設置爲開機啓動
默認nginx的web路徑是/usr/share/nginx/html,能夠改,能夠不改
在web目錄下建一個php文件
vi /usr/share/nginx/html/index.php
將如下內容寫入文件
<?php phpinfo(); ?>
若是出現激動人心的phpinfo信息,則證實lnmp配置成功,恭喜恭喜,若是出現錯誤,就慢慢改唄。
這一步其實最簡單了
把上面的default.conf複製一份(其實不復制也行,從新在default.conf最下面寫一個server,不過我喜歡將不一樣的東西不折不扣的分開),改改server_name和root的地址,還有和他倆相關的選項便可:
server { listen 80; server_name localtest.com; #把這裏的域名改一下便可 #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; index index.php index.html index.htm; location / { root /var/www/html; #設置此虛擬主機的目錄地址 #index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; #錯誤頁面,改不改無所謂了,都用同樣的唄(又暴露了我懶的本質了) } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; #把這裏的目錄改爲和上面root同樣的 include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
怎麼樣,是否是和default.conf沒多大區別,只是改了三個地方而已。
修改完後,重啓一下nginx服務。
systemctl restart nginx
而後配一下hosts文件,重啓一下網絡服務,若是是在本地虛擬機或局域網安裝測試的,別忘了改一下你當前物理機的hosts.
systemctl restart network
在 /var/www/html文件下建一個index.php文件,隨便寫點東西(懶人同胞們請複製下面的php代碼),在瀏覽器裏面輸入你的設置的域名訪問一下吧,哈哈,是否是出現了久違的「hello world.」,恭喜你,配置成功。
<?php echo "hello world."; ?>
好了,整篇教程到這裏就結束了,若是不會,請私信我,我看到會回覆的。