CentOS7.4 源码编译安装LNMP环境

基于CentOS7.4源码编译安装LNMP

系统环境:CentOS7.4
Nginx:1.14.2
MySQL:5.7.25
PHP:7.3.2

一、下载网络yum源

[root@ ~]# wget http://mirrors.aliyun.com/repo/Centos-7.repo -P /etc/yum.repos.d/
[root@ ~]# yum -y install epel-release
[root@ ~]# ls /etc/yum.repos.d/
Centos-7.repo  CentOS-Base.repo  epel.repo  epel.repo.rpmnew  epel-testing.repo
[root@ ~]# yum clean all
[root@ ~]# yum makecache

二、源码编译安装Nginx

1、安装依赖包

[root@ ~]# yum -y install gcc gcc-c++ automake autoconf pcre pcre-devel zlib zlib-devel openssl openssl-devel

2、创建Nginx运行用户

[root@ ~]# useradd -M -s /sbin/nologin nginx

3、源码编译安装Nginx

[root@ ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz

[root@ ~]# tar zxf nginx-1.14.2.tar.gz -C /usr/local/src/

[root@ ~]# cd /usr/local/src/nginx-1.14.2/

[root@ nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

[root@ nginx-1.14.2]# make

[root@ nginx-1.14.2]# make install

4、修改配置文件

[root@ nginx-1.14.2]# 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;
}

5、添加环境变量,优化Nginx服务

[root@ nginx-1.14.2]# /usr/local/nginx/sbin/nginx -t			#检查Nginx配置语法是否正确
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

[root@ nginx-1.14.2]# /usr/local/nginx/sbin/nginx			#开启Nginx

[root@ nginx-1.14.2]# vim /etc/profile								#添加环境变量
export PATH=$PATH:/usr/local/nginx/sbin

[root@ nginx-1.14.2]# source /etc/profile

6、两种方式配置Nginx开机自启动

6.1 添加为系统服务

官方脚本

[root@ nginx-1.14.2]# vim /etc/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:      /etc/sysconfig/nginx
# pidfile:     /usr/local/nginx/logs/nginx.pid					#改动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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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

[root@ nginx-1.14.2]# chmod +x /etc/init.d/nginx

[root@ nginx-1.14.2]# chkconfig --add nginx

[root@ nginx-1.14.2]# chkconfig nginx on

6.2、以守护进程方式启动

官方脚本

[root@ nginx-1.14.2]# vim /lib/systemd/system/nginxd.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

[root@ nginx-1.14.2]# systemctl daemon-reload

[root@ nginx-1.14.2]# systemctl restart nginxd.service

[root@ nginx-1.14.2]# systemctl enable nginxd.service

三、源码安装MySQL

1、卸载系统自带的mariadb

[root@ ~]# yum -y remove mariadb* boost-*

2、安装依赖包

[root@ ~]# yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel

3、源码编译安装MySQL

[root@ ~]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz

[root@  ~]# tar -zxvf mysql-boost-5.7.25.tar.gz -C /usr/local/src/

[root@ ~]# cd /usr/local/src/mysql-5.7.25/

[root@ mysql-5.7.25]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/src/mysql-5.7.25/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=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DMYSQL_USER=mysql

[root@ mysql-5.7.25]# make

[root@ mysql-5.7.25]# make install

4、创建数据库用户和数据目录

[root@ mysql-5.7.25]# useradd -M -s /sbin/nologin -r mysql

[root@ mysql-5.7.25]# mkdir -p /usr/local/mysql/data					#创建数据存储目录

[root@ mysql-5.7.25]# chown -R mysql.mysql /usr/local/mysql/					#修改文件操作权限

5、配置my.cnf文件

以下是简单的配置

[root@ mysql]# 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=utf8mb4

pid-file=/usr/local/mysql/mysqld.pid

log-error=/var/log/mysqld.log

6、添加环境变量

[root@ mysql-5.7.25]# vim /etc/profile					#配置环境变量
export PATH=$PATH:/usr/local/mysql/bin

[root@ mysql-5.7.25]# source /etc/profile					#环境变量立即生效

7、两种方式配置MySQL开启自启动

7.1 添加为系统服务

[root@ mysql-5.7.25]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@ mysql-5.7.25]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10576 Feb 21 23:06 /etc/init.d/mysqld

[root@ mysql-5.7.25]# vim /etc/init.d/mysqld					#修改路径
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

[root@ mysql-5.7.25]# chkconfig --add mysqld

[root@ mysql-5.7.25]# chkconfig mysqld on

7.2 以守护进程方式启动

