Linux学习--编译安装LNMP

Posted 丢爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习--编译安装LNMP相关的知识,希望对你有一定的参考价值。

  1. 安装nginx
  2. 安装pcre-devel
  3. 如开启了openssl-devel,则需安装openssl-devel包【yum install openssl-devel -y】

编译安装nginx常用配置项介绍

  1. 属性
    –prefix= :Nginx安装路径。不指定,则默认为 /usr/local/nginx。
    –sbin-path= :Nginx可执行文件安装路径。只能安装时指定,不指定,默认为/sbin/nginx。
    –conf-path= :没有给定-c选项下默认的nginx.conf的路径。不指定,默认为/conf/nginx.conf。
    –pid-path= :nginx.conf中不指定pid指令的情况下,默认为 /logs/nginx.pid。
    –lock-path= :nginx.lock文件的路径,默认为/logs/nginx.lock
    –error-log-path= :nginx.conf中不指定error_log的情况下,默认错误日志的路径:/logs/error.log。
    –http-log-path= - 在nginx.conf中没有指定access_log指令的情况下,默认访问日志路径: /logs/access.log。
    –user= - 在nginx.conf中没有指定user指令的情况下,默认nginx使用用户为 nobody。
    –group= - 在nginx.conf中没有指定grop指令的情况下,默认nginx使用nobody。
  2. 模块
    –with-http_stub_status_module:启动server_status 页面
    –with-http_ssl_module:启用ssl功能,让nginx支持https,此模块需要安装openssl-devel包
    –without-http_gzip_module:禁用zip模块
#下载nginx的二进制包
[root@lotus ~]# wget http://nginx.org/download/nginx-1.20.0.tar.gz
#解压gz包
[root@lotus ~]# tar xf nginx-1.20.0.tar.gz 
[root@lotus ~]# cd nginx-1.20.0
#安装pcre-devel和openssl-devel
[root@lotus nginx-1.20.0]# yum install -y pcre-devel
[root@lotus nginx-1.20.0]# yum install -y openssl-devel
#编译安装nginx
	#【--prefix】表示nginx编译安装的路径,【--conf-path】表示nginx的配置文件nginx.conf的存放位置,【--with-http_ssl_module】表示支持https功能
[root@lotus nginx-1.20.0]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module
[root@lotus nginx-1.20.0]# make && make install
[root@lotus nginx-1.20.0]# ls /usr/local/nginx
html  logs  sbin
#启动nginx服务
[root@lotus nginx-1.20.0]# cd /usr/local/nginx/sbin
[root@lotus sbin]# ls
nginx
[root@lotus sbin]# ./nginx
#nginx服务启动后,显示80端口已开启
[root@lotus sbin]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:50783               0.0.0.0:*                   LISTEN      1270/rpc.statd      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1248/rpcbind        
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4223/nginx          
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1441/sshd           
tcp        0      0 :::111                      :::*                        LISTEN      1248/rpcbind        
tcp        0      0 :::57749                    :::*                        LISTEN      1270/rpc.statd      
tcp        0      0 :::22                       :::*                        LISTEN      1441/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1520/master         
#编写nginx服务启动脚本
[root@lotus ~]# vim /etc/init.d/nginx

#!/bin/bash
#
# nginx startup script for nginx server
#
# chkconfig: - 85 15
# processname:nginx
# config:/etc/nginx/nginx.conf
# pidfile:/var/run/httpd.pid

# 载入函数库
. /etc/rc.d/init.d/functions
# 载入网络配置
. /etc/sysconfig/network

nginxfile=/usr/local/nginx/sbin/nginx
prog=$(basename $nginxfile)
pidfile=/usr/local/nginx/logs/nginx.pid
conffile=/etc/nginx/nginx.conf
lockfile=/var/lock/subsys/nginx

RETVAL=0

start() {
  if [ -f $pidfile ];then
    echo "nginx is running"
  else
    daemon $nginxfile -c $conffile
    RETVAL=$?
    echo -n $"Starting $prog:"
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
  fi
  return $RETVAL
}
stop() {
  killproc $prog -QUIT
  RETVAL=$?
  echo -n $"Stopping $prog"
  echo
  [ $RETVAL -eq 0 ] && rm -rf $lockfile
  return $RETVAL
}

restart() {
  configtest || return $?
  stop
  start
}

reload() {
  configtest || return $?
  killproc $nginxfile -HUP
  RETVAL=$?
  echo -n $"Reloading $prog:"
}

configtest() {
  $nginxfile -t -c $conffile
}
case "$1" in
  start)
    start
    RETVAL=$?
    ;;
  stop)
    stop
    RETVAL=$?
    ;;
  restart)
    restart
    RETVAL=$?
    ;;
  status)
    status $prog
    RETVAL=$?
    ;;
  reload)
    reload
    RETVAL=$?
    ;;
  *)
    echo "USAGE:$0 {start|stop|reload|restart|status}"
    exit 1
esac
exit $RETVAL


[root@lotus ~]# chmod +x /etc/init.d/nginx

#添加至开机启动
[root@lotus ~]# chkconfig nginx on
[root@lotus ~]# chkconfig --list | grep nginx
nginx          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
  1. 安装mysql
