標籤:指定 測試 adb iad let 標籤 支援 yum安裝 pac
centos7安裝Lnmp(Linux+Nginx+MySql+Php)及Apache
Nginx是俄羅斯人編寫的十分輕量級的HTTP伺服器,Nginx是一個高效能的HTTP和反向 Proxy伺服器,Nginx 超越 Apache 的高效能和穩定性,使得國內使用 Nginx 作為 Web 服務器的網站也越來越多,
我們學習PHP,以及搭建我們自己的LNMP環境,不妨先在本機上嘗試學習,下面我們一步一步來完成在CentOS7 下安裝LNMP(Linux+Nginx+MySQL+PHP)及Apache。
查看已經開放的連接埠
firewall-cmd --list-ports
查看開放的服務
firewall-cmd --list-services
開啟連接埠
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent
命令含義:
–zone #範圍
–add-port=80/tcp #添加連接埠,格式為:連接埠/通訊協議
–permanent #永久生效,沒有此參數重啟後失效
重啟防火牆
firewall-cmd --reload #重啟firewall
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
關閉SELINUX
vi /etc/selinux/config
注釋掉如下兩句,添加最後一項
\#SELINUX=enforcing #注釋掉
\#SELINUXTYPE=targeted #注釋掉
SELINUX=disabled #增加
:wq! 儲存退出
輸入如下命令
setenforce 0 #使配置立即生效
getenforce 查看
sestatus
setenforce 0
yum安裝 程式過程中 出現佔用yum情況:
rm -f /var/run/yum.pid
一、安裝httpd
sudo su -
yum install -y httpd
二、啟動httpd服務
安裝完成之後使用以下命令啟動httpd服務:
#啟動apache
systemctl start httpd.service
#設定apache開機啟動
systemctl enable httpd.service
可以在瀏覽器中輸入伺服器所在的主機的IP即可看到apache的歡迎介面。
要在另外一台主機上實現這種訪問,需要關閉系統的防火牆。
在CentOS7中只能使用以下命令,如果使用上面的命令並不會報任何錯誤,但是起不到關閉防火牆的效果:
systemctl stop firewalld.service
//禁止防火牆開機啟動
systemctl disable firewalld.service
防火牆開放連接埠和服務
firewall-cmd --add-service=http --permanent --zone=public
firewall-cmd --reload
firewall-cmd --list-all
三、安裝MySql資料庫
MySQL資料庫,新版本已經更名為Mariadb,所以這裡需要安裝Mariadb,可以使用下面的命令進行安裝:
yum install -y mariadb mariadb-server
四、開啟資料庫服務
1、安裝完成以後使用下面的命令開啟資料庫服務:
#啟動MariaDB
systemctl start mariadb.service
#重啟MariaDB
systemctl restart mariadb.service
#設定開機啟動
systemctl enable mariadb.service
2、配置MariaDB的字元集
vim /etc/my.cnf.d/server.cnf
在[mysqld]標籤下添加
init_connect=‘SET collation_connection = utf8_unicode_ci‘
init_connect=‘SET NAMES utf8‘
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
檔案/etc/my.cnf.d/client.cnf
vi /etc/my.cnf.d/client.cnf
在[client]中添加
default-character-set=utf8
檔案/etc/my.cnf.d/mysql-clients.cnf
vi /etc/my.cnf.d/mysql-clients.cnf
在[mysql]中添加
default-character-set=utf8
全部配置完成,重啟mariadb
systemctl restart mariadb
之後進入MariaDB查看字元集
mysql -uroot -p 斷行符號
mysql> show variables like "%character%";show variables like "%collation%";
字元集配置完成。
3、添加使用者,設定許可權
建立使用者命令
mysql>create user [email protected] identified by ‘password‘;
直接建立使用者並授權的命令
mysql>grant all on *.* to [email protected] identified by ‘password‘;
授予外網登陸許可權
mysql>grant all privileges on *.* to [email protected]‘%‘ identified by ‘password‘;
授予許可權並且可以授權
mysql>grant all privileges on *.* to [email protected]‘hostname‘ identified by ‘password‘ with grant option;
簡單的使用者和許可權配置基本就這樣了。
其中只授予部分許可權把 其中 all privileges或者all改為select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。
設定root賬戶密碼
mysql_secure_installation
然後一路輸入y就可以。
設定root密碼後,重啟MariaDB生效
systemctl restart mariadb.service
測試訪問資料庫:
mysql -uroot -p
show databases;
退命令:
exit;
五、安裝PHP
yum -y install php
使用下面的命令安裝php對Mariadb的支援:
yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
一路“Y”
php --version
五、重啟Mariadb和httpd服務:
使用下面的命令重啟Mariadb和httpd服務:
#重啟MariaDB
systemctl restart mariadb.service
#重啟apache
systemctl restart httpd.service
vim /var/www/html/index.php ->內容:<?php phpinfo(); ?>
六、安裝nginx
想在 CentOS 系統上安裝 Nginx ,你得先去添加一個資產庫,像這樣:
vim /etc/yum.repos.d/nginx.repo
使用 vim 命令去開啟 /etc/yum.repos.d/nginx.repo ,如果 nginx.repo 不存在,就會去建立一個這樣的檔案,開啟以後按一下小 i 鍵,進入編輯模式,然後複製粘貼下面這幾行代碼,完成以後按 esc 鍵退出,再輸入 :wq (儲存並退出)
[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=1
完成以後,我們就可以使用 yum 命令去安裝 nginx 了,像這樣:
yum -y install nginx
由於安裝了Httpd服務,所以要先停止,關閉apache之後再次啟動nginx。
停止Httpd
systemctl stop httpd.service
測試一下 nginx 服務:
systemctl status httpd.service
測試一下 nginx 的設定檔:
nginx -t
返回
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
說明設定檔成功!
開啟服務:systemctl start nginx.service
開機啟動:systemctl enable nginx.service
七、佈建服務
這裡使用的是nginx做反向 Proxy,將其用戶端通過80連接埠請求的.php內容代理到apache伺服器上。
要想使用nginx做反向 Proxy,需要修改Apache的httpd和nginx的設定檔,使其監聽不同的連接埠,這裡我們使用nginx監聽80連接埠,
使用Apache監聽8080連接埠,這裡我們分別配置Apache和nginx的設定檔,修改結果如下:
(1)Apache設定檔:/etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80Listen 8080
(2)nginx配置如下:/etc/nginx/conf.d/default.conf
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # location ~ \.php$ { proxy_pass http://127.0.0.1:8080; }
(3)不指定8080連接埠訪問nginx伺服器:
http://localhost
(4)指定8080連接埠訪問apache伺服器:
http://localhost:8080
(5)指定8080連接埠訪問apache伺服器下的mysql->mariadb
http://locahost:8080/phpmyadmin
參考:http://www.kimsom.com/article/138
http://www.jb51.net/article/68026.htm
添加nginx服務:
http://www.cnblogs.com/riverdubu/p/6426852.html
centos7安裝Lnmp(Linux+Nginx+MySql+Php+phpMyAdmin+Apache)