1. 程式人生 > >Linux(CentOS 7)+ Nginx(1.10.2)+ Mysql(5.7.16)+ PHP(7.0.12)完整環境搭建

Linux(CentOS 7)+ Nginx(1.10.2)+ Mysql(5.7.16)+ PHP(7.0.12)完整環境搭建

首先安裝Linux系統,我以虛擬機器安裝來做示例,先去下載 VitualBox,這是一款開源的虛擬機器軟體,https://www.virtualbox.org 官網地址。或者是VMware,www.vmware.com,不過這個軟體是收費的。當然同時還要去下載一個Linux映象,我下載是CentOS 7系統,https://www.centos.org/download

下載好了之後開啟虛擬機器,我用的是VMware,選擇建立自定義虛擬機器:

繼續下一步:

點選完成。

看到這個介面後,點選CD/DVD(IDE):

點選選擇光碟映象,把下載好的Centos 7 系統放進去:

接著回到上個頁面,點選啟動磁碟:

 選擇CD/DVD,然後點選重新啟動:

 可以看到已經載入映象檔案,選擇第一個安裝:

 

選擇語言,繼續,

 這玩意得先點進去,然後儲存下,

要上網的同志不要忘記開啟網路了:

這個時候就開始安裝了,安裝的同時把root密碼和使用者賬號密碼設定下:

 設定完成後,我們就可以耐心的等待了……

OK,重啟系統,登入root賬戶,也可以登入你自己設定好的使用者賬戶,是不是很酷炫。

先看下網路有沒有問題,輸入ping www.baidu.com,看到網路可以正常訪問:

如果不能正常訪問網路,修改ifcfg檔案,把ONBOOT="no" 改為 "yes",儲存即可。

[[email protected] ~]# vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 

重啟下網路:

systemctl restart network.service

檢視ip地址,輸入ip addr,紅色部分可以直接在瀏覽器裡訪問到,如下圖:

ok,沒有問題,接下來你就可以安裝各種軟體了……當然,我們先做正事,開始安裝PHP,先去PHP官網上下載壓縮包:

選擇一個版本,然後複製連結地址,在命令列輸入:

wget http://hk1.php.net/get/php-7.0.12.tar.gz/from/this/mirror

提示找不到wget命令,先下載wget:

yum install wget

安裝完成後在執行:

wget http://hk1.php.net/get/php-7.0.12.tar.gz/from/this/mirror

看到已經下載好到目錄下了:

接下來再解壓,輸入:

tar -zxvf mirror

解壓好後再進入到 http://php.net/manual/zh/install.fpm.php 來安裝php-fpm,因為現在的php還不能和nginx一起工作,只能和Apache工作,php-fpm是nginx和php的一個橋樑,所以

 我們繼續安裝php-fpm。

先安裝需要的編譯工具 gcc,gcc++,libxml2-devel:

yum install gcc gcc-c++ libxml2-devel

進入到php目錄下進行編譯和安裝:

cd php-7.0.12/  //進入php目錄下
./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7 --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config  --with-pdo-mysql=/usr/local/mysql --enable-fpm --enable-libxml   //這裡選擇要安裝的php目錄,後面引數是開啟php-fpm,支援mysql等
make   //進行編譯
make install  //進行安裝

cd php-7.0.12/  //進入php目錄下
./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7 --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config  --with-pdo-mysql=/usr/local/mysql --enable-fpm --enable-libxml   //這裡選擇要安裝的php目錄,後面引數是開啟php-fpm,支援mysql等
make   //進行編譯
make install  //進行安裝

安裝完成後進入到安裝目錄下:

cd /usr/local/php7/lib/php

php安裝完成。

mysql的安裝

首先建立一個名為mysql且沒有登入許可權的使用者和一個名為mysql的使用者組:

groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16.tar.gz

安裝mysql需要的編譯工具:

yum -y install cmake gcc-c++ ncurses-devel perl-Data-Dumper boost boost-doc boost-devel

進入到mysql目錄下進行編譯和安裝:

cd /mysql-5.7.16
cmake -DCMAKE_STALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/mysql/data -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1  -DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock  -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLED_DOWNLOADS=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled

cd /mysql-5.7.16
cmake -DCMAKE_STALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/mysql/data -DSYSCONFDIR=/etc -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1  -DMYSQL_UNIX_ADDR=/var/run/mysql/mysql.sock  -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLED_DOWNLOADS=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled 

 執行上面的配置命令的結果如下圖所示:

