首页 » Linux » 正文

LNMP环境搭建(二)

源码方式编译安装

操作系统:CentOS 7
Nginx:1.16.1
PHP:7.2.25
MySQL:5.7.28

1、安装基础依赖环境

###切换源到阿里云
[ -d /etc/yum.repos.d/bak ] || mkdir -p /etc/yum.repos.d/bak
mv -f /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
curl -o /etc/yum.repos.d/aliyun-base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
touch /etc/yum.repos.d/aliyun-epel.repo
cat >> /etc/yum.repos.d/aliyun-epel.repo << EOF
[EPEL]
name=aliyun_epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=0
EOF
yum clean all
yum makecache
yum install zip unzip bzip2 git vim wget net-tools gcc gcc-c++ make -y

2、安装NGINX

安装OPENSSL
cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
yum remove openssl -y
tar -xf openssl-1.1.1d.tar.gz && cd openssl-1.1.1d
./config --prefix=/usr/local/openssl
make && make install
安装PCRE
cd .. && wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar -xf pcre-8.43.tar.gz && cd pcre-8.43.tar.gz
./configure --prefix=/usr/local/pcre
make &&make install
安装ZLIB
cd .. && wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar -xf zlib-1.2.11.tar.gz && cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make &&make install
安装NGINX
mkdir -p /usr/local/nginx/cache/temp
useradd nginx -s /bin/false

cd .. && wget http://nginx.org/download/nginx-1.16.1.tar.gz

./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--lock-path=/usr/local/nginx/run/nginx.lock \
--pid-path=/usr/local/nginx/run/nginx.pid \
--error-log-path=/usr/local/nginx/log/nginx.error.log \
--http-log-path=/usr/local/nginx/log/nginx.access.log \
--modules-path=/usr/local/nginx/modules \
--http-client-body-temp-path=/usr/local/nginx/cache/temp/client_temp \
--http-proxy-temp-path=/usr/local/nginx/cache/temp/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/cache/temp/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/cache/temp/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/cache/temp/scgi_temp \
--user=nginx \
--group=nginx \
--with-compat \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-openssl=/usr/local/src/openssl-1.1.1d \
--with-http_gzip_static_module \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-pcre=/usr/local/src/pcre-8.43 \
--with-mail \
--with-mail_ssl_module \
--with-http_v2_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-file-aio \
--with-threads \
--with-http_addition_module  \
--with-http_slice_module \
--with-http_realip_module

make && make install
#如果是在CentOS 7中编译,需要使用service服务管理脚本管理nginx的启停不要指定--pid-path=/usr/local/nginx/run/nginx.pid选项

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
SYSTEMD管理方式管理NGINX服务
echo -e "[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
PIDFile=/usr/local/nginx/run/nginx.pid
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PrivateTmp=true
     
[Install]
WantedBy=multi-user.target
" >> /lib/systemd/system/nginx.service

systemctl start nginx
systemctl enable nginx

firewall-cmd --permanent --add-service=http
firewall-cmd --reload
init启动管理脚本
#!/bin/bash
# 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: /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/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 "=" -f 2`
                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

/etc/rc.d/init.d/nginx 

chmod 755 /etc/rc.d/init.d/nginx
service nginx start
chkconfig nginx on

2、安装MySQL 5.7

安装CMAKE
#所有版本https://cmake.org/files/
cd .. && wget https://github.com/Kitware/CMake/releases/download/v3.16.0/cmake-3.16.0.tar.gz
tar -xf cmake-3.16.0.tar.gz && cd cmake-3.16.0
./configure --prefix=/usr/local/cmake
make && make install

echo "export PATH=$PATH:/usr/local/cmake/bin" >> /etc/profile
source /etc/profile

安装MySQL

yum install m4 bison ncurses bison-devel ncurses-devel zlib-devel
 -y

useradd mysql -s /bin/false
mkdir -p /home/mysql/mysqldb
chmod -Rf 755 /home/mysql
chown -Rf mysql:mysql /home/mysql

mkdir -p /usr/local/mysql/run/mysql/3306
chown -Rf mysql:mysql /usr/local/mysql/run/mysql
chmod -Rf 755 /usr/local/mysql/run/mysql/3306

rpm -qa|grep mariadb
yum remove mariadb-libs -y
rm -f /etc/my.cnf

cd .. && wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.28.tar.gz
tar -xf mysql-boost-5.7.28.tar.gz && cd mysql-5.7.28

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/home/mysql/mysqldb \
-DSYSCONFDIR=/etc \
-DWITH_SYSTEMD=1 \
-DSYSTEMD_PID_DIR=/usr/local/mysql/run/mysql/3306 \
-DSYSTEMD_SERVICE_NAME=mysqld \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/run/mysql/3306/mysql.sock \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_SSL=/usr/local/openssl \
-DWITH_ZLIB=/usr/local/zlib \
-DWITH_CURL=system \
-DWITH_DEBUG=0 \
-DWITH_EXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DENABLE_DOWNLOADS=1 \
-DWITH_BOOST=./boost \
-DDOWNLOAD_BOOST=1 \
-DDOWNLOAD_BOOST_TIMEOUT=120

