lnmp環境搭建(php7.2.4)

1、虛擬機

強推vmware,比virtual box好上不少,virtual box 跑起來感受好卡,並且動不動就內存xxoo報錯直接閃退了,用vmware還沒出現過這些問題,也不會很卡,跟windows的切換也比較方便,這個是我通常用來安裝的鏡像和虛擬機,能夠自行下載一波php

1.centos7.3鏡像下載html

2.vmware虛擬機下載mysql


2、php部分

1.下載php鏡像

下載
wget http://cn2.php.net/get/php-7.0.28.tar.gz/from/this/mirror
下載完成以後是鏡像文件,須要將其重命名爲tar.gz文件
mv mirror php-7.0.28.tar.gz
解壓
tar -zxvf php-7.0.28.tar.gzlinux

2.參數配置

配置前要安裝/更新libxml2以及libxml2-develnginx

yum -y install libxml2 libxml2-develc++

3.組件安裝

把沒有安裝好的組件安裝好(若是安裝了就會進行升級)git

yum -y install openssl openssl-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel pcre pcre-devel libxslt libxslt-devel bzip2 bzip2-develgithub

4.編譯配置

./configure --prefix=/usr/local/php --with-curl --with-freetype-dir 
--with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 
--with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql 
--with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl 
--with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml 
--enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring 
--enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets 
--enable-sysvsem --enable-sysvshm --enable-xml --enable-zip

(注意,7.2不支持enable-gd-native-ttf這一項,要去掉)web

5.執行編譯

make (編譯時間比較長)
編譯完成以後執行安裝 make install
若是出現In function 'do_convert' .....undefined reference to 'libiconv_open'....的問題,須要到MakeFile文件中找到EXTRA_LIBS,而後在後面加上 -liconvsql

6.配置相應文件

(主要是將配置文件複製到php根文件夾或者將根文件夾下的文件重命名)

cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin

設置php.ini文件設置cgi.fix_pathinfo=0
這一項默認被註釋而且值爲1,根據官方文檔的說明,這裏爲了當文件不存在時,阻止Nginx將請求發送到後端的PHP-FPM模塊,從而避免惡意腳本注入的攻擊,因此此項應該去掉註釋並設置爲0

7.建立web用戶

groupadd www-data
useraddd www-data www-data