測試發現編譯MySQL5.7以及更高的版本時,都需要下載並引用或者直接安裝boost庫,否則在執行cmake命令時會報如下錯誤:

-- Running cmake version 2.8.11
-- Configuring with MAX_INDEXES = 64U
-- SIZEOF_VOIDP 8
-- MySQL 5.7.16          [MySQL版本]
-- Packaging as: mysql-5.7.16-Linux-x86_64
-- Looked for boost/version.hpp in  and 
-- BOOST_INCLUDE_DIR BOOST_INCLUDE_DIR-NOTFOUND
-- LOCAL_BOOST_DIR 
-- LOCAL_BOOST_ZIP 
-- Could not find (the correct version of) boost.       [關鍵錯誤資訊]
-- MySQL currently requires boost_1_59_0                    [解決辦法]

CMake Error at cmake/boost.cmake:76 (MESSAGE):          [具體錯誤和解決方法]
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

  This CMake script will look for boost in <directory>.  If it is not there,
  it will download and unpack it (in that directory) for you.

  If you are inside a firewall, you may need to use an http proxy:

  export http_proxy=http://example.com:80

Call Stack (most recent call first):
  cmake/boost.cmake:228 (COULD_NOT_FIND_BOOST)
  CMakeLists.txt:452 (INCLUDE)


-- Configuring incomplete, errors occurred!
See also "/mydata/mysql-5.7.16/CMakeFiles/CMakeOutput.log".

-- Running cmake version 2.8.11
-- Configuring with MAX_INDEXES = 64U
-- SIZEOF_VOIDP 8
-- MySQL 5.7.16          [MySQL版本]
-- Packaging as: mysql-5.7.16-Linux-x86_64
-- Looked for boost/version.hpp in  and 
-- BOOST_INCLUDE_DIR BOOST_INCLUDE_DIR-NOTFOUND
-- LOCAL_BOOST_DIR 
-- LOCAL_BOOST_ZIP 
-- Could not find (the correct version of) boost.       [關鍵錯誤資訊]
-- MySQL currently requires boost_1_59_0                    [解決辦法]

CMake Error at cmake/boost.cmake:76 (MESSAGE):          [具體錯誤和解決方法]
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

  This CMake script will look for boost in <directory>.  If it is not there,
  it will download and unpack it (in that directory) for you.

  If you are inside a firewall, you may need to use an http proxy:

  export http_proxy=http://example.com:80

Call Stack (most recent call first):
  cmake/boost.cmake:228 (COULD_NOT_FIND_BOOST)
  CMakeLists.txt:452 (INCLUDE)


-- Configuring incomplete, errors occurred!
See also "/mydata/mysql-5.7.16/CMakeFiles/CMakeOutput.log".

只要將http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz下載下來,上傳到/usr/local/boost,在執行命令:

cmake -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
編譯和安裝:
make && make install

 檢視編譯成功後的MySQL安裝目錄:

使用cd命令檢視MySQL的安裝目錄/usr/local/mysql/下面是否生成了相關目錄檔案(最重要的當然是bin、sbin和lib目錄)。如果lib目錄下面沒有生成如圖所示的.so動態庫檔案和.a靜態庫檔案,那麼說明安裝不成功,需要重新編譯安裝。(即使成功了也可能會導致php程序無法找到mysql的相關庫檔案)。

然後把編譯生成的my.cnf檔案備份:

cp /etc/my.cnf /etc/my.cnf.bak

再修改my.cnf配置如下圖:(具體路徑根據你安裝的目錄為準)

複製程式碼

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[client]
port=3306
socket=/var/run/mysql/mysql.sock

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
user = mysql
basedir = /usr/local/mysql
datadir = /mydata/mysql/data
port=3306
server-id = 1
socket=/var/run/mysql/mysql.sock

character-set-server = utf8
log-error = /var/log/mysql/error.log
pid-file = /var/log/mysql/mysql.pid
general_log = 1
skip-name-resolve
#skip-networking
back_log = 300

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128 
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 28M
key_buffer_size = 4M

thread_cache_size = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30


performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1



myisam_sort_buffer_size = 8M
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,NO_AUTO_CREATE_USER,STRICT_TRANS_TABLES

[mysqldump]
quick
max_allowed_packet = 16M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