make && make install

echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
###空密码初始化
mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/mysqldb
SYSTEMD管理方式
cp -v /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/mysqld.service
systemctl start mysqld
systemctl enable mysqld
init启动脚本管理
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
service mysqld start
chkconfig mysqld on

更改MySQL密码

mysql -uroot

mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql>FLUSH PRIVILEGES;

3、安装PHP

安装autoconf

yum install perl-Data-Dumper -y 
cd .. && wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.xz
tar -xf autoconf-2.69.tar.xz && cd autoconf-2.69
./configure 
make && make install

安装automake

yum install perl-Thread-Queue -y
cd .. && wget http://ftp.gnu.org/gnu/automake/automake-1.16.1.tar.xz
tar -xf automake-1.16.1.tar.xz && cd automake-1.16.1
./configure 
make && make install

安装libtool

cd .. && wget http://mirrors.nju.edu.cn/gnu/libtool/libtool-2.4.6.tar.xz
tar -xf libtool-2.4.6.tar.xz && cd libtool-2.4.6
./configure
make && make install

安装nghttp2

cd .. && wget https://github.com/nghttp2/nghttp2/releases/download/v1.40.0/nghttp2-1.40.0.tar.bz2
tar -xf nghttp2-1.40.0.tar.bz2 && cd nghttp2-1.40.0
./configure
make && make install

echo '/usr/local/lib' > /etc/ld.so.conf.d/nghttp2.conf
ldconfig
ldconfig -p| grep libnghttp2

安装CURL

cd .. && wget  https://curl.haxx.se/download/curl-7.67.0.tar.bz2
tar -xf curl-7.67.0.tar.bz2 && cd curl-7.67.0
./buildconf
./configure \
--prefix=/usr/local/curl \
--with-nghttp2=/usr/local \
--with-zlib \
--with-ssl=/usr/local/openssl 
make && make install

cp -v  /usr/local/openssl/lib/libssl.so.1.1 /usr/local/curl/lib/
cp -v  /usr/local/openssl/lib/libcrypto.so.1.1 /usr/local/curl/lib/

安装LIBZIP

yum install bzip2-devel -y
cd .. && wget https://libzip.org/download/libzip-1.5.2.tar.xz
tar -xf  libzip-1.5.2.tar.xz && cd libzip-1.5.2
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/libzip
make && make install

ln -s /usr/local/libzip/lib64/libzip.so.5.0 /usr/lib64/libzip.so
echo "/usr/local/libzip/lib64" >> /etc/ld.so.conf.d/libzip.conf
ldconfig
ldconfig -p |grep libzip

安装libxml2

yum install python-devel -y
cd .. && wget http://distfiles.macports.org/libxml2/libxml2-2.9.9.tar.gz
tar -xf libxml2-2.9.9.tar.gz && cd libxml2-2.9.9
./configure --prefix=/usr/local/libxml2
make && make install

