Nginx是俄羅斯人編寫的十分輕量級的HTTP服務器,Nginx是一個高性能的HTTP和反向代理服務器,Nginx 超越 Apache 的高性能和穩定性,使得國內使用 Nginx 做爲 Web 服務器的網站也愈來愈多,php
咱們學習PHP,以及搭建咱們本身的LNMP環境,不妨先在本機上嘗試學習,下面咱們一步一步來完成在CentOS7 下安裝LNMP(Linux+Nginx+MySQL+PHP)及Apache。html
查看已經開放的端口
firewall-cmd --list-ports
查看開放的服務
firewall-cmd --list-servicesmysql
開啓端口
firewall-cmd --zone=public --add-port=80/tcp --permanentlinux
firewall-cmd --zone=public --add-port=3306/tcp --permanentnginx
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開機啓動sql
關閉SELINUX
vi /etc/selinux/config
註釋掉以下兩句,添加最後一項
\#SELINUX=enforcing #註釋掉
\#SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增長
:wq! 保存退出
輸入以下命令
setenforce 0 #使配置當即生效數據庫
getenforce 查看apache
sestatusvim
setenforce 0centos
yum安裝 程序過程當中 出現佔用yum狀況:
rm -f /var/run/yum.pid
1、安裝httpd
sudo su -
yum install -y httpd
2、啓動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
3、安裝MySql數據庫
MySQL數據庫,新版本已經改名爲Mariadb,因此這裏須要安裝Mariadb,可使用下面的命令進行安裝:
yum install -y mariadb mariadb-server
4、開啓數據庫服務
一、安裝完成之後使用下面的命令開啓數據庫服務:
#啓動MariaDB
systemctl start mariadb.service
#重啓MariaDB
systemctl restart mariadb.service
#設置開機啓動
systemctl enable mariadb.service
二、配置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%";
字符集配置完成。
三、添加用戶,設置權限
建立用戶命令
mysql>create user username@localhost identified by 'password';
直接建立用戶並受權的命令
mysql>grant all on *.* to username@localhost identified by 'password';
授予外網登錄權限
mysql>grant all privileges on *.* to username@'%' identified by 'password';
授予權限而且能夠受權
mysql>grant all privileges on *.* to username@'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;
5、安裝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
5、重啓Mariadb和httpd服務:
使用下面的命令重啓Mariadb和httpd服務:
#重啓MariaDB
systemctl restart mariadb.service
#重啓apache
systemctl restart httpd.service
vim /var/www/html/index.php ->內容:<?php phpinfo(); ?>
6、安裝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 repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=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 ok nginx: configuration file /etc/nginx/nginx.conf test is successful
說明配置文件成功!
開啓服務:systemctl start nginx.service
開機啓動:systemctl enable nginx.service
7、配置服務
這裏使用的是nginx作反向代理,將其客戶端經過80端口請求的.php內容代理到apache服務器上。
要想使用nginx作反向代理,須要修改Apache的httpd和nginx的配置文件,使其監聽不一樣的端口,這裏咱們使用nginx監聽80端口,
使用Apache監聽8080端口,這裏咱們分別配置Apache和nginx的配置文件,修改結果以下:
(1)Apache配置文件:/etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80 Listen 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