[root@ mysql-5.7.25]# vim /lib/systemd/system/mysqld.service
[Unit]
Description=MySQL DBMS

[Service]
LimitNOFILE=10000

Type=simple

User=mysql

Group=mysql

PIDFile=/usr/local/mysql/mysqld.pid

ExecStart=/usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data

ExecStop=/bin/kill -9 $MAINPID

 [Install]
WantedBy=multi-user.target

[root@ mysql-5.7.25]# chmod +x /lib/systemd/system/mysqld.service

8、安全初始化数据库

[root@ mysql-5.7.25]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --explicit_defaults_for_timestamp=1

[root@ mysql-5.7.25]# /etc/init.d/mysqld start					#启动数据库
Starting MySQL. SUCCESS!

[root@ mysql-5.7.25]# mysql -uroot -p					#登录数据库

mysql> alter user 'root'@'localhost' identified by 'root';					#修改密码

mysql>  flush privileges;

如果报错提示:

[ERROR] Could not open file ‘/var/log/mysqld.log’ for error logging: Permission denied
[ERROR] Aborting

做以下操作:

[root@ mysql-5.7.25]# touch /var/log/mysqld.log
[root@ mysql-5.7.25]# chown mysql:mysql /var/log/mysqld.log
[root@ mysql-5.7.25]# chcon system_u:object_r:mysqld_log_t:s0 /var/log/mysqld.log

如果初始化完成没有显示随机生成的密码,做以下操作可以查看:

[root@ mysql-5.7.25]# grep password -n /var/log/mysqld.log
5:2019-02-22T05:53:27.821305Z 1 [Note] A temporary password is generated for root@localhost: (P>hoTVg<6Ms

四、源码编译安装PHP

1、安装依赖包

[root@ ~]# 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、源码编译安装

[root@ ~]# wget http://cn.php.net/distributions/php-7.3.2.tar.gz

[root@ ~]# tar -zxvf php-7.3.2.tar.gz -C /usr/local/src/

[root@ ~]# cd /usr/local/src/php-7.3.2/

[root@  php-7.3.2]# ./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

[root@ php-7.3.2]# make

[root@ php-7.3.2]# make install

如果报错提示:

configure: error: Cannot find ldap libraries in /usr/lib.

就执行一下命令,然后再重新配置

[root@ php-7.3.2]# cp -frp /usr/lib64/libldap* /usr/lib/

如果报错提示:

configure: error: Please reinstall the libzip distribution

重新安装libzip

[root@ php-7.3.2]# yum remove libzip

[root@ php-7.3.2]# wget -O ~/libzip-1.2.0.tar.gz https://libzip.org/download/libzip-1.2.0.tar.gz

[root@ php-7.3.2]# tar -zxvf ~/libzip-1.2.0.tar.gz -C /usr/local/src/

[root@ php-7.3.2]# cd /usr/local/src/libzip-1.2.0/

[root@ libzip-1.2.0]# ./configure

[root@ libzip-1.2.0]# make

[root@ libzip-1.2.0]# make install

如果报错提示:

configure: error: off_t undefined; check your library configuration

做以下操作

# 添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
# 更新配置
ldconfig -v

如果报错提示:

/usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory

做以下操作,这是安装新的libzip可能出现的问题

[root@ php-7.3.2]# cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

如果报错提示:

/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
make: *** [sapi/cli/php] Error 1

做以下操作

vim Makefile

添加图中红框部分内容
llber

3、配置php配置文件

[root@ php-7.3.2]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

[root@ php-7.3.2]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@ php-7.3.2]# cp /usr/local/src/php-7.3.2/php.ini-production /usr/local/php/etc/php.ini

4、配置php开机启动

[root@ php-7.3.2]# cp /usr/local/src/php-7.3.2/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

[root@ php-7.3.2]# chmod +x /etc/init.d/php-fpm

[root@ php-7.3.2]# chkconfig --add php-fpm

[root@ php-7.3.2]# chkconfig php-fpm on

5、验证php

[root@ php-7.3.2]# /etc/init.d/php-fpm start
Starting php-fpm  done

[root@ php-7.3.2]# vim /usr/local/nginx/conf/nginx.conf
location / {
		root   html;
		index  index.php index.html index.htm;					#添加index.php
}

[root@ php-7.3.2]# service nginx restart					#重启nginx

[root@ php-7.3.2]# vim /usr/local/nginx/html/index.php
<?php
        phpinfo();
?>

phpinfo

这只是简单的安装配置,实际配置还是视情况优化。


版权声明:本文为qq157962718原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。