1. 程式人生 > 其它 >Linux下搭建LNMP環境

Linux下搭建LNMP環境

一、安裝NGINX

二、安裝MySQL或MariaDB

三、安裝PHP

1、移除其他版本php軟體

yum remove php-mysql php php-fpm php-common

2、更新yum源

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

3、安裝PHP

yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded  php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache  php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

四、建立nginx和php關係

1、nginx配置檔案

server {
    listen       80;
    server_name  blog.yinghui.com;
    location / {
        root   /html/blog;
        index index.php index.html index.htm;
    }
    location ~ \.php$ {
    fastcgi_pass  127.0.0.1:9000;
    root     /html/blog;
    fastcgi_index  index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
    include fastcgi_params; 
    }
}

2、測試(訪問檔案)

[root@web01 /html/blog]# cat test_info.php 
<?php
   phpinfo();
?>

五、測試php和mariadb關係(開啟網頁顯示mysql successful by root!則表示建立關係成功)

[root@web01 /html/blog]#cat test_mysql.php
<?php
     $servername = "localhost";
     $username = "root";
     $password = "123";
     //$link_id=mysql_connect('主機名','使用者','密碼');
     //mysql -u使用者 -p密碼 -h 主機
     $conn = mysqli_connect($servername, $username, $password);
     if ($conn) {
           echo "mysql successful by root !\n";
        }else{
           die("Connection failed: " . mysqli_connect_error());
        }
?>

六、偽靜態配置

第一個步驟:在wordpress後臺修改頁面配置
登入後臺---設定---固定連結---自定義結構--/%post_id%.html

第二個步驟:實現nginx偽靜態配置

server {
      listen   80;
      server_name   blog.etiantian.org;
      rewrite /wp-admin$ $scheme://$host$uri/permanent;
      location / {
            root   /html/blog;
            index  index.php index.html;
            try_files $uri $uri/ /index.php?$args==$uri;
      }
      location ~ \.php$ {
            root /html/blog;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param HTTPS on;    		 #支援HTTPS
            fastcgi_pass  127.0.0.1:9000;
            include fastcgi_params;
      }
 }