複製程式碼

將MySQL編譯生成的bin目錄新增到當前Linux系統的環境變數中:

echo -e '\n\nexport PATH=/usr/local/mysql/bin:$PATH\n' >> /etc/profile && source /etc/profile

建立MySQL資料庫檔案的存放路徑以及相關安全配置

在Linux主機上建立一個目錄/mydata/mysql/data,用於存放MySQL的資料庫檔案。同時設定其使用者和使用者組為之前建立的mysql,許可權為777。這樣其它使用者是無法進行讀寫的,儘量保證資料庫的安全。

mkdir -p /mydata/mysql/data && chown -R root:mysql /usr/local/mysql
chown -R mysql:mysql /mydata/mysql/data
chmod -R go-rwx /mydata/mysql/data

初始化MySQL自身的資料庫

在MySQL安裝目錄的\bin\路徑下,執行mysqld命令,初始化MySQL自身的資料庫。

cd /usr/local/mysql/bin
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/mysql/data

執行完後,通過 ls -lrt /mydata/mysql/data/ 命令檢視是否生成了MySQL自身的資料庫檔案。

建立MySQL日誌存放目錄以及設定開機啟動

下面配置的MySQL日誌存放目錄以及許可權都是根據前面my.cnf檔案寫的,也就是兩者需要保持一致。

複製程式碼

mkdir -p /var/run/mysql && mkdir -p /var/log/mysql
chown -R mysql:mysql /var/log/mysql && chown -R mysql:mysql /var/run/mysql        #配置開機自啟動
cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld     #增加可執行許可權
chkconfig --add mysqld      #新增到sysV服務
chkconfig mysqld on

複製程式碼

在完成上面的操作後,就可以正式使用MySQL服務了。啟動MySQL程序服務的命令如下:

複製程式碼

[[email protected] ~]# mysqld_safe --user=mysql --datadir=/mydata/mysql/data --log-error=/var/log/mysql/error.log &
[1] 19077
[[email protected] ~]# 2016-10-23T04:21:19.530315Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2016-10-23T04:21:19.563588Z mysqld_safe Starting mysqld daemon with databases from /mydata/mysql/data

######上面這條命令會在後臺繼續執行,所以直接回車並執行下面這條命令
[[email protected] ~]# service mysqld start
Starting MySQL SUCCESS! 

複製程式碼

使用 ps -ef | grep mysql 檢視MYSQL服務端程序和埠監聽情況:

複製程式碼

[[email protected] ~]# ps -ef | grep mysql
root      19077  18966  0 12:21 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql --datadir=/mydata/mysql/data --log-error=/var/log/mysql/error.log
mysql     19685  19077  0 12:21 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mydata/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/error.log --open-files-limit=65535 --pid-file=/var/log/mysql/mysql.pid --socket=/var/run/mysql/mysql.sock --port=3306
root      20318  18966  0 12:22 pts/0    00:00:00 grep --color=auto mysql

複製程式碼

初始化MySQL資料庫的root使用者密碼

和Oracle資料庫一樣,MySQL資料庫也預設自帶了一個root使用者(這個和當前Linux主機上的root使用者是完全不搭邊的),我們在設定好MySQL資料庫的安全配置後初始化root使用者的密碼。配置過程中,一路輸入y就行了。這裡只說明下MySQL5.7.16版本中,使用者密碼策略分成低階LOW、中等MEDIUM和超強STRONG三種,推薦使用中等MEDIUM級別!

複製程式碼

[[email protected] ~]# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8         【只需要長度大於或等於8】
MEDIUM Length >= 8, numeric, mixed case, and special characters        【還需要包含數字、大小寫和類似於@#%等特殊字元】
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file       【還需要包含字典檔案】

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: y

複製程式碼

將MySQL資料庫的動態連結庫共享至系統連結庫

一般MySQL資料庫還會被類似於PHP等服務呼叫,所以我們需要將MySQL編譯後的lib庫檔案新增至當前Linux主機連結庫/etc/ld.so.conf.d/下,這樣MySQL服務就可以被其它服務呼叫了。

複製程式碼