cp -v /usr/local/libxml2/lib/pkgconfig/* /usr/lib64/pkgconfig/
echo "/usr/local/libxml2/lib" >> /etc/ld.so.conf.d/libxml2.conf
ldconfig
ldconfig -p |grep libxml

安装yasm

#http://yasm.tortall.net/Download.html
cd .. && wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
####wget https://github.com/yasm/yasm/archive/v1.3.0.tar.gz
tar -xf yasm-1.3.0.tar.gz && cd yasm-1.3.0 
./configure
make && make install

安装libvpx

#http://www.linuxfromscratch.org/blfs/view/svn/multimedia/libvpx.html
cd .. && wget https://github.com/webmproject/libvpx/archive/v1.8.1/libvpx-1.8.1.tar.gz
tar -xf libvpx-1.8.1.tar.gz && cd libvpx-1.8.1
./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9
make && make install

安装libjpeg

cd .. && wget http://www.ijg.org/files/jpegsrc.v9c.tar.gz
tar -xf jpegsrc.v9c.tar.gz && cd jpeg-9c
./configure --prefix=/usr/local/jpeg --enable-shared  --enable-static
make && make install

安装libiconv

cd .. && wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
tar -xf libiconv-1.16.tar.gz && cd libiconv-1.16
./configure --prefix=/usr/local --enable-static --enable-shared --with-gnu-ld
make && make install
ldconfig
ldconfig -p |grep iconv

安装libpng

#安装libpng有个坑zlib-devel有版本匹配的关系,具体关系未知,centos7的zlib-devel是1.2.7版本,和这里安装的
#libpng 1.6.37不匹配,与下载的源码1.2.11版本的zlib匹配,执行./configure安装即可,即安装2次

cd .. && wget https://nchc.dl.sourceforge.net/project/libpng/libpng16/1.6.37/libpng-1.6.37.tar.xz
tar -xf libpng-1.6.37.tar.xz && cd libpng-1.6.37
./configure --prefix=/usr/local/libpng --enable-shared --enable-static
make && make install

echo "export PATH=$PATH:/usr/local/libpng/bin" >> /etc/profile
source /etc/profile
cp /usr/local/libpng/lib/pkgconfig/libpng.pc /usr/lib64/pkgconfig/ -v

安装freetype

yum install harfbuzz harfbuzz-devel -y
cd .. && wget https://nchc.dl.sourceforge.net/project/freetype/freetype2/2.9/freetype-2.9.tar.bz2
tar -xf freetype-2.9.tar.bz2 && cd freetype-2.9
./configure --prefix=/usr/local/freetype --enable-shared --enable-static
make && make install

cp -v /usr/local/freetype/lib/pkgconfig/* /usr/lib64/pkgconfig/
echo "/usr/local/freetype/lib" >> /etc/ld.so.conf.d/freetype.conf
ldconfig
ldconfig -p |grep freetype

安装libgd

yum install libXpm-devel  libwebp-devel libwebp \
libtiff libtiff-devel fontconfig fontconfig-devel -y
cd .. && wget https://github.com/libgd/libgd/releases/download/gd-2.2.5/libgd-2.2.5.tar.gz
tar -xf libgd-2.2.5.tar.gz && cd libgd-2.2.5
./configure --prefix=/usr/local/libgd \
--enable-shared \
--enable-static \
--with-jpeg=/usr/local/jpeg \
--with-png=/usr/local/libpng \
--with-freetype=/usr/local/freetype \
--with-fontconfig \
--with-xpm=/usr/ \
--with-tiff \
--with-webp \
--with-zlib=/usr/local/zlib
make && make install

安装PHP7.2.25

yum install openldap openldap-devel openldap.i686 \
systemd-devel re2c openldap-devel.i686 -y
cd .. && wget https://www.php.net/distributions/php-7.2.25.tar.xz
tar -xf php-7.2.25.tar.xz && cd php-7.2.25

###强制启用iconv,Linux默认使用的是glibc
sed -i 's/iconv_impl_name=\"glibc\"/iconv_impl_name=\"\"/g' configure

./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/conf \
--with-config-file-scan-dir=/usr/local/php/conf/conf.d \
--enable-ftp \
--enable-sockets \
--enable-cli \
--enable-mbstring \
--enable-pcntl \
--enable-soap \
--enable-opcache \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-fpm \
--with-fpm-systemd \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-mhash \
--enable-simplexml \
--with-libxml-dir=/usr/local/libxml2 \
--with-openssl=/usr/local/openssl \
--with-gd=/usr/local/libgd \
--with-xpm-dir=/usr/lib64 \
--enable-zip \
--with-libzip=/usr/local/libzip \
--with-bz2=/usr/lib64 \
--with-curl=/usr/local/curl \
--with-zlib \
--with-zlib-dir=/usr/local/zlib \
--with-zlib-dir=/usr/local/zlib/lib \
--with-pcre-dir=/usr/local/pcre \
--with-freetype-dir=/usr/local/freetype \
--with-png-dir=/usr/local/libpng \
--with-jpeg-dir=/usr/local/jpeg \
--with-webp-dir \
--with-iconv-dir=/usr/local \
--with-pear \
--disable-debug \
--enable-bcmath \
--with-ldap \
--with-ldap-sasl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-gettext

make && make install

echo "export PATH=$PATH:/usr/local/php/bin:/usr/local/php/sbin" >> /etc/profile
source /etc/profile

#查看配置文件保存位置

php -i|grep php.ini 
mkdir -p /usr/local/php/conf

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

###优化php能用localhost访问MySQL数据库
CONF=/usr/local/php/conf
PHPINI=${CONF}/php.ini
sed -i 's/mysqli.default_socket =/mysqli.default_socket =\/usr\/local\/mysql\/run\/mysql\/3306\/mysql.sock/g' ${PHPINI}
        sed -i 's/pdo_mysql.default_socket=/pdo_mysql.default_socket=\/usr\/local\/mysql\/run\/mysql\/3306\/mysql.sock/g' ${PHPINI}

#centos 6或者init启动脚本管理
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod 755 /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
service php-fpm start 
#或者systemctl start php-fpm

#systemd管理
cp -v sapi/fpm/php-fpm.service /usr/lib/systemd/system/
systemctl start php-fpm
systemctl enable php-fpm

3、修改相关配置文件

mkdir -p /usr/www/html

cd /usr/local/nginx/conf/
mv nginx.conf nginx.conf.bak
touch nginx.conf
cat >> nginx.conf << EOF
user  nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /usr/www/html;
            index  index.html index.htm index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/www/html;
        }
        location ~ \.php\$ {
            root           /usr/www/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/www/html\$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
EOF

touch /usr/www/html/index.php
cat >> /usr/www/html/index.php << EOF
<?php
phpinfo();
?>
EOF
打开后显示phpinfo页面即表示安装成功
phpinfo
iconv

发表评论