更多>>PHP程序设计 Blog

LNMP环境搭建,安装与配置详解,并设置开机自启动(nginx+mysql+php)

此文包括了lnmp环境搭建所需的软件包和依赖包所对应的官网下载地址,及详细下载、安装步骤,并设置开机自启动。还包括了mysql数据库的基本操作,如数据库、表的创建和导入、导出功能。

零、首先要关闭防火墙和SELinux


1、关闭防火墙


(1)检查防火墙状态

iptables -L -n


(2)临时性关闭防火墙(重启后失效)

service iptables stop  #关闭

service iptables start #开启


(3)永久性关闭防火墙

chkconfig iptables off  #关闭

chkconfig iptables on   #开启

chkconfig ip6tables off  #关闭ipv6的防火墙

chkconfig --list iptables  #查看状态


2、关闭SELinux


(1)检查selinux状态

sestatus


(2)临时性关闭SELinux(重启后失效)

setenforce 0


(3)永久性关闭SELinux

打开配置文件:vi /etc/selinux/config

将 SELINUX=enforcing 修改为:SELINUX=disabled

然后重启机器:reboot 或 shutdown -r now



一、准备工作


1、所需软件包


(1)linux环境:CentOS-6.8-i386-bin-DVD1.iso

官网地址:https://www.centos.org/

点击 Get CentOS Now 按钮,进入如下页面:https://www.centos.org/download/

或点击 More download choices 进入如下页面:http://wiki.centos.org/Download 

比如下面的下载页面:http://mirrors.aliyun.com/centos/6.10/isos/i386/

版本说明:

liveDVD版本:体验版,无需安装到硬盘,插入光盘就可以通过光盘软件并体验各种功能。

bin-DVD版本:普通安装版本,若需要给服务器安装一个centos系统到硬盘,就选择这个版本。

此版本文件很大,包含了大量常用软件,装系统时候就可以直接安装而无需从网络上再下载了。

minimal版本:和bin-DVD一样,只是这个文件里只包含了系统和系统必须的几个基本软件包。

netinstall版本:和bin-DVD一样,但是这个的软件包都需要通过网络下载进行安装。

而bin-DVD镜像自身包含了离线的软件包无需下载。


(2)nginx:nginx-1.14.2.tar.gz

官网地址:http://nginx.org/

点击页面右侧的 download 进入如下页面:http://nginx.org/en/download.html

如右键点击“nginx-1.14.2”,在弹出框中,选择“复制链接地址”,如下:

http://nginx.org/download/nginx-1.14.2.tar.gz


(3)mysql:mysql-5.6.39.tar.gz

官网地址:https://www.mysql.com/

点击 DOWNLOADS 进入如下页面:https://www.mysql.com/downloads/

点击顶部导航菜单的最后一个菜单 Archives 进入如下页面:

https://downloads.mysql.com/archives/

点击左侧列表中的 MySQL Community Server 即MySQL社区服务器,进入如下页面:

https://downloads.mysql.com/archives/community/

在此页面:

Product Version: 可以选择MySQL的版本号,比如选择 5.6.39

Operating System: 可以选择系统使用环境,【注意】这里选择 Source Code

OS Version: 可以选择系统是32位还是64位,比如选择 Generic Linux(Architecture Independent)

在“Compressed TAR Archive”后面,右键点击“ Download”,在弹出框中,选择“复制链接地址”,如下:

https://downloads.mysql.com/archives/get/file/mysql-5.6.39.tar.gz


(4)php:php-7.0.33.tar.gz

官网地址:https://www.php.net/

点击 Downloads 进入:https://www.php.net/downloads.php

页面左侧顶部,有最新版的下载链接,如右键点击 php-7.3.5.tar.gz (sig) 

在弹出框中,选择“复制链接地址”,如下:https://www.php.net/distributions/php-7.3.5.tar.gz

如果想要下载之前的版本,可以在页面右侧,点击“Old archives”,进入:https://www.php.net/releases/

可根据需要选择相应的版本下载。如右键点击“PHP 7.0.33 (tar.gz)”

