Centos7 搭建lnmp環境 (centos7+nginx+MySQL5.7.9+PHP7)


阿里雲一台服務器出現問題!

我估計是一鍵安裝包環境的原因,所以打算重新搭建下環境!

首先,當然是先做好快照!安全第一!

對系統盤做更換系統操作,裝上純凈版的centos。

 裝好后,進入系統

一、掛載數據盤

df -h

只有系統盤了,掛載上原來的數據盤

fdisk -l

看到數據盤了/dev/vdb1

掛載上這個數據盤,

mkdir /data0

mount /dev/vdb1 /data0

然后寫入分區:

echo '/dev/vdb1  /data0 ext4    defaults    0  0' >> /etc/fstab

不寫入分區表,重啟后又要掛載的。

怎么知道分區類型是ext4,用這個命令:

df -hT

 

好的成功了!

 

二、安裝nginx

首先更新系統軟件

# yum update

安裝nginx

1.安裝nginx源

# yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.安裝nginx

# yum install nginx

3.啟動nginx

# service nginx start

Redirecting to /bin/systemctl start  nginx.service

4.訪問http://你的ip/

如果成功安裝會出來nginx默認的歡迎界面

 

三、安裝MySQL5.7.*

1.安裝mysql源

# yum localinstall  http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

2.安裝mysql

# yum install mysql-community-server

3.安裝mysql的開發包,以后會有用

# yum install mysql-community-devel

4.啟動mysql

# service mysqld start

Redirecting to /bin/systemctl start  mysqld.service

5.查看mysql啟動狀態

# service mysqld status

出現pid

證明啟動成功

6.獲取mysql默認生成的密碼

# grep 'temporary password' /var/log/mysqld.log

 選中的就是密碼。

7.換成自己的密碼

# mysql -uroot -p

Enter password:輸入上頁的密碼,進入mysql

8. 更換密碼

1
mysql>  ALTER USER  'root' @ 'localhost'  IDENTIFIED BY  'MyNewPasdfs4!' ;

這個密碼一定要足夠復雜,不然會不讓你改,提示密碼不合法;

9.退出mysql並試用下新密碼

mysql> quit;

mysql -uroot -p

確認密碼正確

四、編譯安裝php7.0.0

1.下載php7源碼包

# cd /root & wget -O php7.tar.gz http://cn2.php.net/get/php-7.0.1.tar.gz/from/this/mirror

2.解壓源碼包

# tar -xvf php7.tar.gz

3.進入目錄

# cd php-7.0.1

4.安裝php依賴包 

# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

5.編譯配置,這里如果上一步的某些依賴包沒有安裝好,就會遇到很多configure error,我們一一解決,安裝上相關軟件開發包就可以

復制代碼
# ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx  \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared  \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir  \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets  \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache
復制代碼

configure error:

1.configure: error: xml2-config not found. Please check your libxml2 installation.

解決:

1
<span style= "color: #ff0000" ># yum  install  libxml2 libxml2-devel< /span >

 

2.configure: error: Cannot find OpenSSL's <evp.h>

解決:

1
<span style= "color: #ff0000" ># yum  install  openssl openssl-devel< /span >

 

3.configure: error: Please reinstall the BZip2 distribution

解決:

1
<span style= "color: #ff0000" ># yum  install  bzip2  bzip2 -devel< /span >

 

4.configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

解決:

1
<span style= "color: #ff0000" ># yum  install  libcurl libcurl-devel< /span >

 

5.If configure fails try --with-webp-dir=<DIR> configure: error: jpeglib.h not found.

 

解決:

1
<span style= "color: #ff0000" ># yum  install  libjpeg libjpeg-devel< /span >

6.If configure fails try --with-webp-dir=<DIR>

checking for jpeg_read_header in -ljpeg... yes

configure: error: png.h not found.

解決:

1
<span style= "color: #ff0000" ># yum  install  libpng libpng-devel< /span >

 

7.If configure fails try --with-webp-dir=<DIR>

checking for jpeg_read_header in -ljpeg... yes

checking for png_write_image in -lpng... yes

If configure fails try --with-xpm-dir=<DIR>

configure: error: freetype-config not found.

解決:

1
<span style= "color: #ff0000" ># yum  install  freetype freetype-devel< /span >

8.configure: error: Unable to locate gmp.h

解決:

1
<span style= "color: #ff0000" ># yum  install  gmp gmp-devel< /span >

9.configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解決:

  # yum install libmcrypt libmcrypt-devel

10.configure: error: Please reinstall readline - I cannot find readline.h

解決:

1
<span style= "color: #ff0000" ># yum  install  readline readline-devel< /span >

11.configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

解決:

1
<span style= "color: #ff0000" ># yum  install  libxslt libxslt-devel< /span >

6.編譯與安裝

# make && make install

這里要make好久,要耐心一下

 7.添加 PHP 命令到環境變量

# vim /etc/profile

在末尾加入

PATH=$PATH:/usr/local/php/bin

export PATH

要使改動立即生效執行

# source /etc/profile

查看環境變量

# echo $PATH

查看php版本

# php -v

8.配置php-fpm

# cp php.ini-production /etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm

9.啟動php-fpm

# /etc/init.d/php-fpm start

五、配置nginx虛擬機,綁定域名

# vim /etc/nginx/conf.d/php7.aaa.com.conf
這里可以把php7.aaa.com.conf改成自己的域名

把下面的內容復制到php7.aaa.com.conf里

server{
    listen 80;
    server_name  php7.aaa.com;
    root /var/www/html/php7.aaa.com; # 該項要修改為你准備存放相關網頁的路徑
    location / {
        index  index.php index.html index.htm;
         #如果請求既不是一個文件,也不是一個目錄,則執行一下重寫規則
         if (!-e $request_filename)
         {
            #地址作為將參數rewrite到index.php上。
            rewrite ^/(.*)$ /index.php/$1;
            #若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
         }
    }
    #proxy the php scripts to php-fpm
    location ~ \.php {
            include fastcgi_params;
            ##pathinfo支持start
            #定義變量 $path_info ,用於存放pathinfo信息
            set $path_info "";
            #定義變量 $real_script_name,用於存放真實地址
            set $real_script_name $fastcgi_script_name;
            #如果地址與引號內的正則表達式匹配
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                    #將文件地址賦值給變量 $real_script_name
                    set $real_script_name $1;
                    #將文件地址后的參數賦值給變量 $path_info
                    set $path_info $2;
            }
            #配置fastcgi的一些參數
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
            ###pathinfo支持end
        fastcgi_intercept_errors on;
        fastcgi_pass   127.0.0.1:9000;
    }
    
    location ^~ /data/runtime {
    return 404;
    }
    
    location ^~ /application {
    return 404;
    }
    
    location ^~ /simplewind {
    return 404;
    }
}

2.重啟nginx

# service nginx reload

3.

# vim /var/www/html/php7.aaa.com/index.php

把下面的代碼復制到這個文件 里

<?php

phpinfo();

4.查看訪問http://php7.aaa.com

ok!收工!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM