本文內容是,如何在Linux centos7下快速搭建LNMP環境。虛擬機、實體機環境均可以。
另外,安裝教程參考的是,下面這篇文章進行文字排版和內容擴充,感謝hcchanqing做者。
CentOS6.2 yum安裝配置LNMP服務器(Nginx+PHP+MySQL)php
特別提醒:本文系統用的Centos7,是7!!參考教程用的是centos6.2html
環境配置mysql
Web環境(LNMP)linux
LNMP 指 Linux + Nginx + Mysql + PHP
LAMP 指 Linux + Apache + Mysql + PHP
安裝以前先配置防火牆,主要能讓windows系統可以訪問80和數據庫3306端口。web
# 注意,下面命令適用於centos7如下,不含centos7 vi /etc/sysconfig/iptables # 編輯防火牆配置文件 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT # 容許80端口經過防火牆 -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT # 容許3306端口經過防火牆
Centos7的設置請點這裏redis
安裝過程太長就不一一寫了,能夠參考這個Vmware 裝Linux教程sql
yum install nginx
service nginx start
沒開成功,出現了下面的一段文本shell
搜了一下,說版本太新,提示命令已經換了。好,那就輸入下面的命令數據庫
/bin/systemctl start nginx.service # 更簡潔的寫法 systemctl start nginx.service
結果什麼都沒有返回,那怎麼驗證nginx服務是否有開啓?
ps -ef | grep nginx # 有返回的話表示已經開啓了
chkconfig nginx on
返回了一串提示,猜想應該是版本太新,命令又換了。
按照提示,輸入。systemctl 相關命令
systemctl enable nginx.service
OK,仍是什麼都沒有返回,看來linux的尿性應該是成功執行通常不會有東西返回的
systemctl is-enabled nginx.service # 驗證是否開啓,有開啓會顯示enabled
在瀏覽器輸入linux的ip地址,若是出現下面的內容,表示nginx搭建OK了。
然而過程並無那麼順利,再獲得welcome頁前,我是打不開頁面的
找了下度娘,果不其然,又是和版本有關,centos的防火牆改爲了firewall
再也不叫iptables
查看原文
鍵入下面命令
firewall-cmd --zone=public --add-port=80/tcp --permanent # 命令含義: # –zone #做用域 # –add-port=80/tcp #添加端口,格式爲:端口/通信協議 # –permanent #永久生效,沒有此參數重啓後失效
而後重啓防火牆,再訪問一下地址就能看到welcome to nginx
systemctl stop firewalld.service systemctl start firewalld.service
接着再按照教程安裝mysql,顯然只要把其中的舊命令換成新命令就能安裝mysql了。
照着這個思路,結果又踩坑了。。。道路真是坎坷
首先跑了下面的命令
yum install mysql mysql-server
第一次安裝過程很正常,還看到了complete(可能我看了一個假的complete)。
而後接下來的啓動服務、設置開機自啓等操做都是返回not found(差很少這個意思,就是沒找到)
再查看mysql的相關進程倒是有的,並且which mysql
也有返回目錄
會不會是名字的緣由?因而折騰以前操做中mysql服務的名字,好比下面的命令
/bin/systemctl start mysqld.service /bin/systemctl start mysql-server.service /bin/systemctl start mysql.service ...
結果確定是掉坑裏了。正解在這:CentOS7下安裝Mysql失敗經歷--CentOS7使用yum安裝和卸載Mysql過程
關鍵的命令Mark一下
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm # step 1 rpm -ivh mysql-community-release-el7-5.noarch.rpm # step 2 yum install mysql-server # step 3
/bin/systemctl start mysqld.service # 更簡潔的寫法 systemctl start mysqld.service # 或者 systemctl start mysqld
systemctl enable mysqld.service systemctl is-enabled mysqld.service # 檢測是否已經設置開機自啓
mysql -u root -p # 登陸帳號 mysql>use mysql; # 進入mysql數據庫 mysql>update user set password=password('要設置的密碼') where user='root' and host='localhost'; # 設置root的帳號密碼爲root mysql>flush privileges; # 不重啓生效 mysql>grant all privileges on *.* to root@'%' identified by '要設置的密碼'; # 設置遠程鏈接帳號 ############################# # 語法 # grant all privileges on 庫名.表名 to '用戶名'@'IP地址' identified by '密碼' with grant option; ############################# # PS:在mysql命令下,要結束或運行命令必定要讓語句結束加上 ; 號
安裝以前,我嘗試查看php版本,發現是有的,版本爲5.4,因此我決定升級PHP的版本
php --version
首先查看php的安裝包(我使用的是yum安裝方式)
yum list installed | grep php
看到的都是5.4的安裝包
安裝前移除當前的安裝包,避免以後的安裝衝突
yum remove php*
因爲默認的YUM源沒法升級PHP,因此須要添加第三方的YUM源,此處用到webtatic
# CentOS 7.x rpm -Uvh http://mirror.webtatic.com/yum/el7/epel-release.rpm rpm -Uvh http://mirror.webtatic.com/yum/el7/webtatic-release.rpm # CentOS 6.5 rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
yum install php71w -y # 基礎 yum install php71w-fpm -y # nginx鏈接使用 yum install php71w-mbstring -y # 寬字節 yum install php71w-mysqlnd -y # mysql相關 yum install php71w-pecl-redis -y # redis擴展 yum install php71w-mcrypt -y # 加密使用 yum install php71w-opcache -y # 性能加速 php5.5 以上使用
或者更短的命令
yum install php71w php71w-fpm php71w-mbstring php71w-mysqlnd php71w-pecl-redis php71w-mcrypt php71w-opcache
systemctl start php-fpm.service
systemctl enable php-fpm.service
cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak # 備份原有配置文件 vi /etc/nginx/nginx.conf # 編輯這個文件 user nginx nginx; # 修改nginx運行帳號爲:nginx組的nginx用戶
按Esc
輸入:wq
保存並退出
緊接着
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.confbak # 備份原有配置文件 vi /etc/nginx/conf.d/default.conf # 編輯
找到location / {
增長index.php
index index.php index.html index.htm;
接着,取消FastCGI server部分location的註釋
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # 要注意fastcgi_param行的參數 改成 $document_root$fastcgi_script_name 或者使用絕對路徑 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
以下
特殊狀況:
配置成上面的方式,個人php文件並不能正常訪問,要修改以下
$document_root$fastcgi_script_name # 替換成絕對路徑 /usr/share/nginx/html$fastcgi_script_name
vi /etc/php.ini
設置中國時區
date.timezone = PRC
cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.confbak # 備份原來的配置文件 vi /etc/php-fpm.d/www.conf # 修改內容以下 user = nginx # 由原來的apache換成nginx group = nginx # 由原來的apache換成nginx
chown nginx.nginx /usr/share/nginx/html/ -R # 設置目錄全部者 chmod 700 /usr/share/nginx/html/ -R # 設置目錄權限
systemctl restart nginx.service # 重啓nginx服務 systemctl restart php-fpm.service # 重啓php服務
在/usr/share/nginx/html/
目錄下放置index.php
文件
在瀏覽器中輸入服務器的ip,OK訪問沒問題 :)
本文使用的系統安裝包:Linux_CentOS7_x64
https://pan.baidu.com/s/1cGfe... 密碼:5sph
文章內容親測有效,也是安裝過程,文章若是有內容不正確或者內容有誤的地方請不吝指出 :)
查看文章最新的內容