一步步yum安装LNMP,脱坑笔记!!!
更改國內yum源:
1.備份yum源文件,位置在/etc/yum.repos.d/CentOS-Base.repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak2.獲取阿里的yum源:(centos7為例)
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo3.更新cache
yum makecache4.更新該yum源下所有資源:
yum -y update?
?
?
安裝Nginx
1.更換nginx源為官網源(這步可省略)
To set up the yum repository for RHEL/CentOS, create the file named /etc/yum.repos.d/nginx.repo with the following contents:[nginx] name=nginx repo baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/ gpgcheck=0 enabled=1 Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “6” or “7”, for 6.x or 7.x versions, respectively.2.安裝并啟動nginx:
查詢nginx源、安裝nginx、關閉防火墻、啟動nginx
yum search nginx yum -y install nginx service iptables stop service nginx start?安裝PHP
如果打算安裝5.X版本,直接yum install php就可以。
我這里安裝PHP7.0,所以如下操作:
更新yum源、安裝php7.0及許多依賴
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm yum install php70w-common php70w-fpm php70w-opcache php70w-gd php70w-mysqlnd php70w-mbstring php70w-pecl-redis php70w-pecl-memcached php70w-devel安裝OK后,php -v即可看到 php7的版本信息
?那么問題來了,怎么驗證nginx可以解析php呢?
在nginx默認啟動位置/usr/share/nginx/html下,新建info.php。里面測試代碼如下:
<?php phpinfo(); ?>瀏覽器里訪問http://服務器IP/info.php,發現瀏覽器直接下載了,還是打不開。怎么弄呢?繼續往下看。
在/etc/nginx/conf.d/default.conf中,找到對應代碼修改為如下代碼:
location ~ \.php$ {root /usr/share/nginx/html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}重新啟動nginx、php-fpm。然后重新訪問上一步路徑,效果如下:
?
這樣子,php就安裝完成了。繼續安裝mysql。
?
?
安裝MySQL
執行以下命令:
yum -y install mysql mysql-server ,mysql-devel有時候,會出現mysql-server找不到的錯誤。那么不急,再執行下面這條命令后即可。
rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm yum intsall mysql-server安裝成功后,啟動mysql。并且修改密碼為123.
service mysqld start use mysql; update user set password=password('123') where user='root' and host='localhost'; flush privileges;最后測試下,php能否連接上呢?
在/usr/share/nginx/html下新建mysql.php
<?php $conn=mysql_connect('localhost','root','123') ; if($conn)echo "OK"; elseecho "failed"; ?>結果是鏈接失敗了,php-fpm錯誤日志顯示沒有mysql_connect()函數。怎么解決呢?
因為缺少php-mysql模塊,所以安裝命令為:
yum install php-mysql最后特別需要說明的一點,PHP7以上版本不支持原來的mysql_connect函數。應該使用如下測試代碼,我就陷坑好久!
<?php $mysqli = new mysqli("localhost", "root", "password"); if(!$mysqli) { echo"database error"; }else{ echo"php env successful"; } $mysqli->close(); ?>至此,LNMP環境全部配置完成。快去建站吧!
?
轉載于:https://www.cnblogs.com/phper12580/p/10128512.html
總結
以上是生活随笔為你收集整理的一步步yum安装LNMP,脱坑笔记!!!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASIO 腾空出世 (那些年我们追过的网
- 下一篇: 区块链企业级解决方案 ( Hyperle