歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> centos6.7、centos6.5下yum方式搭建lnmp環境,php5.4/mysql5.5

centos6.7、centos6.5下yum方式搭建lnmp環境,php5.4/mysql5.5

日期:2017/3/1 11:52:14   编辑:關於Linux
由於電腦重裝系統,之前虛擬機中的所有系統都打不開了,不得已又得從0開始安裝。我下載的是centos6.5的鏡像,在虛擬機中安裝完以後,執行了一下yum update,系統升成了6.7的,但是對於lnmp環境的安裝時沒有影響的,6.5和6.7的安裝方法是一樣的。 一、安裝php5.4 1.配置yum源 6.7和6.5中默認是沒有php5.4版本的,所以我們需要先配置一下yum源。 執行命令: rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm [plain] view plain copy [root@localhost yum.repos.d]# rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm Retrieving http://mirror.webtatic.com/yum/el6/latest.rpm warning: /var/tmp/rpm-tmp.qxPwK1: Header V4 DSA/SHA1 Signature, key ID cf4c4ff9: NOKEY Preparing... ########################################### [100% 1:webtatic-release ########################################### [100%] 2.安裝php5.4 執行完以後查看yum search php54w,可以看到php5.4相關的模塊。執行命令安裝php5.4 [plain] view plain copy [root@localhost ~]# yum install php54w 安裝完後,執行命令可以看到PHP版本信息 [html] view plain copy [root@localhost ~]# php --version PHP 5.4.45 (cli) (built: Oct 16 2015 11:24:58) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies 3.安裝php-fpm [html] view plain copy [root@localhost ~]# yum install php-fpm.x86_64 Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile f * base: mirrors.btte.net * extras: mirrors.btte.net * updates: mirror.bit.edu.cn * webtatic: sp.repo.webtatic.com Resolving Dependencies --> Running transaction check ---> Package php-fpm.x86_64 0:5.3.3-46.el6_7.1 will be installed --> Processing Dependency: php-common(x86-64) = 5.3.3-46.el6_7.1 for package: php-fpm-5.3.3-46.el6_7.1.x86_64 --> Running transaction check ---> Package php-common.x86_64 0:5.3.3-46.el6_7.1 will be installed --> Processing Conflict: php54w-common-5.4.45-2.w6.x86_64 conflicts php-common < 5.4.0 --> Finished Dependency Resolution Error: php54w-common conflicts with php-common-5.3.3-46.el6_7.1.x86_64 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest 可以看到倒數第三行報了一個沖突,導致安裝失敗,沖突的原因是php-fpm包依賴5.3版本的php-common,但是我們安裝php5.4的時候安裝了5.4版本的php-common,所以造成php-common-5.3安裝不上,解決這個沖突只要安裝依賴php-common-5.4版本的php-fpm就OK [html] view plain copy [root@localhost ~]# yum search php54w | grep fpm php54w-fpm.x86_64 : PHP FastCGI Process Manager [root@localhost ~]# yum install php54w-fpm.x86_64 Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirrors.btte.net * updates: mirror.bit.edu.cn * webtatic: sp.repo.webtatic.com Resolving Dependencies --> Running transaction check ---> Package php54w-fpm.x86_64 0:5.4.45-2.w6 will be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================================================================== Package Arch Version Repository Size ==================================================================================================================================================== Installing: php54w-fpm x86_64 5.4.45-2.w6 webtatic 1.3 M Transaction Summary ==================================================================================================================================================== Install 1 Package(s) Total download size: 1.3 M Installed size: 3.8 M Is this ok [y/N]: y Downloading Packages: php54w-fpm-5.4.45-2.w6.x86_64.rpm | 1.3 MB 00:13 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : php54w-fpm-5.4.45-2.w6.x86_64 1/1 Verifying : php54w-fpm-5.4.45-2.w6.x86_64 1/1 Installed: php54w-fpm.x86_64 0:5.4.45-2.w6 Complete! 測試php-fpm服務是否正常 [html] view plain copy [root@localhost ~]# service php-fpm start Starting php-fpm: [ OK ] 一切順利!至此php環境就安裝完畢,接下來就安裝nginx 如果出現“Fatal error: Call to undefined function mb_strlen()"錯誤 還需要安裝php-mbstring [root@iZ25fe2mpugZ ~]# yum search php54w| grep mbstring php54w-mbstring.x86_64 : A module for PHP applications which need multi-byte [root@iZ25fe2mpugZ ~]# yum install php54w-mbstring.x86_64 如果出現“ Unknown: open(/var/lib/php/session/sess_63u4vit4pii5gelmsq5inf3hd6, O_RDWR) failed: Permission denied”這樣的錯誤,需要將session目錄設置777權限。chmod -R 777 /var/lib/php/session 以此類推,出現“Class 'DOMDocument' not found”,表示xml擴展沒有安裝 yum search php54w | grep xml yum install php54w-mbstring.x86_64 二.安裝nginx 先看看我們都有哪些版本的Nginx [html] view plain copy [root@localhost ~]# yum search nginx Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirrors.btte.net * updates: mirror.bit.edu.cn * webtatic: uk.repo.webtatic.com ================================================================ N/S Matched: nginx ================================================================ nginx16.x86_64 : A high performance web server and reverse proxy server nginx18.x86_64 : A high performance web server and reverse proxy server Name and summary matches only, use "search all" for everything. 我們選擇安裝18版本的。 安裝的時候發現下載速度奇慢,最後也因為這個安裝失敗,所以我們現在先替換一下yum源。 [html] view plain copy rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm 替換完以後再執行安裝。這次下載速度快了很多,安裝也很順利。全部安裝完以後,開啟nginx服務 [html] view plain copy [root@localhost yum.repos.d]# service nginx start Starting nginx: [ OK ] 關掉防火牆服務 [html] view plain copy [root@localhost yum.repos.d]# service iptables stop iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] 在浏覽器中輸入虛擬機的ip,當查看到到下圖的時候表示Nginx已經安裝成功。 三.修改nginx 配置,使其能夠識別php腳本 默認安裝完nginx以後,是沒辦法解析php腳本的,需要修改nginx的配置文件才行。 [html] view plain copy [root@localhost html]# vim /etc/nginx/nginx.conf 1.添加index.php 2.打開FastCGI部分注釋(FastCGI、Nginx、php-fpm、php都有哪些關系?稍後文章會詳細介紹這一點) 將/scripts改為$document_root 保存退出,重啟nginx服務。 進入/usr/share/nginx/html下寫一個php腳本,然後測試看能不能打開 [html] view plain copy [root@localhost html]# vim index.php [php] view plain copy phpinfo(); 保存退出,在浏覽器中訪問該文件,192.168.71.128/index.php(我的虛擬機IP是71.128),如果看到php信息頁就表示已經配置成功。接下來就該配置mysql啦。 四.安裝MySQL 安裝mysql5.5相關的所有的包 [html] view plain copy [root@localhost ~]# yum install mysql55* Loaded plugins: fastestmirror, refresh-packagekit, security Setting up Install Process Loading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirrors.btte.net * updates: mirror.bit.edu.cn * webtatic: sp.repo.webtatic.com Resolving Dependencies --> Running transaction check ---> Package mysql55w.x86_64 0:5.5.49-1.w6 will be installed ---> Package mysql55w-bench.x86_64 0:5.5.49-1.w6 will be installed --> Processing Dependency: perl(GD) for package: mysql55w-bench-5.5.49-1.w6.x86_64 --> Processing Dependency: perl(DBI) for package: mysql55w-bench-5.5.49-1.w6.x86_64 ---> Package mysql55w-devel.x86_64 0:5.5.49-1.w6 will be installed --> Processing Dependency: openssl-devel for package: mysql55w-devel-5.5.49-1.w6.x86_64 ---> Package mysql55w-embedded.x86_64 0:5.5.49-1.w6 will be installed ---> Package mysql55w-embedded-devel.x86_64 0:5.5.49-1.w6 will be installed ---> Package mysql55w-libs.x86_64 0:5.5.49-1.w6 will be installed --> Processing Dependency: libmysqlclient16 for package: mysql55w-libs-5.5.49-1.w6.x86_64 ---> Package mysql55w-server.x86_64 0:5.5.49-1.w6 will be installed --> Processing Dependency: perl-DBD-MySQL for package: mysql55w-server-5.5.49-1.w6.x86_64 ---> Package mysql55w-test.x86_64 0:5.5.49-1.w6 will be installed --> Processing Dependency: perl(Time::HiRes) for package: mysql55w-test-5.5.49-1.w6.x86_64 --> Running transaction check ---> Package libmysqlclient16.x86_64 0:5.1.69-1.w6 will be installed ---> Package openssl-devel.x86_64 0:1.0.1e-42.el6_7.4 will be installed --> Processing Dependency: zlib-devel for package: openssl-devel-1.0.1e-42.el6_7.4.x86_64 --> Processing Dependency: krb5-devel for package: openssl-devel-1.0.1e-42.el6_7.4.x86_64 ---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed ---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed ---> Package perl-GD.x86_64 0:2.44-3.el6 will be installed ---> Package perl-Time-HiRes.x86_64 4:1.9721-141.el6_7.1 will be installed --> Running transaction check ---> Package krb5-devel.x86_64 0:1.10.3-42z1.el6_7 will be installed --> Processing Dependency: libselinux-devel for package: krb5-devel-1.10.3-42z1.el6_7.x86_64 --> Processing Dependency: libcom_err-devel for package: krb5-devel-1.10.3-42z1.el6_7.x86_64 --> Processing Dependency: keyutils-libs-devel for package: krb5-devel-1.10.3-42z1.el6_7.x86_64 ---> Package zlib-devel.x86_64 0:1.2.3-29.el6 will be installed --> Running transaction check ---> Package keyutils-libs-devel.x86_64 0:1.4-5.el6 will be installed ---> Package libcom_err-devel.x86_64 0:1.41.12-22.el6 will be installed ---> Package libselinux-devel.x86_64 0:2.0.94-5.8.el6 will be installed --> Processing Dependency: libsepol-devel >= 2.0.32-1 for package: libselinux-devel-2.0.94-5.8.el6.x86_64 --> Processing Dependency: pkgconfig(libsepol) for package: libselinux-devel-2.0.94-5.8.el6.x86_64 --> Running transaction check ---> Package libsepol-devel.x86_64 0:2.0.41-4.el6 will be installed --> Processing Conflict: mysql55w-libs-5.5.49-1.w6.x86_64 conflicts mysql-libs < 5.5 --> Finished Dependency Resolution Error: mysql55w-libs conflicts with mysql-libs-5.1.73-5.el6_7.1.x86_64 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest 倒數第三行,又出現一個沖突,現在我們先卸載掉mysql-libs-5.1.73-5.el6_7.1.x86_64 [html] view plain copy [root@localhost ~]# yum remove mysql-libs-5.1.73-5.el6_7.1.x86_64 卸載成功後,再次執行安裝命令 [html] view plain copy [root@localhost ~]# yum install mysql55* 安裝完以後開啟mysql服務,查看是否正常 [html] view plain copy [root@localhost ~]# service mysqld start 啟動成功後,我們需要給mysql的root用戶分配登錄密碼 [html] view plain copy [root@localhost ~]# mysqladmin -u root password 'newpassword' 設置完成後就可以登錄mysql控制台了 [html]view plain copy [root@localhost ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.49 MySQL Community Server (GPL) 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> 登錄沒問題,至此MySQL也就安裝完畢了,接下來要做的就是讓php能夠連接上MySQL 五、安裝php-mysql擴展 1.安裝擴展 查看是否有安裝php-mysql擴展 [html]view plain copy [root@localhost ~]# php -i | grep mysql 命令結果返回空,說明沒有安裝,那麼現在我們就需要安裝這些擴展。搜索yum源,找到合適的擴展包 [html]view plain copy [root@localhost ~]# yum search all php-mysql Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile * base: mirrors.btte.net * extras: mirrors.btte.net * updates: mirror.bit.edu.cn * webtatic: us-east.repo.webtatic.com ================================================================ Matched: php-mysql ================================================================ php-mysql.x86_64 : A module for PHP applications that use MySQL databases php54w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases php55w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases php56w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases php70w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases 安裝php54w-mysqlnd,因為我們php是php5.4版本的 [html]view plain copy [root@localhost ~]# yum install -y php54w-mysqlnd.x86_64 安裝完以後重啟php-fpm,再次浏覽192.168.71.128/index.php就可以看到mysql擴展啦。 下面就寫個簡單腳本測試一下php連接mysql的情況。 2.測試擴展 首先登錄mysql控制台,在test中創建一張表,並寫入幾條數據 [sql]view plain copy mysql> create table person( -> id int not null primary key auto_increment, -> name char(5) not null -> )engine=innodb default charset=utf8;[sql]view plain copy mysql> insert into person (name) values ('zhangsan'),('lisi'),('wangwu'); 測試表准備好以後,開始寫測試腳本 [html]view plain copy [root@localhost ~]# cd /usr/share/nginx/html/ [root@localhost html]# vim mysql.php[php]view plain copy try{ $pdo = new PDO('mysql:host=localhost;dbname=test','root','root'); $res = $pdo->query('select * from person'); foreach ($res as $row) { echo ' ';print_r($row); } } catch (PDOException $e) { print "Error:".$e->getMessage()." "; die; } 訪問腳本192.168.71.128/mysql.php,如果看到下面內容,說明php-mysql擴展已經沒有問題。 六、將lnmp加入開機啟動項 經過上面五部的操作,我們的lnmp環境就已經成功搭建完成了,為了後續的使用,我們現在把php-fpm/nginx/mysql都加入到開機啟動項中。 [html]view plain copy [root@localhost html]# chkconfig php-fpm on [root@localhost html]# chkconfig nginx on [root@localhost html]# chkconfig mysqld on 注:如果重啟機器後發現nginx訪問不了了,請檢查一下防火牆是不是打開了,因為centos默認防火牆是開機啟動的。 至此,lnmp環境就徹底的搭建完成啦,盡情的享受coding吧~
Copyright © Linux教程網 All Rights Reserved