Centos7.2源码编译安装LNMP

2个月前 Linux 157

nginx版本1.14.0

mysql版本5.7.20

php版本7.2.19

下载网络yum源

这里安装的是阿里的网络源,epel扩展源,也可以安装阿里的,但是阿里的epel源有些包不全,所以下面就直接用yum安装网络epel源。

wget http://mirrors.aliyun.com/repo/Centos-7.repo -P /etc/yum.repos.d/

yum -y install epel-release

yum clean all;
yum makecache

nginx

1、安装依赖包:

yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*

2、创建nginx运行用户

useradd -M -s /sbin/nologin nginx

3、下载pcre包并解压

wget https://jaist.dl.sourceforge.net/project/pcre/pcre/8.42/pcre-8.42.zip
unzip pcre-8.42.zip -d /usr/local/src/

4、下载nginx源码包并解压

wget http://nginx.org/download/nginx-1.14.0.tar.gz

tar zxf nginx-1.14.0.tar.gz -C /usr/local/src/

cd /usr/local/src/nginx-1.14.0/

5、编译安装

./configure --prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_ssl_module \
--user=nginx \
--group=nginx \
--with-pcre=/usr/local/src/pcre-8.42 
make
make install

6、修改配置文件

vim /usr/local/nginx/conf/nginx.conf
 user  nginx nginx;      #修改用户和组
#以下代码在配置文件中去掉开头的# (取消注释)
 location ~ \.php$ {
     root           html;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;    #修改路径
     include        fastcgi_params;
 }

测试配置文件修改是否有错误

/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

7、编辑profile文件,添加环境变量

vim /etc/profile
#根据自己的安装目录进行调整
export PATH=$PATH:/usr/local/nginx/sbin 

保存退出

#加载变量立即生效
source /etc/profile
vim /etc/init.d/nginx         #配置启动脚本文件

添加以下代码(PATH,DAEMON,CONFIGFILE,PIDFILE根据自己目录进行修改)

#! /bin/bash
#chkconfig: 2345 80 90
#description:nginx run

# nginx启动脚本
# @author    Devil
# @version    0.0.1
# @date        2017-05-29

PATH=/usr/local/nginx/
DESC="nginx1"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start()
{
    $DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop()
{
    $DAEMON -s stop || echo -n "nginx not running"
}
do_reload()
{
    $DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
    start)
        echo -n "Starting $DESC: $NAME"
        do_start
        echo "."
    ;;
    stop)
        echo -n "Stopping $DESC: $NAME"
        do_stop
        echo "."
    ;;
    reload|graceful)
        echo -n "Reloading $DESC configuration..."
        do_reload
        echo "."
    ;;
    restart)
        echo -n "Restarting $DESC: $NAME"
        do_stop
        do_start
        echo "."
    ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
        exit 3
    ;;
esac
exit 0

保存 退出

添加执行权限

chmod +x /etc/init.d/nginx

设置开机自启动

chkconfig --add nginx
chkconfig nginx on

php

1、安装依赖包

yum -y install php-mcrypt libmcrypt libmcrypt-devel  autoconf  freetype gd libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel re2c net-snmp-devel libjpeg-devel php-ldap openldap-devel openldap-servers openldap-clients freetype-devel gmp-devel

2、下载PHP源码包

wget https://www.php.net/distributions/php-7.2.19.tar.bz2

3、解压压缩包

tar -xjvf php-7.2.19.tar.bz2  -C /usr/local/src/
cd /usr/local/src/php-7.2.19/

4、编译安装

–with-ldap –with-ldap-sasl (21,22行)如果不添加这两项,要是安装zabbix监控时候,会有提示还得需要再次编译,如果不安装zabbix,也可以忽略。

如果不执行这步

./configure的时候会出现报错error:configure: error: Cannot find ldap libraries in /usr/lib.

解决方案:

cp -frp /usr/lib64/libldap* /usr/lib/

编译

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

如果不执行这步. make会报错

/usr/bin/ld: ext/ldap/.libs/ldap.o: undefined reference to symbol ‘ber_strdup’
/usr/lib64/liblber-2.4.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

解决方案


vim Makefile      #在以EXTRA_LIBS开头的一行结尾添加‘-llber’