[[email protected] ~]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
[[email protected] ~]# ldconfig
[[email protected] ~]# ldconfig -v |grep mysql
ldconfig: 無法對 /libx32 進行 stat 操作: 沒有那個檔案或目錄
ldconfig: 多次給出路徑“/usr/lib”
ldconfig: 多次給出路徑“/usr/lib64”
ldconfig: 無法對 /usr/libx32 進行 stat 操作: 沒有那個檔案或目錄
/usr/local/mysql/lib:
    libmysqlclient.so.20 -> libmysqlclient.so.20.3.3

複製程式碼

建立其它MySQL資料庫使用者

使用MySQL資料庫root管理員使用者登入MySQL資料庫後,可以管理資料庫和其他使用者了。這裡演示建立一個名為evai的MySQL使用者(密碼為@Typeusers2016.com)和一個名為typeusers的資料庫。

複製程式碼

[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.16-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
######登入成功後,建立typecodes資料庫,並設定字符集
mysql> CREATE DATABASE `typeusers` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.01 sec)
######建立名為typecodes使用者,並讓它擁有typecodes資料庫所有的許可權
mysql> grant all privileges on typeusers.* to [email protected] identified by '@Typeusers2016.com'; Query OK, 0 rows affected, 2 warnings (0.01 sec)

  mysql> exit

  Bye

複製程式碼

ok,退出,然後用剛建立的使用者登入到mysql:

複製程式碼

[[email protected] ~]# mysql -uevai -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.16-log Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

複製程式碼

安裝nginx

去官網 http://nginx.org 下載最新的穩定版本,複製連結:

下載並解壓:

複製程式碼

[[email protected] ~]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
--2016-10-23 13:22:15--  http://nginx.org/download/nginx-1.10.2.tar.gz
正在解析主機 nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 95.211.80.227
正在連線 nginx.org (nginx.org)|206.251.255.63|:80... 已連線。
已發出 HTTP 請求,正在等待迴應... 200 OK
長度:910812 (889K) [application/octet-stream]
正在儲存至: “nginx-1.10.2.tar.gz”

100%[=================================================>] 910,812     43.0KB/s 用時 18s    

2016-10-23 13:22:35 (49.4 KB/s) - 已儲存 “nginx-1.10.2.tar.gz” [910812/910812])

 [[email protected] ~]# tar -zxvf nginx-1.10.2.tar.gz     #解壓

複製程式碼

在下載一個叫pcre的東東,PCRE 簡介:PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 相容的正則表示式庫。這些在執行正規表示式模式匹配時用與Perl 5同樣的語法和語義是很有用的。Boost太龐大了,使用boost regex後,程式的編譯速度明顯變慢。測試了一下,同樣一個程式,使用boost::regex編譯時需要3秒,而使用pcre不到1秒。因此改用pcre來解決C語言中使用正則表示式的問題,簡單來說可以進行URL重寫,我們下載過來並解壓

