Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx是一个高性能的HTTP和反向代理服务器,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
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