CentOS7.2编译安装LNMP

前端之家收集整理的这篇文章主要介绍了CentOS7.2编译安装LNMP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、前言

1、环境说明

基础环境Linux+Nginx+MysqL+PHP

linux:7.2

Nginx:1.10.2

MysqL:5.6.17

PHP:5.5.12

2、部署说明

PHP安装目录:/usr/local/PHP

PHP.ini配置文件路径:/usr/local/PHP/etc/PHP.ini

Nginx安装目录:/usr/local/Nginx

Nginx配置文件路径:/etc/Nginx/Nginx.conf

Nginx网站根目录:/usr/local/Nginx/html


二、准备工作

1、最小化安装Centos7.2

  1. #建立一个软件包目录存放
  2. mkdir-p/usr/local/src/
  3. #清理已经安装包
  4. rpm-ehttpd
  5. rpm-eMysqL
  6. rpm-ePHP
  7. yum-yremovehttpd
  8. yum-yremoveMysqL
  9. yum-yremovePHP
  10. #搜索apache包
  11. rpm-qahttp*
  12. #强制卸载apache包
  13. rpm-e--nodeps查询出来的文件
  14. #检查是否卸载干净
  15. rpm-qa|grephttp*

selinux可能会致使编译安装失败,我们先禁用它。

永久禁用,需要重启生效

  1. sed-i's/SELINUX=enforcing/SELINUX=disabled/g'/etc/selinux/config

临时禁用,不需要重启

  1. setenforce0

2、安装必备工具

  1. yum-yinstallmakegccgcc-c++gcc-g77flexbisonfilelibtoollibtool-libsautoconfkernel-devellibjpeglibjpeg-devellibpnglibpng-devellibpng10libpng10-develgdgd-develfreetypefreetype-devellibxml2libxml2-develzlibzlib-develglib2glib2-develbzip2bzip2-devellibeventlibevent-develncursesncurses-develcurlcurl-devele2fsprogse2fsprogs-develkrb5krb5-devellibidnlibidn-developensslopenssl-develgettextgettext-develncurses-develgmp-develpspell-develunziplibcaplsof


三、安装MysqL5.6.17

1、准备工作

按照标准需要给MysqL创建所属用户用户

  1. #创建群组
  2. groupaddMysqL
  3. #创建一个用户,不允许登陆和不创主目录
  4. useradd-s/sbin/nologin-gMysqL-MMysqL
  5. #检查刚创建的用户
  6. tail-1/etc/passwd

centos最小化安装后,会有MysqL的库因此先卸载!

  1. #检查安装与否
  2. rpm-qa|grepMysqL
  3. #强制卸载
  4. rpm-erpm-eMysqL-libs-5.1.73-3.el6_5.x86_64--nodeps

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

  1. wgethttp://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz
  2. tarzxvfcmake-2.8.12.2.tar.gz
  3. cdcmake-2.8.12.2
  4. ./configure
  5. make&&makeinstall

2、使用cmake来编译安装MysqL 5.6.17

  1. wgethttp://dev.MysqL.com/get/Downloads/MysqL-5.6/MysqL-5.6.17.tar.gz
  2. tarzxvfMysqL-5.6.17.tar.gz
  3. cdMysqL-5.6.17
  4. cmake\
  5. -DCMAKE_INSTALL_PREFIX=/usr/local/MysqL\
  6. -DMysqL_DATADIR=/usr/local/MysqL/data\
  7. -DSYSCONFDIR=/etc\
  8. -DWITH_MYISAM_STORAGE_ENGINE=1\
  9. -DWITH_INNOBASE_STORAGE_ENGINE=1\
  10. -DWITH_MEMORY_STORAGE_ENGINE=1\
  11. -DWITH_READLINE=1\
  12. -DMysqL_UNIX_ADDR=/var/lib/MysqL/MysqL.sock\
  13. -DMysqL_TCP_PORT=3306\
  14. -DENABLED_LOCAL_INFILE=1\
  15. -DWITH_PARTITION_STORAGE_ENGINE=1\
  16. -DEXTRA_CHARSETS=all\
  17. -DDEFAULT_CHARSET=utf8\
  18. -DDEFAULT_COLLATION=utf8_general_ci\
  19. -DMysqL_USER=MysqL\
  20. -DWITH_DEBUG=0\
  21. -DWITH_SSL=system
  22.  
  23. make&&makeinstall
  24.  
  25. #修改/usr/local/MysqL权限
  26. chmod+w/usr/local/MysqL
  27. chown-RMysqL:MysqL/usr/local/MysqL