[[email protected] ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
[[email protected] ~]# tar -zxvf pcre-8.39.tar.gz

進入到nginx資料夾目錄下,編譯安裝:

./configure --prefix=/usr/local/nginx  --with-pcre=../pcre-8.39     #回車
make && make install

安裝完成後進入 /usr/local/nginx 目錄下,可以看到已經安裝完畢:

複製程式碼

[[email protected] nginx-1.10.2]# cd /usr/local/nginx/
[[email protected] nginx]# ll
總用量 4
drwxr-xr-x. 2 root root 4096 10月 23 13:43 conf
drwxr-xr-x. 2 root root   38 10月 23 13:43 html
drwxr-xr-x. 2 root root    6 10月 23 13:43 logs
drwxr-xr-x. 2 root root   18 10月 23 13:43 sbin

複製程式碼

接下來啟動nginx,看到nginx已經啟動:

複製程式碼

[[email protected] nginx]# cd sbin/
[[email protected] sbin]# ./nginx 
[[email protected] sbin]# ps -aux |grep nginx
root      32128  0.0  0.0  18112   592 ?        Ss   13:46   0:00 nginx: master process ./nginx
nobody    32129  0.0  0.1  18532  1308 ?        S    13:46   0:00 nginx: worker process
root      32131  0.0  0.0 112664   984 pts/0    R+   13:46   0:00 grep --color=auto nginx
[[email protected] sbin]# 

複製程式碼

我們用curl http:/127.0.0.1 測試下是否已經可以看到資訊:

複製程式碼

[[email protected] nginx]# curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

複製程式碼

沒有問題,開啟瀏覽器輸入ip地址訪問:

 也是成功的,不過有些小夥伴會打不開,原因是防火牆開啟了導致訪問失敗,CentOS 7預設使用的是firewall作為防火牆,我們輸入 systemctl stop firewalld.service 把它關閉,再次訪問瀏覽器就可以看到了。附上防火牆的命令:

systemctl start firewalld.service    #啟動firewall
systemctl stop firewalld.service     #停止firewall
systemctl disable firewalld.service  #禁止firewall開機啟動

這個時候我們還不能訪問php檔案的網頁的,因為nginx還無法解析它,這個時候該php-fpm大展身手了:

複製程式碼

[[email protected] nginx]# cd /usr/local/php7/sbin/
[[email protected] sbin]# ll
總用量 28168
-rwxr-xr-x. 1 root root 28839999 10月 22 23:43 php-fpm
[[email protected] sbin]# ./php-fpm 
[23-Oct-2016 14:14:51] ERROR: failed to open configuration file '/usr/local/php7/etc/php-fpm.conf': No such file or directory (2)
[23-Oct-2016 14:14:51] ERROR: failed to load configuration file '/usr/local/php7/etc/php-fpm.conf'
[23-Oct-2016 14:14:51] ERROR: FPM initialization failed

複製程式碼

提示找不到php-fpm.conf檔案,進入到 /usr/local/php7/etc 檢視,發現只有default檔案,把它copy一份到當前目錄下命名為php-fpm.conf

複製程式碼

[[email protected] sbin]# cd /usr/local/php7/etc
[[email protected] etc]# ll
總用量 12
-rw-r--r--. 1 root root 1239 10月 22 23:43 pear.conf
-rw-r--r--. 1 root root 4465 10月 22 23:43 php-fpm.conf.default
drwxr-xr-x. 2 root root   29 10月 22 23:43 php-fpm.d

  [[email protected] etc]# cp php-fpm.conf.default php-fpm.conf

複製程式碼

再次啟動還是報錯,發現conf.d目錄下沒有.conf字尾的檔案,一樣copy一份到該目錄下改為www.conf ,再啟動就成功了:

複製程式碼

[[email protected] etc]# /usr/local/php7/sbin/php-fpm 
[23-Oct-2016 14:19:14] WARNING: Nothing matches the include pattern '/usr/local/php7/etc/php-fpm.d/*.conf' from /usr/local/php7/etc/php-fpm.conf at line 125.
[23-Oct-2016 14:19:14] ERROR: No pool defined. at least one pool section must be specified in config file
[23-Oct-2016 14:19:14] ERROR: failed to post process the configuration
[23-Oct-2016 14:19:14] ERROR: FPM initialization failed
[[email protected] etc]# cd php-fpm.d
[[email protected] php-fpm.d]# ll
總用量 20
-rw-r--r--. 1 root root 18521 10月 22 23:43 www.conf.default
[[email protected] php-fpm.d]# cp www.conf.default www.conf
[[email protected] php-fpm.d]# /usr/local/php7/sbin/php-fpm 
[[email protected] php-fpm.d]# ps -aux | grep php-fpm
root      32369  0.0  0.2 148336  2548 ?        Ss   14:22   0:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)

nobody    32370  0.0  0.2 148336  2200 ?        S    14:22   0:00 php-fpm: pool www

nobody    32371  0.0  0.2 148336  2200 ?        S    14:22   0:00 php-fpm: pool www

root      32373  0.0  0.0 112664   984 pts/1    R+   14:23   0:00 grep --color=auto php-fp

[[email protected] php-fpm.d]# 

複製程式碼

然後開啟編輯 /usr/local/nginx/conf/nginx.conf 檔案,找到下面這段並改為如下圖:

複製程式碼

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

複製程式碼

再進入html目錄下,建立一個test.php檔案:

複製程式碼

[[email protected] nginx]# cd html/
[[email protected] html]# ll
總用量 8
-rw-r--r--. 1 root root 537 10月 23 13:43 50x.html
-rw-r--r--. 1 root root 612 10月 23 13:43 index.html
[[email protected] html]# vim test.php

#輸入
<?php
phpinfo();

#儲存退出
[[email protected] html]# ../sbin/nginx -s reload      #重新載入nginx

複製程式碼

開啟瀏覽器,輸入你的ip地址:http://xxx.xxx.xx.xxx/test.php 訪問:

 至此,lnmp環境搭建完成。