Ubuntu 14.04 + nginx + php + mysql + phpmyadmin環境搭建,詳細操做

版本:Ubuntu 14.04             nginx-1.12.2               mysql-5.6             php-5.6php

一、安裝nginx-1.12.2html

1)依賴安裝mysql

sudo apt-get install openssl libssl-dev sudo apt-get install libpcre3 libpcre3-dev sudo apt-get install zlib1g-dev

2)添加用戶nginx

sudo useradd www

3)下載http://nginx.org/download/nginx-1.12.2.tar.gz 並解壓sql

進入nginx-1.12.2所在目錄,好比/home/cky數據庫

cd /home/cky

解壓瀏覽器

tar xzf nginx-1.12.2.tar.gz

4)進入相應目錄並編譯服務器

cd  nginx-1.12.2 ./configure \ --prefix=/usr/local/nginx \ --user=www \ --group=www \ --with-http_stub_status_module \ --with-http_ssl_module

5)安裝cookie

sudo make install

6)複製nginx到bin目錄下,方便使用框架

sudo cp /usr/local/nginx/sbin/nginx  /usr/bin/

7)啓動

sudo nginx

8)其餘經常使用操做

關閉nginx

sudo ginx -s stop

重讀nginx配置文件

sudo nginx -s reload

9)測試,瀏覽器輸入http://127.0.0.1彈出Welcome to nginx!即表示安裝成功

 

二、安裝php-fpm

sudo add-apt-repository  ppa:ondrej/php
sudo apt-get -y update

sudo apt-get install php5.6-fpm

 其餘擴展

 

sudo   apt-get   install  php5.6-curl
sudo apt-get install php5.6-xml

 

 

 

三、修改nginx配置文件以支持 PHP

#打開配置文件  

sudo  vi  /usr/local/nginx/conf/nginx.conf

按鍵盤a進入編輯模式

更改內容1,第43行左右

location / { root html; index index.html index.htm; }

更改成:

location / { root /www/wwwroot; index index.html index.htm index.php;        if (!-e $request_filename){
#地址做爲將參數rewrite到index.php上。tp框架接收s參數爲controller和action,很多框架都利用這種方式來實現僞pathinfo模式(pathinfo爲php功能,nginx並不支持)
rewrite ^/(.*)$ /index.php?s=$1 last;
break;
} }

#上面的正則能解決不帶xx.php的,這條正則是爲了rewrite url中帶index.php/admin/admin/login這種,思路是同樣的,將index.php後的字符串當成參>數
location ~ /.*\.php/ {
rewrite ^(.*?/?)(.*\.php)(.*)$ /$2?s=$3 last;
break;
}

更改內容2,第65行左右

        #location ~ \.php$ {

        # root html;

        # fastcgi_pass 127.0.0.1:9000;

        # fastcgi_index index.php;

        # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

        # include fastcgi_params;

        #}

更改成:

location ~ \.php$ { root /www/wwwroot/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }

按ESC鍵退了插入模式,並保存退出

:wq

 

四、修改php配置文件

1)修改php5-fpm屬組與監聽端口,在 PHP 5.4 以後,php5-fpm並非監聽 9000 端口

sudo vi /etc/php/5.6/fpm/pool.d/www.conf

php5-fpm屬組:

;user = www-data ;group = www-data

改成

user = www group = www

 

更改監聽端口:

;listen = /var/run/php5.6-fpm.sock

改成

listen = 127.0.0.1:9000

 

2)配置php-fpm

sudo  vi  /etc/php/5.6/fpm/php.ini

768行,將

;cgi.fix_pathinfo=1

改成(還要去掉分號)

cgi.fix_pathinfo=0

保存退出

 

五、安裝mysql數據庫

1)安裝 mysql-client-core-5.6

sudo apt-get install mysql-client-core-5.6

2)安裝 mysql-client-5.6

sudo apt-get install mysql-client-5.6

3)安裝 mysql-server-5.6

sudo apt-get install mysql-server-5.6

安裝過程當中要設置root用戶的密碼

 4)mysql相關操做

查看mysql進程是否運行

ps -ef | grep mysql

查看msyql 監聽端口

netstat -tap | grep mysql

msyql服務中止

sudo service mysql stop

Mysql 服務啓動

sudo service mysql start

Mysql 服務重啓

sudo service mysql restart

5)設置Mysql 服務遠程訪問

sudo vi /etc/mysql/my.cnf

47行改成

#bind-address = 127.0.0.1

重啓msyql

sudo service mysql restart

修改訪問權限

進入mysql,輸入以下命令,輸入密碼,進入mysql命令行

mysql -u root -p

受權root用戶訪問權限,並刷新權限,此處的root可用其它MySQL用戶替換,pwd部分需替換爲該用戶對應的密碼

grant all privileges on *.* to root@"%" identified by "pwd" with grant option; flush privileges;

exit;

重啓mysql服務

sudo service mysql restart

 

六、數據庫中存儲中文時出現亂碼

1)進入mysql命令行,查看mysql 服務端編碼

status

發現是latin1的編碼方式非utf8,須要修改成 utf8編碼方式

2)退出mysql命令行並修改配置文件

sudo vi /etc/mysql/my.cnf

在[client]下追加

default-character-set = utf8 

在[mysqld]下追加

character-set-server = utf8

修改後以下

[mysqld] # 
# * Basic Settings

#  user = mysql pid-file        = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp lc-messages-dir = /usr/share/mysql character-set-server = utf8 skip-external-locking

在[mysql]下追加

default-character-set = utf8

保存退出,重啓mysql服務器

sudo service mysql restart

再次查看mysql服務端的編碼方式

進入mysql的命令行再次輸入:

status

 確認latin1等的編碼已改成utf8

 七、安裝phpmyadmin

1)安裝mysqli擴展

sudo  apt-get  install php5.6-mysql

將phpmyadmin安裝下載,並解壓到/usr/local/phpmyadmin/目錄

2)在解壓文件中複製config.sample.inc.php爲config.inc.php,按服務器中mysql的情形進行配置;

/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '127.0.0.1';
$cfg['Servers'][$i]['connect_type'] = 'socket';
$cfg['Servers'][$i]['socket'] = '/tmp/mysqld.sock ';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = false;

 

3)建立存放log的文件,路徑

sudo touch /var/log/phpmyadmin-nginx.log

4)在nginx的安裝目錄/usr/local/nginx/conf目錄下,建立nginx集成phpmyadmin的配置文件phpmyadmin.conf:

server {
    listen 888;
    server_name  localhost;
    access_log   /var/log/phpmyadmin-nginx.log; 
    set          $php_upstream '127.0.0.1:9000';

    location / {
        root     /usr/local/phpmyadmin;
        index    index.php;
    }

    location ~ \.php$ {
        root           /usr/local/phpmyadmin;
        fastcgi_pass   $php_upstream;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

5)在nginx的配置文件/usr/local/nginx/conf/nginx.conf中包含phpmyadmin的配置文件:

include /usr/local/nginx/conf/phpmyadmin.conf;

6)重啓nginx便可

7)訪問http://127.0.0.1:888

相關文章
相關標籤/搜索