(下面這步能夠跳過,由於在前面編譯的時候已經指定了prefix。)
打開/usr/local/php/etc/php-fpm.comf,最後一行,include=NONE/etc/php-fpm.d/*.conf 前面NONE改成/usr/local)

而後修改文件名

cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
vi /usr/local/php/etc/php-fpm.d/www.conf

修改user 和group 設置爲前面建立的www-data

啓動php-fpm 直接輸入php-fpm
若是是重啓php-fpm 則輸入/etc/init.d/php-fpm restart


3、nginx部分

1.下載nginx

wget http://nginx.org/download/nginx-1.4.7.tar.gz
解壓,而後./configure 執行配置
編譯而後執行安裝make && make install
跑nginx可能會出現 nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
則須要執行/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf指定nginx.conf的位置

2.修改nginx.conf文件

前面的user的值注意看看是否是www-data
去掉location php那一塊的註釋

location /{
    root    /home/www  #網站根目錄
    index    index.html index.htm    index.php
}

location ~ [^/]\.php(/|$){
    root    /home/www;   #網站根目錄
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index    index.php;
    fastcgi_param    SCRIPT_FILENAME    SCRIPTE_NAME/$fast_script_name;
    include    fastcgi_params;
    include    fastcgi.conf;

}

重啓nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx

3.設置php-fpm和nginx自啓動

NGINX SHELL腳本 放到/etc/init.d/下取名nginx

#!/bin/sh # # nginx - this script starts and stops the nginx daemon # 
# chkconfig: - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # chkconfig: 2345 90 91 # description: nginx web server# processname: nginx# config: /opt/nginx/conf/nginx.conf# pidfile: /opt/nginx/nginx.pid

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network


if [ -f /etc/sysconfig/nginx ];then
. /etc/sysconfig/nginx
fi

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx #-c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t #-c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart)
$1
;;
test)
configtest
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|test}"
exit 2
esac

PHP-FPM SHELL腳本 放到/etc/init.d/下 取名php-fpm

#!/bin/sh  
# chkconfig:   2345 15 95

# description:  PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation \

# with some additional features useful for sites of any size, especially busier sites.
# DateTime: 2016-09-20

# Source function library.  
. /etc/rc.d/init.d/functions  

# Source networking configuration.  
. /etc/sysconfig/network  

# Check that networking is up.  
[ "$NETWORKING" = "no" ] && exit 0  

phpfpm="/usr/local/php/sbin/php-fpm"  
prog=$(basename ${phpfpm})  

lockfile=/var/lock/subsys/phpfpm

start() {  
    [ -x ${phpfpm} ] || exit 5  
    echo -n $"Starting $prog: "  
    daemon ${phpfpm}
    retval=$?  
    echo  
    [ $retval -eq 0 ] && touch $lockfile  
    return $retval  
}  

stop() {  
    echo -n $"Stopping $prog: "  
    killproc $prog -QUIT  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile  
    return $retval  
}  

restart() {  
    configtest || return $?  
    stop  
    start  
}  

reload() {  
    configtest || return $?  
    echo -n $"Reloading $prog: "  
    killproc ${phpfpm} -HUP  
    RETVAL=$?  
    echo  
}  

force_reload() {  
    restart  
}  

configtest() {  
  ${phpfpm} -t
}  

rh_status() {  
    status $prog  
}  

rh_status_q() {  
    rh_status >/dev/null 2>&1  
}  

case "$1" in  
    start)  
        rh_status_q && exit 0  
        $1  
        ;;  
    stop)  
        rh_status_q || exit 0  
        $1  
        ;;  
    restart|configtest)  
        $1  
        ;;  
    reload)  
        rh_status_q || exit 7  
        $1  
        ;;  
    status)  
        rh_status  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"  
        exit 2  

esac

後續處理:

添加執行權限
chmod a+x /etc/init.d/nginx
chmod a+x /etc/init.d/php-fpm

加入服務
chkconfig --add nginx
chkconfig --add php-fpm

開機自啓
chkconfig nginx on
chkconfig php-fpm on

順即可以設一下快捷指令

`vi ~/.bashrc`
alias nginx='/etc/init.d/nginx'
alias php-fpm='/etc/init.d/php-fpm'
source ~/.bashrc

這樣就能夠直接經過nginx指令和php-fpm指令進行操做而不用經過絕對路徑的指令進行操做


4、mysql安裝

1.下載

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-57-community-release-el7-10.noarch.rmp
yum -y install mysql-community-server(這一步會花很長時間)
systemctl start mysqld.service
能夠查看一下mysql運行狀態
systemctl status mysqld.service

2.修改初始密碼

獲取mysql初始密碼 grep "password" /var/log/mysqld.log
進入mysql輸入上面獲取的密碼
而後修改密碼規則:
set global validate_password_policy=0;
set global validate_password_length=1;
修改密碼 ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD'

3.可視化數據庫phpmyadmin

wget https://files.phpmyadmin.net/phpMyAdmin/4.8.0.1/phpMyAdmin-4.8.0.1-all-languages.tar.gz
下載以後解壓到網站根目錄
把phpmyadmin下面的config.sample.inc.php更名爲config.inc.php
將裏面的host參數由localhost改爲127.0.0.1 不然可能出現mysqli_real_connect ........ No such file的報錯


5、其它安裝

Memcached安裝

分爲服務端安裝和客戶端安裝
1.服務端安裝
方法1:

yum -y install memcached
/usr/bi/memcached -l 127.0.0.1 -p11211 -m 150 -u root

方法2:
memcached 依賴於libevent 庫,所以咱們須要先安裝libevent.
假設將源碼放在/usr/local/src

cd /usr/local/src
wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz3 tar zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local/libevent
make && make install

安裝memcached服務端

cd /usr/local/src
wget http://www.memcached.org/files/memcached-1.4.33.tar.gz3 tar zxvf memcached-1.4.33.tar.gz
cd memcached-1.4.335 ./configure --prefix=/usr/local/memcached --with-libevent=/usr/loca/libevent
make && make install

若是出現g++ command not foundyum install gcc gcc-c++

2.客戶端安裝
客戶端需依賴libmemcached

cd /usr/local/src
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz3 tar zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.185 ./configure --prefix=/usr/local/libmemcached
make && make install

安裝php-fpmmemcached擴展

cd /usr/local/src
wget http://pecl.php.net/get/memcached-2.2.0.tgz3 tar zxvf memcached-2.2.0.tgz
cd memcached-2.2.0

到了這一步,咱們要使用安裝php時生成的 phpize 來生成 configure 配置文件

/usr/local/php/bin/phpize --with-php-config=/usr/local/php/bin/php-config
./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
make && make install

若是上面第一步phpize的時候出現
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable is set correctly and then rerun this script.

解決方案:

wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
tar -zvxf m4-1.4.9.tar.gz
cd m4-1.4.9/
./configure && make && make install
cd ../
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
tar -zvxf autoconf-2.62.tar.gz
cd autoconf-2.62/
./configure && make && make install

若是第二步.configure編譯的時候出現php_smart_str.h No such file or directory

git clone https://github.com/php-memcached-dev/php-memcached.git
cd php-memcached/
git checkout php7
/usr/local/php/bin/phpize
./configure --disable-memcached-sasl --with-libmemcached-dir=/usr/local/libmemcached --with-php-config=/usr/local/php/bin/php-config

6、其它可能遇到的問題

NAT模式下沒法上網

輸入ifconfig沒有ipv4地址的話 輸入dhclient從新分配ip,而且要設定爲開機自啓,第一個要檢查/etc/sysconfig/network-scripts/ifcfg-eth*文件BOOTPROTO=dhcp ONBOOT=yes,而後正確的話,就在/etc/rc.d/rc.local文件下追加

if config eth0 up 
dhclient eth0

(後面發現這個自啓好像沒用,每次都要手動執行dhclient,有空再看下)

橋接模式下沒法上網

cd /etc/sysconfig/network-scripts/
vim ifcfg-enp0s3

將其改成動態獲取ip,即爲dhcp,而後設置開啓自啓

BOOTPROTO=dhcp
ONBOOT=yes

最後service network restart

出現windows沒法ping通虛擬機

1.網絡設置 注意要設置成橋接網卡

2.出現相互能夠ping通,可是本地沒法訪問虛擬機的網站
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
而後保存iptables-save


參考文章
1.Linux下安裝php環境而且配置Nginx支持php-fpm模塊
2.CentOS7安裝MySQL
3.linux php安裝memcached擴展

相關文章
相關標籤/搜索