关于my.cnf配置文件

在启动MysqL服务时,会按照一定次序搜索my.cnf,先在/etc目录下找,找不到则会搜索”$basedir/my.cnf”就是安装目录下/usr/local/MysqL/my.cnf,这是新版MysqL配置文件的默认位置!注意:在最小安装完成后,在/etc目录下会存在一个my.cnf,需要将此文件更名为其他的名字。如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的MysqL的正确配置,造成无法启动。由于我们已经卸载了最小安装完成后的MysqL库所以,就没必要操作了。

  1. cdsupport-files/
  2. #如果还有my.cnf请备份
  3. mv/etc/my.cnf/etc/my.cnf.bak
  4. cpmy-default.cnf/etc/my.cnf
  5. #执行初始化配置脚本,创建系统自带数据库和表,注意配置文件的路径
  6. /usr/local/MysqL/scripts/MysqL_install_db--defaults-file=/etc/my.cnf--basedir=/usr/local/MysqL--datadir=/usr/local/MysqL/data--user=MysqL
  7. #拷贝MysqL安装目录下support-files服务脚本到init.d目录
  8. cpsupport-files/MysqL.server/etc/init.d/MysqLd
  9. #赋予权限、设置开机自启并启动
  10. chmod+x/etc/init.d/MysqLd
  11. chkconfigMysqLdon
  12. serviceMysqLdstart
  13. #或者
  14. /etc/init.d/MysqLstart

MysqL5.6.x启动成功后,root默认没有密码,我们需要设置root密码。设置之前,我们需要先设置PATH,否则不能直接调用MysqL

  1. #修改/etc/profile文件
  2. vim/etc/profile
  3. #在文件末尾添加
  4. PATH=/usr/local/MysqL/bin:$PATH
  5. exportPATH
  6. #让配置立即生效
  7. source/etc/profile
  8. #登陆测试,默认是没有密码,直接回车就可进入
  9. MysqL-uroot-p
  10. #设置MysqL密码
  11. /usr/local/MysqL/bin/MysqLadmin-uroot-ppassword'你的密码'
  12. #登陆进命令行模式
  13. MysqL-uroot-p
  14. #查看用户
  15. >selectuser,hostfromMysqL.user;
  16. #删除不必要的用户
  17. >dropuser""@localhost;
  18. >dropuserroot@'::1';
  19. #赋予账号远程访问的权限
  20. >GRANTALLPRIVILEGESON*.*TO'root'@'127.0.0.1'IDENTIFIEDBY'你的密码';
  21. >GRANTALLPRIVILEGESON*.*TO'root'@'localhost'IDENTIFIEDBY'你的密码';
  22. #其它一些信息查询
  23. #检查MysqL版本
  24. MysqL-uroot-p"密码"-e"selectversion();"

MysqL安装完毕!


四、安装PHP5.5.12

1、安装依赖关系

  1. yuminstalllibmcryptlibmcrypt-develmcryptmhash

2、下载并编译安装PHP

  1. wgethttp://mirrors.sohu.com/PHP/PHP-5.5.12.tar.gz
  2. tarzxvfPHP-5.5.12.tar.gz
  3. cdPHP-5.5.12
  4. ./configure--prefix=/usr/local/PHP--with-config-file-path=/usr/local/PHP/etc--enable-fpm--with-fpm-user=www--with-fpm-group=www--with-MysqL=MysqLnd--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-magic-quotes--enable-safe-mode--enable-bcmath--enable-shmop--enable-sysvsem--enable-inline-optimization--with-curl--with-curlwrappers--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
  5. make&&makeinstall

下面的警告说明已自带不需启用或安装,可忽略:


继续:

  1. #修改fpm配置PHP-fpm.conf.default文件名称
  2. mv/usr/local/PHP/etc/PHP-fpm.conf.default/usr/local/PHP/etc/PHP-fpm.conf
  3. #复制PHP.ini配置文件
  4. cpPHP.ini-production/usr/local/PHP/etc/PHP.ini
  5. #复制PHP-fpm启动脚本到init.d
  6. cpsapi/fpm/init.d.PHP-fpm/etc/init.d/PHP-fpm
  7. #赋予执行权限
  8. chmod+x/etc/init.d/PHP-fpm
  9. #添加为启动项
  10. chkconfig--addPHP-fpm
  11. #设置开机启动
  12. chkconfigPHP-fpmon
  13. #按照标准,给PHP-fpm创建一个指定的用户和组
  14. #创建群组
  15. groupaddwww
  16. #创建一个用户,不允许登陆和不创主目录
  17. useradd-s/sbin/nologin-gwww-Mwww
  18. #立即启动PHP-fpm
  19. /etc/init.d/PHP-fpmstart