在弹出框中,选择“复制链接地址”,如下:https://www.php.net/distributions/php-7.0.33.tar.gz


(5)libxml2:libxml2-2.9.9.tar.gz

下载地址:http://xmlsoft.org/downloads.html

在页面右侧,第6行 “Binary ports” 下面一行中

点击 on xmlsoft.org 进入:ftp://xmlsoft.org/libxml2/ 选择相应版本下载


(6)openssl:openssl-1.0.2r.tar.gz

官网地址:https://www.openssl.org/

点击“Downloads”,进入:https://www.openssl.org/source/ 选择相应版本下载


(7)zlib:zlib-1.2.11.tar.gz

官网地址:http://www.zlib.net/

点击“All released versions of zlib”,进入:http://www.zlib.net/fossils/


(8)pcre:pcre-8.43.tar.gz

官网地址:http://www.pcre.org/

点击 Download 下面的 “https://ftp.pcre.org/pub/pcre/” 链接,

进入如下页面:https://ftp.pcre.org/pub/pcre/ 选择相应版本下载


2、创建安装目录


(1)进入根目录:

cd /


(2)在根目录下创建 www 目录,在 www 目录下,创建如下子目录:

web 存放程序代码目录

source 环境安装目录

lnmp 软件包目录

创建目录(直接创建全部目录):

mkdir -p /www/{lnmp,source,web}


3、解压软件包


(1)进入软件包目录:

cd /www/lnmp/


(2)将各软件包上传到 /www/lnmp/ 目录下,并解压

解压软件包(直接全部解压):