#下载二进制包
[root@lotus download]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz
#解压,MySQL二进制包是解压后就可以使用
[root@lotus download]# tar xf  mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz -C /usr/local
#创建软链接
[root@lotus local]# pwd
/usr/local
[root@lotus local]# ln -sv mysql-5.6.10-linux-glibc2.5-x86_64 mysql
#添加mysql用户,mysql组
[root@lotus local]# groupadd -r -g 306 mysql
[root@lotus local]# useradd -g 306 -r -u 306 mysql
#修改mysql文件目录的属主,属组
[root@lotus mysql]# pwd
/usr/local/mysql
[root@lotus mysql]# chown -R mysql.mysql .
#初使化mysql,建议将数据库文件放到一个独立的分区,独立的逻辑卷上面,本文简单放入至单独的目录【/data】
[root@lotus mysql]# mkdir /data
[root@lotus mysql]# chown mysql.mysql /data
#减小其它用户权限为不能访问
[root@lotus mysql]# chmod o-x /data
#初使化mysql服务器
[root@lotus mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/
#修改mysql目录下的文件的属主为root
[root@lotus mysql]# chown -R root .
#复制mysql启动脚本至/etc/init.d目录下
[root@lotus mysql]# cp support-files/mysql.server /etc/init.d/mysqld
#加入开机启动列表
[root@lotus mysql]# chkconfig --add mysqld
[root@lotus mysql]# chkconfig --list mysqld
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
#修改配置文件
[root@lotus mysql]# vim /etc/my.cnf

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /data
port = 3306
# server_id = .....
socket = /tmp/mysql.sock
thread_cache_size=8
query_cache_size=16M
sort_buffer_size=1M
read_buffer_size=1M
read_rnd_buffer_size=4M
myisam_sort_buffer_size=64M
max_allowed_packet=1M
key_buffer_size=256M
skip-external-locking
thread_concurrency=2
#启动MySQL
[root@lotus mysql]# service mysqld start
Starting MySQL. SUCCESS! 
#添加/etc/profile.d/mysql.sh文件,增加mysql的bin目录到环境变量
[root@lotus mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
#输出mysql的头文件和库文件
[root@lotus mysql]# vim /etc/ld.so.conf.d/mysql-x86_64.conf
/usr/local/mysql/lib
#重读库文件
[root@lotus mysql]# ldconfig -v
#输出头文件
[root@lotus mysql]# ln -sv /usr/local/mysql/include /usr/include/mysql

报错:

[root@lotus ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

解决方法:

修改mysql的配置文件
添加
[client]
socket=同mysqld配置的socket
  1. 安装php-FPM
    编译时报错1:
    make: *** [sapi/cli/php] Error 1
    解决方法:
#下载libiconv
[root@lotus php-5.4.45]# wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
#编译安装
$ ./configure --prefix=/usr/local
$ make
$ make install
#安装完成后,先进行make clean清理一下以前编译时的文件,重新执行make 即可

报错2
当make正常,但是使用make install出现错误:cp: cannot stat `sapi/cli/php.1’: No such file or directory
解决方法:

find . -name '*.1' > /tmp/php-1.lst.$$
tar -cf /tmp/php-1.tar.$$ -T /tmp/php-1.lst.$$
make clean
tar -xf /tmp/php-1.tar.$$
rm /tmp/php-1.tar.$$ /tmp/php-1.lst.$$
then 'make install' should work.
#安装libxml2-devel包
[root@lotus php-5.4.45]# yum install -y libxml2-devel
#安装bzip2-devel
[root@lotus ~]# yum install -y bzip2-devel
#编译安装libmcrypt
[root@lotus ~]# wget https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
[root@lotus ~]# tar xf libmcrypt-2.5.8.tar.gz 
[root@lotus ~]# cd libmcrypt-2.5.8
[root@lotus libmcrypt-2.5.8]# ./configure
[root@lotus libmcrypt-2.5.8]# make && make install
#下载php
[root@lotus ~]# wget https://www.php.net/distributions/php-5.4.45.tar.gz
#解压
[root@lotus ~]# tar xf php-5.4.45.tar.gz 
[root@lotus ~]# cd php-5.4.45
#编译安装
[root@lotus php-5.4.45]#  ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm  --without-iconv
#复制配置文件
[root@lotus php-5.4.45]# cp php.ini-production /etc/php.ini
#修改nginx配置文件,让nginx服务器可以解析php文件
location / {
            root   html;
            index  index.php index.html index.htm;
        }

location ~ \\.php$ {
            root /usr/local/nginx/html;   #网站根目录
            fastcgi_pass   http://127.0.0.1:9000;     #通过本机的9000端口将PHP请求转发>给PHP-FPM进行处理
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;               #nginx调用fastcgi接口处理PHP请求
        }
#拷贝php-fpm.conf配置文件至/usr/local/php/etc/php-fpm.conf
[root@lotus bin]# cp /root/download/php-5.4.45/sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf

#修改原html文件为php文件
[root@lotus html]# pwd
/usr/local/nginx/html
[root@lotus html]# mv index.html index.php
[root@lotus html]# vim index.php
#在index.php网页文件中添加php代码
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
<h1>
<?php
  $conn=mysql_connect("localhost","root","123456");
  if ($conn) {
   echo "Connect Successfully";
  } else {
   echo "Connect Failure";
  }

?>
</h1>
<?php
  phpinfo();
?>
</body>
</html>

PHP和MySQL连接显示结果正常如下:
在这里插入图片描述

以上是关于Linux学习--编译安装LNMP的主要内容,如果未能解决你的问题,请参考以下文章

使用编译搭建LNMP环境

LNMP架构搭建Discuz论坛(实战!)

编译安装LNMP架构之Discuz论坛

linux的centos下怎么编译安装LNMP,然后运行phpcms

LNMP部署及应用理论及实操

Centos 7 下yum搭建lnmp环境(yum安装方式)