例如:
EXTRA_LIBS = -lcrypt -lz -lresolv -lcrypt -lrt -lldap -lgmp -lpng -lz -ljpeg -lz -lrt -lm -ldl -lnsl -lpthread -lxml2 -lz -lm -ldl -lssl -lcrypto -lcurl -lxml2 -lz -lm -ldl -lssl -lcrypto -lfreetype -lxml2 -lz -lm -ldl -lnetsnmp -lssl -lssl -lcrypto -lm -lxml2 -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lssl -lcrypto -lcrypt -llber
make
make install

5、配置php配置文件

移动php配置文件的位置,并修改名称

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

复制php.ini文件

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

6、启动脚本

复制php启动脚本到/etc/init.d/

cp /usr/local/src/php-7.2.6/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

添加执行权限,添加到启动项并设置卡机启动

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

chkconfig --add php-fpm

chkconfig php-fpm on

启动php-fpm

/etc/init.d/php-fpm start

7、修改nginx 配置文件支持php页面

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

增加 inde.php

location / {
             root   html;
             index  index.php index.html index.htm;   #再该行增加index.php
         }

修改完配置文件重启nginx服务

service nginx restart

8.设置环境变量

vim ~/.bash_profile 
#加上
alias php=/usr/local/php/bin/php
重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录
source ~/.bash_profile 
测试  如果有数据就证明成功了
php -v  

9、编写php测试页

创建并编辑index.php

vim /usr/local/nginx/html/index.php

添加以下代码

<?php
        phpinfo();
?>

10、访问测试

127.0.0.1

mysql

1.卸载系统自带的mariadb*

yum -y remove mariadb* boost-*

2、安装依赖包

yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel

3、下载源码包

wget https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.20.tar.gz

4、解压源码包

tar zxf mysql-boost-5.7.20.tar.gz -C /usr/local/src/

5、配置编译并安装

cd /usr/local/src/mysql-5.7.20/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/src/mysql-5.7.20/boost/boost_1_59_0 \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_USER=mysql

编译安装

make && make install

6、创建数据库用户

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

7、创建所需目录

#数据存储目录
mkdir -p /usr/local/mysql/data 
#日志目录
mkdir -p /usr/local/mysql/log 
#更改属主数组为MySQL
chown -R mysql.mysql /usr/local/mysql/        

8、配置my.cnf文件

vim /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
character-set-server=utf8
pid-file=/usr/local/mysql/mysqld.pid     
log-error=/usr/local/mysql/log/mysqld.log

9、配置mysql启动脚本

#复制启动脚本到/etc/init.d

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

修改数据存储目录

#编辑启动脚本 找到basedir,datadir 更改为数据目录
vim /etc/init.d/mysqld         
↓↓↓
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

10、配置环境变量

#编辑文件
vim /etc/profile

添加以下代码,路径根据自己安装目录进行更改

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

加载变量立即生效

source /etc/profile

11、设置开机自启动

chkconfig --add mysqld
chkconfig mysqld on

12、安全初始化数据库

这样初始化之后,数据库是没有密码的。如果要想初始化之后分配临时密码,可以将–initialize-insecure 红色部分去掉,初始化之后,可以分配到一个临时密码。

/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

启动数据库

/etc/init.d/mysqld start

进入数据库更改数据库密码

mysql -u root      
 
mysql> alter user 'root'@'localhost' identified by 'qwe123';
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

多站点设置

mkdir /usr/local/nginx/conf/vhost
vim /usr/local/nginx/conf/nginx.conf

在nginx.conf下的http块下使用include vhost/*.conf;即可

include vhost/*.conf;
cd /usr/local/nginx/conf/vhost
vim www.demo.com.conf
#tp5 适用 其他的没有测试过
server {
    listen       80;
    server_name  loca.tp5demo.com;

    root  /www/tp5demo/public;
    index index.php index.html index.htm;

    location / {
     if (!-e $request_filename) {
           rewrite  ^(.*)$  /index.php?s=/$1  last;
           break;
     }        
    }
    error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html  
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /www/tp5demo/public;
    }
 
    location ~ \.php$ {
        root           /www/tp5demo/public;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

参考url:https://www.bfshu.com/jcaz/123



本文为脚本叔叔原创文章,转载无需和我联系,但请注明来自脚本叔叔http://jianghaihao.com

目前有1条留言
  1. fulongli:

    学习了

    2019-07-04 09:34:45 [回复]

发表评论

全站搜索

    Table of Contents