Centos7编译安装Nginx+Mysql+PHP多版本环境

lnmp环境安装配置

Centos7编译安装Nginx1.10.3+MySQL5.7.16+PHP5.5 5.6 7.0 7.1多版本环境教程
准备篇

1、防火墙配置

详情看

network技术学习分享Centos7设置关闭firewall安装iptables篇

 

2、系统约定

软件源代码包存放位置:/usr/local/src

源码包编译安装位置:/usr/local/软件名字

3、下载软件包

1、下载nginx教程

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

2、下载MySQL

https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.16.tar.gz

3、下载php

http://mirrors.sohu.com/php/php-7.1.3.tar.gz

http://mirrors.sohu.com/php/php-7.0.17.tar.gz

http://mirrors.sohu.com/php/php-5.6.30.tar.gz

http://mirrors.sohu.com/php/php-5.5.38.tar.gz

4、下载cmake(MySQL编译工具)

https://cmake.org/files/v3.7/cmake-3.7.2.tar.gz

5、下载pcre (支持nginx伪静态)

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz

6、下载openssl(nginx扩展)

https://www.openssl.org/source/openssl-1.1.0e.tar.gz

7、下载zlib(nginx扩展)

http://www.zlib.net/zlib-1.2.11.tar.gz

8、下载libmcrypt(php扩展)

https://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz

9、下载yasm(php扩展)

http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz

10、t1lib(php扩展)

http://download.freenas.org/distfiles/t1lib-5.1.2.tar.gz

11、下载gd库安装包

https://github.com/libgd/libgd/releases/download/gd-2.1.1/libgd-2.1.1.tar.gz

12、libvpx(gd库需要)

https://codeload.github.com/webmproject/libvpx/tar.gz/v1.3.0

13、tiff(gd库需要)

http://download.osgeo.org/libtiff/tiff-4.0.7.tar.gz

14、libpng(gd库需要)

ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng16/libpng-1.6.28.tar.gz

15、freetype(gd库需要)

http://download.savannah.gnu.org/releases/freetype/freetype-2.7.1.tar.gz

16、jpegsrc(gd库需要)

http://www.ijg.org/files/jpegsrc.v9b.tar.gz

17、Boost(编译mysql需要)

https://ufpr.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

以上软件包上传到/usr/local/src目录

4、安装编译工具及库文件(使用yum命令安装)

yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libXaw-devel libXmu-devel libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel

安装篇

以下是用远程连接工具远程登录到服务器,在命令行下面操作的

一、安装mysql教程

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
成功安装之后重启mysql服务

service mysqld restart

初次安装mysql是root账户是没有密码的
设置密码的方法:

输入mysql -uroot进入mysql数据库
mysql> set password for ‘root’@‘localhost’ = password(‘newpasswd’);

远程授权连接mysql
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root’@’localhost’ IDENTIFIED BY ‘你要设置的数据库密码’ WITH GRANT OPTION;

mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘你要设置的数据库密码’ WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES; #更新权限

 

二、安装Nginx教程

1、安装pcre

cd /usr/local/src

mkdir /usr/local/pcre

tar zxvf pcre-8.40.tar.gz

cd pcre-8.40

./configure –prefix=/usr/local/pcre

make

make install

2、安装openssl

cd /usr/local/src

mkdir /usr/local/openssl

tar zxvf openssl-1.1.0e.tar.gz

cd openssl-1.1.0e

./config –prefix=/usr/local/openssl

make

make install

vi /etc/profile

export PATH=$PATH:/usr/local/openssl/bin

:wq!

source /etc/profile

3、安装zlib

cd /usr/local/src

mkdir /usr/local/zlib

tar zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11

./configure –prefix=/usr/local/zlib

make

make install

4、安装Nginx

groupadd www

useradd -g www www -s /bin/false

cd /usr/local/src

tar zxvf nginx-1.10.3.tar.gz

cd nginx-1.10.3

./configure –prefix=/usr/local/nginx –without-http_memcached_module –user=www –group=www –with-http_stub_status_module –with-http_ssl_module –with-http_gzip_static_module –with-openssl=/usr/local/src/openssl-1.1.0e –with-zlib=/usr/local/src/zlib-1.2.11 –with-pcre=/usr/local/src/pcre-8.40

注意:–with-openssl=/usr/local/src/openssl-1.1.0e –with-zlib=/usr/local/src/zlib-1.2.11 –with-pcre=/usr/local/src/pcre-8.40指向的是源码包解压的路径,而不是安装的路径,否则会报错

make

make install

/usr/local/nginx/sbin/nginx #启动Nginx

设置nginx开机启动

vi /etc/rc.d/init.d/nginx #编辑启动文件添加下面内容

############################################################

#!/bin/sh

#

# nginx – this script starts and stops the nginx daemon

#

# chkconfig: – 85 15

# description: Nginx is an HTTP(S) server, HTTP(S) reverse \

# proxy and IMAP/POP3 proxy server

# processname: nginx

# config: /etc/nginx/nginx.conf

# config: /usr/local/nginx/conf/nginx.conf

# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ “$NETWORKING” = “no” ] && exit 0

nginx=”/usr/local/nginx/sbin/nginx”

prog=$(basename $nginx)

NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {

# make required directories

user=`$nginx -V 2>&1 | grep “configure arguments:” | sed ‘s/[^*]*–user=\([^ ]*\).*/\1/g’ -`

if [ -z “`grep $user /etc/passwd`” ]; then

useradd -M -s /bin/nologin $user

fi

options=`$nginx -V 2>&1 | grep ‘configure arguments:’`

for opt in $options; do

if [ `echo $opt | grep ‘.*-temp-path’` ]; then

value=`echo $opt | cut -d “=” -f 2`

if [ ! -d “$value” ]; then

# echo “creating” $value

mkdir -p $value && chown -R $user $value

fi

fi

done

}

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

make_dirs

echo -n $”Starting $prog: ”

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

[ $retval -eq 0 ] && touch $lockfile

return $retval

}

stop() {

echo -n $”Stopping $prog: ”

killproc $prog -QUIT

retval=$?

echo

[ $retval -eq 0 ] && rm -f $lockfile

return $retval

}

restart() {

#configtest || return $?

stop

sleep 1

start

}

reload() {

#configtest || return $?

echo -n $”Reloading $prog: ”

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

status $prog

}

rh_status_q() {

rh_status >/dev/null 2>&1

}

case “$1″ in

start)

rh_status_q && exit 0

$1

;;

stop)

rh_status_q || exit 0

$1

;;

restart|configtest)

$1

;;

reload)

rh_status_q || exit 7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit 0

;;

*)

echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”

exit 2

esac

############################################################

:wq! #保存退出

chmod 775 /etc/rc.d/init.d/nginx #赋予文件执行权限

chkconfig nginx on #设置开机启动

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

在浏览器中打开服务器IP地址,会看到下面的界面,说明Nginx安装成功。

扩展阅读:

Nginx配置txt、pdf、doc、xls等文件直接下载的方法

在nginx配置文件中添加以下代码

location / {

if ($request_filename ~* ^.*?\.(txt|pdf|doc|xls)$){

add_header Content-Disposition: ‘attachment;’;

}

}

 

成为第一个发表评论的人

发表评论

您的电子邮件地址不会被公开.


*