码迷,mamicode.com
首页 > 其他好文 > 详细

LNMP环境搭建

时间:2017-07-13 19:05:55      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:lnmp

centos6.7 编译安装LNMP

Date:2017-07-11

Author:Allen_Jol

环境说明:

CentOS release 6.7 (Final)

2.6.32-573.el6.x86_64

所需软件包:

nginx-1.12.0.tar.gz

mysql- 5.6.17.tar.gz      //编译mysql需要boost

php-7.1.7.tar.gz

软件包所在目录:/usr/local/src

提供一个批量解压tar.gz包的方法:ls *.tar.gz |xargs -n1 tar-zxvf

清理已安装的包:

rpm -e --nodeps mysql php httpd

yum -y remove mysql php httpd              //尽量别用,yum remove会把依赖包都卸载

关闭selinux和iptables防火墙

sed -ri‘/^SELINUX=/c\SELINUX=disabled‘ /etc/selinux/config

或者

sed -i‘/SELINUX=enforcing/SELINUX=disabled‘ /etc/selinux/config

setenforce  0              //临时生效,不需要重启系统

关闭防火墙:

service iptables stop

chkconfig iptables off

或者防火墙开放80端口

iptables -I INPUT  -p tcp --dport 80 -j ACCEPT

service iptables save

安装epel

yum -y install epel-release

安装依赖包和相关工具

yum -y install "DevelopmentTools"

yum -y install make gcc gcc-c++gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeglibjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetypefreetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devele2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel opensslopenssl-devel gettext gettext-devel ncurses-devel gmp-devel pspell-devel unziplibcap lsof ncurses-devel ncurses pcre pcre-devel

一、安装nginx-1.12.0

------------前面yum安装pcre zlibopenssl这里就不需要编译了----------

1、安装pcre

cd/usr/local/src/

wget https://ftp.pcre.org/pub/pcre/pcre-8.39.tar.gz

tar -zxvf pcre-8.39.tar.gz

cd pcre-8.39/

./configure

make -j $(grep processor /proc/cpuinfo | wc-l) && make install

// grep processor /proc/cpuinfo | wc -l 统计cpu线程数,同时用cpu多线程make速度更快

如果不想编译pcre,也可以yum安装 pcre pcre-devel

2、安装zlib

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

tar -zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11/

./configure

make -j $(grep processor /proc/cpuinfo | wc-l) && make install

3、安装openssl

wget https://www.openssl.org/source/openssl-1.0.2k.tar.gz

tar -zxvf openssl-1.0.2k.tar.gz

cd openssl-1.0.2k/

./config

make -j $(grep processor /proc/cpuinfo | wc-l) && make install

 

 

-------------------正式编译nginx-1.12.0--------------------------

tar -zxvf nginx-1.12.0.tar.gz

cd nginx-1.12.0/

./configure --user=www --group=www--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module--with-http_gzip_static_module --with-stream --with-stream_ssl_module   --with-http_gunzip_module --with-http_mp4_module --with-http_flv_module --with-http_dav_module --with-http_sub_module --with-pcre

 

注意:因为前面我们的zlibpcre前面使用yum安装所以不需要指定目录,即使这两个是编译安装也只需要./configure就行,后面不用加上--prefix=/usr/local/zlib这样。如果zlibopenssl也是编译,编译nginx就要指定目录--with-zlib=/usr/local/src/zlib-1.2.11  --with-pcre=/usr/local/src/pcre-8.39。至于openssl,如果编译的,那么编译nginx的时候就加上--with-openssl=/usr/local/src/ openssl-1.0.2k。如果yum只需要加上--with-http_ssl_module

 

make -j $(grep processor /proc/cpuinfo | wc-l) && make install

 

注意:听说opensslzlib包最好用yum安装。如果编译安装opensslzlib容易有相关库的问题经常会出错。如果这里出错了,我们就用yum安装它们

 

添加用户nginx

useradd -M -s /sbin/nologin nginx