find ./*.tar.gz -exec tar zxvf {} \; //注意结尾有分号 ;

单个解压:

tar zxvf nginx-1.14.2.tar.gz

…… ……


4、安装所需工具包


gcc  nginx编译依赖gcc环境


【检测是否安装: rpm -qa | grep gcc】


执行如下安装命令:

yum install -y gcc gcc-c++ make sudo autoconf libtool-ltdl-devel gd-devel freetype-devel libxml2-devel libjpeg-devel libpng-devel openssl-devel curl-devel patch libmcrypt-devel libmhash-devel ncurses-devel bzip2 libcap-devel ntp sysklogd diffutils sendmail iptables unzip cmake


出现如下语句时,表示安装完成:

Complete!

[root@localhost lnmp]#


5、创建用户


(1)创建2个用户,一个用来启动mysql,一个用来启动nginx。


(2)创建mysql用户组和用户:

groupadd mysql

useradd -r mysql -g mysql


(3)创建nginx用户组和用户:

groupadd www

useradd -r www -g www


(4)查看创建的用户

cat /etc/passwd


(5)查看创建的用户组

cat /etc/group



二、安装Nginx


安装nginx前,需要先安装3个依赖包:


pcre:在使用nginx的rewrite模块的时候,需要有pcre库的支持

openssl:在使用ssl功能时,需要有openssl库的支持

zlib:在使用gzip模块时,需要有zlib库的支持


1、安装pcre


(1)进入安装目录

cd /www/lnmp/pcre-8.43


(2)预编译

./configure


(3)编译

make


(4)安装

make install


2、安装openssl


(1)进入安装目录

cd /www/lnmp/openssl-1.0.2r


(2)预编译

./configure


(3)编译

make


(4)安装

make install


3、安装zlib


(1)进入安装目录

cd /www/lnmp/zlib-1.2.11


(2)预编译

./configure


(3)编译

make


(4)安装

make install


4、安装nginx


(1)进入安装目录

cd /www/lnmp/nginx-1.14.2


安装nginx的时候,参数可能会多一些,可以通过进入nginx源码目录,使用 ./configure --help 查看有哪些编译参数。

./configure --help


常用的有以下几个:


--prefix=PATH  要安装的目录

--sbin-path=PATH  指定nginx二进制文件的路径,没指定的话这个路径依赖 --prefix 选项

--conf-path=PATH  如果在命令行未指定配置文件,那么将会通过 --prefix 指定的路径去查找配置文件

--error-log-path=PATH  错误文件路径,nginx写入错误日志文件地址

--pid-path=<path>  nginx master进程pid写入的文件位置,通常在 var/run 下

--user=<user>  worker进程运行的用户

--group=<group>  worker进程运行的组

--with-http_ssl_module  开启ssl模块

--with-zlib=DIR  设置zlib源码目录

--with-openssl=DIR  设置openssl的源码目录

--with-pcre=DIR  设置pcre的源码目录


(2)预编译

./configure --user=www --group=www --prefix=/www/source/nginx --with-pcre=/www/lnmp/pcre-8.43 --with-zlib=/www/lnmp/zlib-1.2.11 --with-openssl=/www/lnmp/openssl-1.0.2r


(3)编译

make


(4)安装

make install


安装完毕后,可以发现 /www/source 目录下多出一个 nginx 目录,说明已经安装成功了。


5、安装完成开始使用nginx


(1)启动nginx

/www/source/nginx/sbin/nginx


(2)查看nginx的版本

/www/source/nginx/sbin/nginx -V


(3)关闭nginx

/www/source/nginx/sbin/nginx -s stop


(4)查看nginx是否启动,若返回值,则已启动,反之则没有启动。

ps -A | grep nginx


6、浏览器测试访问


打开浏览器输入ip 或 localhost,会显示“Welcome to nginx!”的提示语,则说明启动成功。


7、设置nginx开机启动


(1)创建nginx文件

vi /etc/init.d/nginx


(2)编写启动脚本


nginx官方文档启动脚本网址:

https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/


把脚本内容复制进 nginx 文件中


需要修改的2处地方:

第22行 #修改成nginx执行程序的路径

nginx=”/www/source/nginx/sbin/nginx”

第25行 #修改成nginx.conf文件的路径

NGINX_CONF_FILE=”/www/source/nginx/conf/nginx.conf”


按下 ESC 退出,输入: :wq 保存退出


(3)设置文件的执行权限

chmod a+x /etc/init.d/nginx

配置权限前:-rw-r--r--. 1 root root  2700 May  6 08:12 nginx

配置权限后:-rwxr-xr-x. 1 root root  2700 May  6 08:12 nginx


(4)然后就可以通过如下指令控制启动停止

/etc/init.d/nginx start  #启动

/etc/init.d/nginx stop  #停止

/etc/init.d/nginx restart  #重启

/etc/init.d/nginx status  #查看状态


(5)使用chkconfig进行管理

上面的方法虽然完成了用脚本管理nginx服务的功能,还是不太方便。

比如要设置nginx开机启动等,这时可以使用chkconfig来设置。


先将nginx服务加入chkconfig管理列表

chkconfig --add /etc/init.d/nginx


加完这个之后,就可以使用service对nginx进行启动、重启等操作了。

service nginx start  #启动

service nginx stop  #停止

service nginx restart  #重启

service nginx status  #查看状态


(6)最后设置开机启动nginx

chkconfig nginx on



三、安装MySQL


1、说明

MySQL5.5之后就开始使用cMake来代替传统的configure了


MySQL cMake 常规参数介绍:


-DCMAKE_INSTALL_PREFIX= 指向mysql安装目录

-DINSTALL_SBINDIR=sbin 指向可执行文件目录(prefix/sbin)

-DMYSQL_DATADIR=/var/lib/mysql 指向mysql数据文件目录(/var/lib/mysql)

-DSYSCONFDIR=/etc/mysql 指向mysql配置文件目录(/etc/mysql)

-DINSTALL_PLUGINDIR=lib/mysql/plugin 指向插件目录(prefix/lib/mysql/plugin)

-DINSTALL_MANDIR=share/man 指向man文档目录(prefix/share/man)

-DINSTALL_SHAREDIR=share 指向aclocal/mysql.m4安装目录(prefix/share)

-DINSTALL_LIBDIR=lib/mysql 指向对象代码库目录(prefix/lib/mysql)

-DINSTALL_INCLUDEDIR=include/mysql 指向头文件目录(prefix/include/mysql)

-DINSTALL_INFODIR=share/info 指向info文档存放目录(prefix/share/info)

prefix官方推荐设为/usr



2、下面进行编译安装


(1)进入安装目录

cd /www/lnmp/mysql-5.6.39


(2)预编译

cmake -DCMAKE_INSTALL_PREFIX=/www/source/mysql


(3)编译(需要执行30分钟,请耐心等待)

make


(4)安装

make install


3、配置mysql


mysql安装好后并不能直接使用,需要配置一下才能启动。


(1)进入安装好的目录

cd /www/source/mysql


(2)删除系统自带的mysql配置文件

删除前先进行备份

cp /etc/my.cnf /etc/my.cnf_bak


下面执行删除命令

rm -f /etc/my.cnf


(3)初次赋予权限

chown -R mysql:mysql ./


(4)安装数据库


【特别说明:手动输入,否则出现错误】


./scripts/mysql_install_db --user=mysql


【特别说明:手动输入,否则出现错误】


返回结果如下:

	OK
	
	To start mysqld at boot time you have to copy
	support-files/mysql.server to the right place for your system
	
	PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
	To do so, start the server, then issue the following commands:
	
	  ./bin/mysqladmin -u root password 'new-password'
	  ./bin/mysqladmin -u root -h bogon password 'new-password'
	
	Alternatively you can run:
	
	  ./bin/mysql_secure_installation
	
	which will also give you the option of removing the test
	databases and anonymous user created by default.  This is
	strongly recommended for production servers.
	
	See the manual for more instructions.
	
	You can start the MySQL daemon with:
	
	  cd . ; ./bin/mysqld_safe &
	
	You can test the MySQL daemon with mysql-test-run.pl
	
	  cd mysql-test ; perl mysql-test-run.pl
	
	Please report any problems at http://bugs.mysql.com/
	
	The latest information about MySQL is available on the web at

	  http://www.mysql.com

	Support MySQL by buying support/licenses at http://shop.mysql.com

	New default config file was created as ./my.cnf and
	will be used by default by the server when you start it.
	You may edit this file to change server settings


(5)再次赋予权限

chown -R root:root ./*

chown -R mysql:mysql ./data/


4、设置myssql开机启动


(1)将mysql.server加入自启动

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

检测是否复制成功:ls /etc/init.d/


(2)先将mysql服务加入chkconfig管理列表

chkconfig --add /etc/init.d/mysqld


(3)最后设置开机启动nginx

chkconfig mysqld on


(4)修改 linux 的环境变量 

vi /etc/profile


在/etc/profile文件的最后加入一句

export PATH="/www/source/mysql/bin:$PATH"


保存 :wq


下面这句是让配置立即生效

source /etc/profile 


(5)启动mysql

service msyqld start


(6)进入mysql数据库

下面就可以直接操作数据库了,刚安装好 mysql root 是没有密码的


[root@bogon mysql]# mysql -uroot -p

Enter password: 


【注意】此时,直接按下回车键,即可进入,显示如下:


Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.6.39 Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 


5、修改mysql数据库的登录密码


(紧接着上一步的操作)


(1)显示全部数据库

mysql> show databases;


(2)切换到mysql数据库

mysql> use mysql;


(3)显示当前数据库下面的全部表

mysql> show tables;


(4)显示user表的结构

mysql> desc user;


(5)修改密码

mysql 5.7以上版本,已经没有了 password 字段,取而代之的是 authentication_string 字段


修改密码,若是5.6及以下版本:

mysql> update user set password=PASSWORD('test123') where user='root';


修改密码,若是5.7及以上版本:

mysql> update user set authentication_string=PASSWORD('test123') where user='root'; 


(6)新设置用户或更改密码后,刷新MySQL的系统权限相关表,否则会出现拒绝访问

mysql> flush privileges;


(7)退出mysql

mysql> exit;


6、数据库操作


(1)创建数据库

语法:create database if not exists 数据库名称;

mysql> create database if not exists think;


(2)查看全部数据库

语法:show databases;

mysql> show databases;


(3)切换数据库

语法:use 数据库名称;

mysql> use think;

Database changed


(4)查看此数据库下的表

mysql> show tables;

Empty set (0.00 sec)


mysql> 


(5)创建 表

CREATE TABLE if not exists `my_news` (

  `news_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',

  `news_name` varchar(200) NOT NULL COMMENT '标题',

  `news_time` int(11) NOT NULL COMMENT '日期',

  PRIMARY KEY (`news_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='新闻表';


mysql> create table if not exists my_news (news_id int(11) not null auto_increment comment 'ID', news_name varchar(200) not null comment 'biaoti', news_time int(11) not null comment 'date', primary key(news_id)) engine=InnoDB default charset=utf8 comment='news table';


mysql> create table if not exists my_user (user_id int(11) not null auto_increment comment 'ID', user_name varchar(200) not null comment 'biaoti', user_time int(11) not null comment 'date', primary key(user_id)) engine=InnoDB default charset=utf8 comment='user table';


(6)列出表的结构:

mysql> desc my_news;


(7)添加记录:

mysql> insert into my_news(news_name, news_time)values('ASP', '12'),('PHP', 24),('JSP', 56);


mysql> insert into my_user(user_name, user_time)values('AAA', '12'),('BBB', 24),('CCC', 56);


7、数据库导出


(1)进入数据库存放目录

cd /www/source/mysql/data/


(2)导出数据库 think 的数据和表结构

[root@localhost data]# mysqldump -uroot -p think > think.sql

Enter password: 

输入密码,回车即可


会发现存放目录下,多出了一个 think.sql 文件


(3)导出数据库 think 的表结构

[root@localhost data]# mysqldump -uroot -p -d think > think_desc.sql

Enter password: 

输入密码,回车即可


(4)导出数据库 think 下的单张表 my_user

[root@localhost data]# mysqldump -uroot -p think my_news > my_news.sql

Enter password: 

输入密码,回车即可


(5)导出数据库 think 下的单张表 my_user 里的2条记录

默认排序时:

[root@localhost data]# mysqldump -uroot -p think my_news --where="1=1 limit 2" > mu.sql


指定排序时:

[root@localhost data]# mysqldump -uroot -p think my_news --where="1=1 order by news_id desc limit 2" > mu2.sql


(6)导出数据库 think 到指定文件夹


首先创建存放文件夹

mkdir /www/web/_back


执行导出命令

[root@localhost data]# mysqldump -uroot -p think > /www/web/_back/think.sql

Enter password: 

输入密码,回车即可


8、数据库导入


(1)首先创建一个空数据库

[root@localhost _back]# mysql -uroot -p

Enter password:

输入密码,进入数据库


创建数据库命令

mysql> create database if not exists pdme;


(2)非mysql命令行模式导入

退出mysql操作(如果已进入mysql)

mysql> exit;


语法:mysql -uroot -p 数据库名 < 数据库路径

执行代码:

[root@localhost _back]# mysql -uroot -p pdme < /www/web/_back/think.sql 

Enter password: 

输入密码,回车即可


(3)mysql命令行模式导入

选择数据库:

mysql> use pdme2;


设置数据库编码:

mysql> set names utf8;


导入数据库(注意数据库存放路径):

mysql> source /www/web/_back/think.sql;



四、安装PHP


1、首先安装依赖包libxml2


(1)进入安装目录

cd /www/lnmp/libxml2-2.9.9


(2)预编译

./configure --with-python=no --without-zlib


(3)编译

make


(4)安装

make install


2、安装php


(1)进入安装目录

cd /www/lnmp/php-7.0.33


php安装常用的编译参数,可以通过 ./configure --help 查看


--prefix 安装到的目录

--enable-fpm 开始 fpm 模式,nginx 下必需开启

--enable-fpm-user  fpm 的启动账户

--enable-fpm-group fpm 的启动账户组

--with-openssl开启 openssl

--with-libxml-dir 开启 libxml

--with-zlib     开启 zlib

--enable-mbstring开启 mbstring

--with-mysqli=mysqlnd 开启 mysqli

--with-pdo-mysql 开启 pdo mysql

--with-gd 开启gd库

--enable-sockets 开启 sockets

--with-curl 开启 curl

--enable-maintainer-zts 开启 maintainer zts,以后安装多线程的话,这个必须开启


(2)预编译

./configure --prefix=/www/source/php --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-openssl --with-libxml-dir --with-zlib --enable-mbstring --with-mysqli=mysqlnd --enable-mysqlnd --with-pdo-mysql=/www/source/mysql/ --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-sockets --with-curl --enable-maintainer-zts


(3)编译

make


结果会有个提示:

Build complete.

Don't forget to run 'make test'.


(4)编译测试(需要执行25分钟,请耐心等待)

make test


结果会有个提示:

You may have found a problem in PHP.

This report can be automatically sent to the PHP QA team at

http://qa.php.net/reports and http://news.php.net/php.qa.reports

This gives us a better understanding of PHP's behavior.

If you don't want to send the report immediately you can choose

option "s" to save it.  You can then email it to qa-reports@lists.php.net later.

Do you want to send this report now? [Yns]: 

直接输入 n 回车即可:

Do you want to send this report now? [Yns]: n

[root@localhost php-7.0.33]# 


(5)安装

make install


结果如下:

Wrote PEAR system config file at: /www/source/php/etc/pear.conf

You may want to add: /www/source/php/lib/php to your php.ini include_path

/www/lnmp/php-7.0.33/build/shtool install -c ext/phar/phar.phar /www/source/php/bin

ln -s -f phar.phar /www/source/php/bin/phar

Installing PDO headers:           /www/source/php/include/php/ext/pdo/

[root@localhost php-7.0.33]# 


3、配置php


进入php安装目录

cd /www/source/php


复制下面3个配置文件(注意第3个配置文件的路径):


cp etc/php-fpm.conf.default etc/php-fpm.conf

cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf

cp /www/lnmp/php-7.0.33/php.ini-production lib/php.ini


4、设置开机启动php


(1)将php加入自启动

cp /www/lnmp/php-7.0.33/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm


(2)设置权限(因为初始权限为644)

chmod 755 /etc/init.d/php-fpm


(3)先将php服务加入chkconfig管理列表

chkconfig --add /etc/init.d/php-fpm


(4)最后设置开机启动nginx

chkconfig php-fpm on


(5)启动php

service php-fpm start


5、修改linux的环境变量


在 /etc/profile 文件的最后加入:

export PATH="/www/source/php/bin:$PATH"

保存退出


让配置立即生效

source /etc/profile



五、环境测试


1、nginx配置


(1)进入配置文件目录

cd /www/source/nginx/conf/


(2)备份配置文件

cp /www/source/nginx/conf/nginx.conf /www/source/nginx/conf/nginx.conf_bak


(3)修改配置文件

vi /www/source/nginx/conf/nginx.conf


主要修改2处:


第一处:

	location / {
		root   html;
		index  index.html index.htm;
	}

修改成:

	location / {
		root   /www/web;
		index  index.html index.htm index.php;
	}


第二处:

	#location ~ \.php$ {
	#    root           html;
	#    fastcgi_pass   127.0.0.1:9000;
	#    fastcgi_index  index.php;
	#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
	#    include        fastcgi_params;
	#}

修改成:

	location ~ \.php$ {
	#    root           html;
	    fastcgi_pass   127.0.0.1:9000;
	    fastcgi_index  index.php;
		fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  /www/web$fastcgi_script_name;
        fastcgi_param  PATH_TRANSLATED $document_root$fastcgi_path_info;		
	    include        fastcgi_params;
	}


(4)重启nginx服务器

service nginx restart


2、建立测试文件


(1)进入web目录

cd /www/web


(2)新建文件index.php

vi index.php


输入如下代码:

<?php

phpinfo();


3、打开服务器的浏览器进行访问

localhost


会输出相应的phpinfo相关信息

22222.jpg

评论列表

暂时没有相关记录

发表评论

用来接收审核回复提醒,请认真填写

  换一张?
captcha
看不清?点击图片换一张