源码安装LNMP centos+nginx+php7.4+mysql5.7

lnmp : linux(centos7.4)+nginx+php7.4 +mysql

写在前面的

最近又来折腾自己的服务器 尝试源码 安装并记录下安装过程和遇到的问题 希望能帮到各位

想看yum 安装 lnmp的请点击这里跳转
添加用户组

#添加www 用户
groupadd -f www
useradd -g www www

nginx安装

nginx官网根据自己需求选择版本下载

#下载
cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
#解压
tar -zxvf nginx-1.16.1.tar.gz
#进入解压目录
cd nginx-1.16.1

安装依赖

yum -y install glibc-headers gcc gcc-c++ pcre pcre-devel zlib zlib-devel  openssl openssl-devel initscripts  autoconf automake make 

编译安装设置

mkdir /usr/local/nginx
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio 

安装

make
make install

设置nginx开机自启
在系统服务目录里创建nginx.service文件

vim /lib/systemd/system/nginx.service
##内容如下
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl enable nginx.service #开机自启
systemctl start nginx.service #启动nginx服务
systemctl stop nginx.service #停止nginx服务
systemctl restart nginx.service #重新启动服务

将nginx设置为service 命令启动

vim /etc/init.d/nginx

(nginx 和 NGINX_CONF_FILE 按照自己的安装路径自行设定)

#!/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:     /var/run/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/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 $prog -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

设置权限

chmod a+x /etc/init.d/nginx
#将nginx服务加入chkconfig管理列表
chkconfig --add /etc/init.d/nginx

这样可以使用service对nginx进行启动,重启等操作了。

#设置开机自启动
chkconfig nginx on 

nginx 编译安装完毕

#尝试重启nginx
service nginx restart 

输入首页 发现


在这里插入图片描述
  • 添加至环境变量
vim /etc/profile # 在文件中加入下面
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
source /etc/profile #使配置文件生效。

至此 nginx 安装成功!!

php 安装

访问php官网进行下载

在这里插入图片描述

右击鼠标获取下载地址执行

cd /usr/local/src/

#下载 php
cd ..
wget https://www.php.net/distributions/php-7.4.4.tar.gz#可以下载最新的版本   楼主下载的是7.4.4截图没更新
#解压
tar -zxvf php-7.4.4.tar.gz

因为php没有法直接配合nginx进行工作 所以得安装php-fpm

yum install -y  gcc gcc++  sqlite-devel automake autoconf libtool make epel-release libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel libicu-devel oniguruma oniguruma-devel gd-devel GeoIP GeoIP-devel GeoIP-data gperftools  libuuid-devel libblkid-devel libudev-devel fuse-devel libedit-devel libatomic_ops-devel trousers-devel gettext gettext-devel gettext-common-devel  libffi-devel e2fsprogs-devel uuid-devel libuuid-devel ImageMagick-devel  libsqlite3x-devel libwebp-devel libzip libzip-devel

先删除旧版本 libzip

yum remove -y libzip

下载编译安装

wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install
export LD_LIBRARY_PATH=/usr/local/libgd/lib #设置libgd库环境变量
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/" #设置libzip环境变量
#安装php到指定目录通常安转在/usr/local/php7(自定义的安装目录名称)   
cd php-7.4.4
mkdir /usr/local/php
mkdir /etc/php
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/php --with-freetype --with-jpeg  --with-zlib  --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-ftp --enable-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-zip  --enable-soap --with-gettext --enable-opcache --with-xsl --with-webp --with-xpm

写在后面的 编译的时候 会报各种错 php版本不一样 有些已经废弃 不要盲目的复制粘贴 要学会看手册连接上手册的问题地址

#编译
make #执行过程比较慢 安心等待
make install #php安装完毕

将php-fpm 添加到service

#编辑php-fpm.conf文件
cd /usr/local/php/etc
#发现只有php-fpm.conf.default 复制并重命名
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
#将 ;pid = run/php-fpm.pid  前面的分号去掉
# 去自己刚开始解压文件夹sapi/fpm中复制init.d.php-fpm 放到/etc/init.d下面并命名为php-fpm
cp /usr/local/src/php-7.4.4/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /usr/local/php/etc/php-fpm.d/www.conf.default  /usr/local/php/etc/php-fpm.d/www.conf
# 赋予脚本可执行命令,添加开机自启动
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

这样就可以使用service 启动php-fpm

配置nginx 启动php

vim /usr/local/nginx/conf/nginx.conf
#将 root   /home/www;添加到  listen       80; server_name  localhost; 下面
#在 location / { }后面添加

 location ~ \.php$ {
        root /home/www;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
         }
      
        #重启nginx 
        service nginx restart

Mysql 安装

# 安装依赖包
yum install -y  gcc gcc-c++ cmake ncurses ncurses-devel bison numactl libncurses*
#下载相应源码包mysql5.7 
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
#解压
tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
#移动
mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql
#赋权
chown -R www:www /usr/local/mysql
#创建文件夹 赋权 
mkdir -p /data/mysql
chown -R www:www /data/mysql
chown -R www:www /usr/local/mysql
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R www:www /var/log/mariadb/
  • 编辑/etc/my.cnf (将下面的内容添加到my.cnf中)
#配置my.conf
vim /etc/my.cnf
[mysqld]
bind-address=0.0.0.0
port=3306
user=www
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/tmp/mysql.sock
log-error=/data/mysql/mysql.err
pid-file=/data/mysql/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

初始化mysql

/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=www --initialize

将mysql添加到service

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
#重启
service mysql start

在/data/mysql找到mysql.err,查看root用户的临时密码(一般在最后一行):

ln -s  /usr/local/mysql/bin/mysql    /usr/bin
cat /data/mysql/mysql-error.log 
#修改密码  
mysql -uroot -p #输入刚才看到的临时密码
SET PASSWORD = PASSWORD('yourpwd');#这里输入的是自己设定的密码
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
use mysql; 
grant all privileges on *.* to 'root'@'%' identified by '~~yourpwd~~  ';
update user set host = '%' where user = 'root'; 
select host, user from user; 
FLUSH PRIVILEGES;

设置开机启动

chkconfig --add mysql 
chkconfig mysql  on

再使用可视化工具连接远程mysql 可能会报错如果遇到请移步 解决Nacicat无法连接远程mysql 报错10038


你可能感兴趣的:(源码安装LNMP centos+nginx+php7.4+mysql5.7)