PHP安装完毕!


五、安装Nginx1.10.2

1、准备工作

  1. #添加用户与属组
  2. groupadd-rNginx
  3. useradd-s/sbin/nologin-gNginx-MNginx
  4. #创建目录
  5. mkdir-pv/var/tmp/Nginx/client/
  6. #解压安装包
  7. tarzxvfNginx-1.10.2.tar.gz

2、编译安装

  1. ./configure\
  2. --prefix=/usr/local/Nginx\
  3. --sbin-path=/usr/local/Nginx/bin/Nginx\
  4. --conf-path=/usr/local/Nginx/conf/Nginx.conf\
  5. --error-log-path=/var/log/Nginx/error.log\
  6. --http-log-path=/var/log/Nginx/access.log\
  7. --pid-path=/var/run/Nginx/Nginx.pid\
  8. --lock-path=/var/lock/Nginx.lock\
  9. --user=Nginx\
  10. --group=Nginx\
  11. --with-http_ssl_module\
  12. --with-http_flv_module\
  13. --with-http_stub_status_module\
  14. --with-http_gzip_static_module\
  15. --http-client-body-temp-path=/var/tmp/Nginx/client/\
  16. --http-proxy-temp-path=/var/tmp/Nginx/proxy/\
  17. --http-fastcgi-temp-path=/var/tmp/Nginx/fcgi/\
  18. --http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi\
  19. --http-scgi-temp-path=/var/tmp/Nginx/scgi\
  20. --with-pcre

敲黑板:注意sbin、conf、pid、local的path要和后续的控制脚本保持一致!

  1. make&&makeinstall

3、为Nginx提供SysV init脚本

  1. cat/etc/init.d/Nginx

#!/bin/sh

#

# Nginx - this script starts and stops the Nginx daemin

# chkconfig: - 85 15

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

# proxy and IMAP/POP3 proxy server

# processname: Nginx

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

# pidfile: /run/Nginx/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/sbin/Nginx"

prog=$(basename $Nginx)

Nginx_CONF_FILE="/usr/local/Nginx/conf/Nginx.conf "

lockfile=/var/lock/Nginx.lock

start() {

[ -x $Nginx ] || exit 5

[ -f $Nginx_CONF_FILE ] || exit 6

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 -eq 0 ] && rm -f $lockfile

restart() {

configtest || return $?

stop

start

reload() {

echo -n $"Reloading $prog: "

killproc $Nginx -HUP

RETVAL=$?

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

restart|configtest)

reload)

rh_status_q || exit 7

force-reload)

force_reload

status)

rh_status

condrestart|try-restart)

*)

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

exit 2

esac

类似的脚本网上很多,注意config、pid、sbin的路径保持和编译一致即可。

  1. chmod+x/etc/init.d/Nginx
  2. chkconfig--addNginx
  3. chkconfigNginxon
  4. #然后就可以启动服务了
  5. systemctlrestartNginx.service
  6. #或
  7. /etc/init.d/Nginxstart

Nginx安装完毕 ,下面开始整合NginxPHP


六、整合NginxPHP

1、编辑Nginx.conf,启用如下选项:

  1. cd/usr/local/Nginx/conf/
  2. catNginx.conf

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;

}

2、编辑/etc/Nginx/fastcgi_params,将其内容更改为如下内容

  1. #mvfastcgi_paramsfastcgi_params.bak
  2. #catfastcgi_params

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE Nginx;

fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

在所支持的主页面格式中添加PHP格式的主页,类似如下:

location/{

roothtml;

indexindex.PHPindex.htmlindex.htm;

}

  1. #重新载入配置
  2. #/etc/init.d/Nginxreload

3、在/usr/local/Nginx/html目录下新建index.PHP测试页面

  1. cat>/usr/local/nignx/htmlindex.PHP<<EOF

<?php

PHPinfo();

?>

可通过浏览器 访问此测试页面,验证Nginx连接PHP配置是否成功。页面应该如下图

至此CenOS72下LNMP编译安装完毕!

猜你在找的CentOS相关文章