修改nginx配置文件(修改了这些,以后zabbix部署就方便了,很多东西再部署zabbix需要更改

 

vim /usr/local/nginx/conf/nginx.conf

#user nobody 改成user nginx;

或者在nobody下面添加:

user nginx nginx;

 

脚本里面可以用以下方法添加nginx

sed -i ‘/worker_processes 1;/i\user nginx nginx;‘ /usr/local/nginx/conf/nginx.conf

说明:在worker_processes  1;上面插入user nginxnginx;

或者

sed -i ‘/#user  nobody;/a\\usernginx nginx;‘ /usr/local/nginx/conf/nginx.conf

说明:在#user nobody;下面插入user nginx nginx;

技术分享

 

检查配置文件准确性

/usr/local/nginx/sbin/nginx -t

 

启动nginx

/usr/local/nginx/sbin/nginx

 

一般来说在nginx的配置文件修改后进行如下操作,

/usr/local/nginx/sbin/nginx -t检测一下配置文件是否正确,如果正确的话

再使用/usr/local/nginx/sbin/nginx -s reload 使nginx平滑启动

 

查看nginx进程

ps -ef | grep nginx

 

查看nginx端口

ss -antlup | grep 80

也可以在windows上用浏览器访问一下:

http://ip

 

Linux上访问

curl -I localhost

出现

HTTP/1.1 200 OK   说明成功

 

======================nginx编译安装并且启动成功================

二、安装mysql-5.6.17

 

1、首先创建mysql用户和组

创建用户组

groupadd mysql

创建一个用户msyql,不允许登陆和不创建主目录

useradd -s /sbin/nologin -g mysql -M mysql

mkdir -p /usr/local/mysql/data

检查创建的用户

cat /etc/passwd | grep mysql

或者

tail -1 /etc/passwd

 

注:MySQL5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具。 因此,我们首先要在系统中源码编译安装cmake工具。

 

安装cmake工具:

wget http://wwwNaNake.org/files/v2.8/cmake-2.8.12.2.tar.gz

tar -zxvf cmake-2.8.12.2.tar.gz

cd cmake-2.8.12.2/

./configure

make -j $(grep process /proc/cpuinfo | wc-l) && make install

或者

gmake && gmake install

 

使用cmake工具来编译安装mysql-5.6.17,编译时间有点久,耐心等待一下

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.18.tar.gz

yum groupinstall "DevelopmentTools"

tar -zxvf mysql- 5.6.17.tar.gz

cd /usr/local/src/ mysql- 5.6.17/

cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data-DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1-DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock-DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1-DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1-DWITH_BOOST=/usr/local/src/boost_1_59_0

 

注意:mysql5.7.5开始boost库是必须的. 编译需要boost包,-DDOWNLOAD_BOOST=1 表示下载这个包,DWITH_BOOST=/usr/local/src/boost_1_59_0表示指定它的目录

 

编译mysql

make -j $(grep processor /proc/cpuinfo | wc-l) && make install

 

注意:如果没有执行make && make install,那么在/usr/local/mysql下面是没有scripts目录的

 

3.修改/usr/local/mysql权限

chmod +w /usr/local/mysql

chown -R mysql:mysql /usr/local/mysql

 

4、执行初始化配置脚本,创建系统自带的数据库和表,注意配置文件的路径:

命令如下:

------------------初始化mysql----------------------------

注意:mysql_install_db 这样初始化的mysql是不会生成初始密码的

mysqld 那样的方法会生成初始密码,mysqld方法应该是在bin目录下的

 

/usr/local/mysql/scripts/mysql_install_db--user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

 

5、拷贝mysql安装目录下support-files服务脚本到init.d目录

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

 

注意:如果这里是/etc/init.d/mysql 那么之后启动就是service mysqlstart 如果是/etc/init.d/mysqld   以后启动就是service mysqldstart 或者直接是/etc/init.d/mysqld

 

6、给刚才复制过来的脚本赋予权限:

chmod +x /etc/init.d/mysqld

 

7、复制文件

\cp/usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

 

service mysqld  start               //启动mysql或者用/etc/init.d/mysql start

chkconfig mysqld  on                    //设置开机启动

ps -ef   | grep mysqld         //查看mysql进程是否启动                                  

8、写入环境变量:

echo "exportPATH=$PATH:/usr/local/mysql/bin" >> /etc/profile

source /etc/profile

 

 

9、利用mysqladmin更改密码的方法:

/usr/local/mysql/bin/mysqladmin -urootpassword ‘123456‘  //当然,提前写入了环境变量的话就不用在前面加上/usr/local/mysql/bin的绝对路径了

 

10、如果没有用mysqladmin的方法更改密码而是登陆以后更改数据库密码:

mysql> set password=password(‘123456‘);

Query OK, 0 rows affected (0.00 sec)

 

mysql> flush privileges;                     //刷新权限

 

mysql> quit;          //或者exit退出这里退出时候分号加不加都可以

 

++++++++++++mysql5.6.17编译完成++++++++++++++++++++

 

 

三、安装php-7.1.7

首先解决php依赖的包安装

1.安装libiconv

cd libiconv-1.14/

./configure --prefix=/usr/local/libiconv

make && make install

 

可能出错:

In file includedfrom progname.c:26:0:

./stdio.h:1010:1:error: ‘gets’ undeclared here (not in a function)

 _GL_WARN_ON_USE (gets, "gets is asecurity hole - use fgets instead");

 ^

make[2]: ***[progname.o] Error 1

make[2]: Leavingdirectory `/usr/local/src/zabbix-2.4.7/libiconv-1.14/srclib‘

make[1]: *** [all]Error 2

make[1]: Leavingdirectory `/usr/local/src/zabbix-2.4.7/libiconv-1.14/srclib‘

make: *** [all]Error 2

解决:

先找到stdio.in.h  一般还是在这个目录       vim srclib/stdio.in.h

vi$HOME/libiconv-1.14/srclib/stdio.in.h

然后跳到698行,把

 

_GL_WARN_ON_USE(gets, "gets is a security hole - use fgets instead");

替换成

_GL_WARN_ON_USE(gets, "gets is a security hole - use fgets instead");

然后make&& make install

 

 

 

2、安装libmcrypt

cd libmcrypt-2.5.8

./configure

make && make install

 

3.安装 Mhash

cd mhash-0.9.9.9

./configure

make && make install

 

4.安装Mcrypt

cd mcrypt-2.6.8

注意一下这步运行下,不然下面可能报错

exportLD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

./configure

make && make install

 

编译mcrypt可能会报错configure: error: ***libmcrypt was not found

解决:

vi /etc/ld.so.conf

最后一行添加

/usr/local/lib/

保存并退出::wq!

执行载入命令:

ldconfig

或者

echo "/usr/local/lib" >>/etc/ld.so.conf

ldconfig

 

编译mcrypt可能会报错

/bin/rm: cannot remove `libtoolT‘: No suchfile or directory

 

解决:

修改 configure 文件,把RM=‘$RM‘改为RM=‘$RM -f‘ 这里的$RM后面一定有一个空格。 如果后面没有空格,直接连接减号,就依然会报错。

 

-----------------------------开始编译PHP-----------------------------------

 

cd php-7.1.7/

./configure --prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www--with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd--with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib--with-libxml-dir=/usr --enable-xml --disable-rpath  --enable-bcmath --enable-shmop --enable-sysvsem--enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring--with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl--with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip--enable-soap --without-pear --with-gettext --disable-fileinfo--enable-maintainer-zts

 

make -j $(grep processor /proc/cpuinfo | wc-l) && make install

 

2.修改fpm配置php-fpm.conf.default文件名称

mv /usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf

3.复制php.ini配置文件

cp php.ini-production/usr/local/php/etc/php.ini

4.复制php-fpm启动脚本到init.d

cp sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm

5.赋予执行权限

chmod +x /etc/init.d/php-fpm

6.添加为启动项

chkconfig --add php-fpm

7.设置开机启动

chkconfig php-fpm on

8.按照标准,给php-fpm创建一个指定的用户和组

创建群组:

groupadd www

创建一个用户,不允许登陆和不创主目录:

useradd -s /sbin/nologin -g www -M www

复制一个进程池文件否则启动php会报错

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

9.立即启动php-fpm

service php-fpm start

#或者

/etc/init.d/php-fpm start

 

Starting php-fpm  done   这样说明启动php成功

 

检查端口

ss –antlup    看到9000   80 3306  说明环境都安装成了

技术分享

 

 

 

 

 

-----------------------整合phpnginx-----------------------------------

 

 

 

利用sed更改nginx.confserver_name为指定的ip

sed -i ‘38 s/server_name  localhost;/server_name  192.168.76.119;/g‘/usr/local/nginx/conf/nginx.conf

技术分享

sed命令加入index.php

sed -i ‘46 s/index.html/index.php index.html/‘/usr/local/nginx/conf/nginx.conf

技术分享

后面index.php是为了后面支持php后期php部分还要在index.php写入测试内容:

echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php

 

取消指定/usr/local/nginx/conf/nginx.conf里第6672行开头的注释:

sed -i "66,72 s/\s*#\(.*\).*/\1/g;"/usr/local/nginx/conf/nginx.conf

技术分享

/usr/local/nginx/conf/nginx.conf中第70/scripts$fastcgi_script_name;替换成$document_root$fastcgi_script_name;

sed -i ‘70s@\/scripts$fastcgi_script_name;@$document_root$fastcgi_script_name;@‘/usr/local/nginx/conf/nginx.conf

技术分享

这个$document_root也可以更改成/usr/local/nginx/html

 

 

 

修改完配置之后

测试一下是否有错:

/usr/local/nginx/sbin/nginx –t

nginx: the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file/usr/local/nginx/conf/nginx.conf test is successful

说明启动成功

 

平滑重启nginx

/usr/local/nginx/sbin/nginx -s reload

 

如果开了防火墙,一定要记得把端口开放  80 9000  3306

 

----------------最后测试 PHP  MYSQL  NGINX----------------------------

1.进入nginx的服务器目录,这一步是在nginx.conf里的root中配置的,可以自行修改。

2.新建一个php文件:vim index.php 内容如下:

<?phpphpinfo(); ?>

或者直接echo进去:

echo "<?php phpinfo(); ?>">> /usr/local/nginx/html/index.php

 

然后运行:

http://127.0.0.1/index.php(这里把IP换成你自己的服务器的IP)

技术分享

这里直接输入ip按照道理来说应该是房文档nginx,可能配置文件里主页改成了index.php的原因直接访问到了php

访问nginx192.168.79.134/index.html

技术分享

测试成功以后,一定要记得删除/usr/local/nginx/html/index.php里面的内容,不然被黑客看到这个是非常危险的。因为我们的php编译方式等等都在上面

 

 

支持nginx开机启动的脚本:

 

#!/bin/sh

 

#

 

# nginx - this script starts and stops thenginx 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 "=" -f2`

 

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

 

赋予文件执行权

chmod +x /etc/init.d/nginx

 

设置nginx开机自启

chkconfig nginx on

 

重启nginx

service nginx restart


本文出自 “12252646” 博客,请务必保留此出处http://12262646.blog.51cto.com/12252646/1947265

LNMP环境搭建

标签:lnmp

原文地址:http://12262646.blog.51cto.com/12252646/1947265

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!