centos7_LNMP环境部署教程

环境centos7
LNMP 环境(PHP7 + MySQL5.7 + Nginx1.10)过程笔记。

安装nginx

yum install -y nginx

浏览器打开ip没有欢迎界面
查看端口
bash: netstat: command not found
解决网络工具没有安装.执行下面命令就可以了.
yum install net-tools

查看安装目录

 ps  -ef | grep nginx
root      1631     1  0 05:50 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     1632  1631  0 05:50 ?        00:00:00 nginx: worker process
root     26230  1437  0 06:06 pts/0    00:00:00 grep --color=auto nginx

查找nginx配置文件路径

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

查看主机根目录位置

cat /etc/nginx/nginx.conf

在server下有

 root   /usr/share/nginx/html

HTTP ERROR 502,网管错误
关闭防火墙

systemctl stop firewalld.service

注意,应该开启防火墙并开放端口,此处只为测试,故省略

再通过ip访问,可以看到nginx的欢迎界面

Welcome to nginx on Fedora!

可以在配置文件同目录中添加文件夹vhost,然后在里面添加.conf文件,分别配置不同的虚拟主机

include vhost/*.conf
systemctl enable nginx

PHP安装

Nginx本身是不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端

rpm 安装 Php7 相应的 yum源

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

安装php7.0

yum install -y php70w

安装php扩展

yum install -y  php70w-mysql.x86_64   php70w-gd.x86_64   php70w-ldap.x86_64   php70w-mbstring.x86_64  php70w-mcrypt.x86_64

安装PHP FPM

​ yum install -y php70w-fpm

启动php-fpm(必须,不然nginx不识别PHP)

systemctl start php-fpm

php-fpm开机启动

 systemctl enable php-fpm

在nginx配置文件的server中末尾添加

 # 这里新加的
 # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
 # Fastcgi服务器和程序(PHP,Python)沟通的协议.
location ~ \.php$ {
 # 设置监听端口
fastcgi_pass   127.0.0.1:9000;
# 设置nginx的默认首页文件(上面已经设置过了,可以删除)
fastcgi_index  index.php;
# 设置脚本文件请求的路径
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
# 引入fastcgi的配置文件
 include        fastcgi_params;
}

重启nginx

systemctl restart nginx

PHP的安装目录在/etc/php.d,配置文件是/etc/php.ini

mysql安装

wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz

tar -zxvf mysql-5.6.37-linux-glibc2.12-86_64.tar.gz
文件名最好复制ls获取的

将解压的安装包移动到/usr/local/目录下并改名为mysql

mv mysql-5.6.37-linux-glibc2.12-x86_64 /usr/local/mysql 
rm mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz 

cd /usr/local/mysql

删除上一个mysql遗留

rm -rf /etc/my.cnf.rpmsave 

复制mysql配置文件到系统环境配置目录下,复制前,检查/etc/下是否有my.cnf,如果有就删除

 cd /etc/local/mysql/mysql-5.6.37-linux-glibc2.12-x86_64/
cp support-files/my-default.cnf /etc/my.cnf 

安装一个需要的依赖库

yum -y install autoconf  

安装mysql

cd /usr/local/mysql/mysql-5.6.37-linux-glibc2.12-x86_64/
./scripts/mysql_install_db --user=root --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --defaults-file=/etc/my.cnf
FATAL ERROR: Could not find my-default.cnf

安装失败错误解决办法:

cp -r /usr/local/mysql/mysql-5.6.37-linux-glibc2.12-x86_64/. /usr/local/mysql
./scripts/mysql_install_db --user=root --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 安装数据库  

安装成功
错误原因
应该将解压的安装包移动到/usr/local/目录下并改名为mysql

mv mysql-5.6.37-linux-glibc2.12-x86_64 /usr/local/mysql

编辑配置文件/etc/my.cnf

[client]
socket=/tmp/mysql.sock 
[mysql]  
# 设置mysql客户端默认字符集  
default-character-set=utf8     
[mysqld]  
skip-name-resolve 
user = root
#设置3306端口  
port = 3306    
socket=/tmp/mysql.sock 
# 设置mysql的安装目录  
basedir=/usr/local/mysql  
# 设置mysql数据库的数据的存放目录  
datadir=/usr/local/mysql/data  
# 允许最大连接数  
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集  
character-set-server=utf8  
# 创建新表时将使用的默认存储引擎  
default-storage-engine=INNODB  
lower_case_table_name=1  
max_allowed_packet=16M 
log-error = /usr/local/mysql/data/error.log         
pid-file = /usr/local/mysql/data/mysql.pid 

赋予配置文件最大权限

ls /etc/my.cnf -all
-rw-r--r-- 1 root root 765 Jul 19 08:49 /etc/my.cnf
[root@nginx mysql]# chown -R 777 /etc/my.cnf 

复制服务脚本,进系统环境

cp support-files/mysql.server /etc/rc.d/init.d/mysqld 

赋予服务控制执行脚本的权利

chmod +x /etc/rc.d/init.d/mysqld   

添加服务进系统服务

$ chkconfig --add mysqld   

查看服务是否生效

$ chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off   (出现这个证明正常生效)

开启服务

systemctl start mysqld  ($ systemctl status mysqld查看服务状态)

打开环境变量配置文件

vim ~/.bash_profile
export PATH=$PATH:/usr/local/mysql/bin (在最后面添加路径,加入环境变量)

执行下面的命令是修改的内容立即生效:

source ~/.bash_profile

第一次登陆不用密码,直接回车
$ mysql -u root

set password = password(‘520140’);
exit;

登录则使用 mysql -u root -p
输入密码回车即可

mysql -u root -p 输入密码即可登陆
在mysql控制台执行,开启root访问权限示例

>grant all privileges on *.* to 'root'@'%' identified by '520140(改成自己的密码)' with grant option;
> flush privileges;
刷新一下权限,不用重启MySql服务。

闲言碎语

到此,所有的LNMP环境配置结束,接下来还有Nginx多站点配置和SSL的配置教程。

点赞

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注