lnmp环境手动部署


目录
  • lnmp环境手动部署
  • 1. 安装nginx
  • 2. 安装mysql
  • 3. 安装php
  • 4. 配置nginx
  • 5. 访问测试页面
  • 6.nginx开机自启
    • systemd方式
    • 通过chkconfig方式

lnmp环境手动部署

环境说明

系统 IP 需要安装的服务
centos7 192.168.32.125 nginx mysql-5.7 php-7.4.9 php-mysql

已关闭防火墙和selinux,并配置好了yum源,包括epel源

1. 安装nginx

#创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

#安装依赖包
[root@localhost ~]#  yum -y install pcre-devel openssl openssl-devel gd gd-devel gcc gcc-c++
......
[root@localhost ~]# yum -y groups mark install 'Development Tools'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Marked install: Development Tools


#创建日志存放目录
[root@localhost ~]#  mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx

#下载nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz

#编译安装
[root@localhost ~]# tar xf nginx-1.18.0.tar.gz 
[root@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@localhost nginx-1.18.0]# ./configure \
 --prefix=/usr/local/nginx \
 --user=nginx \
 --group=nginx \
 --with-debug \
 --with-http_ssl_module \
 --with-http_realip_module \
 --with-http_image_filter_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_stub_status_module \
 --http-log-path=/var/log/nginx/access.log \
 --error-log-path=/var/log/nginx/error.log


[root@localhost nginx-1.18.0]# make -j `nproc`
[root@localhost nginx-1.18.0]# make install



#配置环境变量
[root@localhost nginx-1.18.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost nginx-1.18.0]# . /etc/profile.d/nginx.sh
[root@localhost nginx-1.18.0]# echo $PATH 
/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin


#启动nginx
[root@localhost nginx-1.18.0]# nginx 
[root@localhost nginx-1.18.0]# ss -antl
State      Recv-Q Send-Q                 Local Address:Port                                Peer Address:Port              
LISTEN     0      100                        127.0.0.1:25                                             *:*                  
LISTEN     0      128                                *:80                                             *:*                  
LISTEN     0      128                                *:22                                             *:*                  
LISTEN     0      100                            [::1]:25                                          [::]:*                  
LISTEN     0      128                             [::]:22                                          [::]:*  

2. 安装mysql

#安装依赖包
[root@localhost ~]# yum -y install libaio ncurses-devel openssl-devel openssl cmake mariadb-devel

#创建用户和组
[root@localhost ~]# useradd -r -M -s /sbin/nologin mysql

#下载二进制格式的mysql软件包
[root@localhost ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# mv mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysql

#修改目录/usr/local/mysql的属主属组
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql
[root@localhost ~]# ll /usr/local/mysql -d
drwxr-xr-x 9 mysql mysql 129 Aug  6 10:04 /usr/local/mysql

#添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost ~]# . /etc/profile.d/mysql.sh 
[root@localhost ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin


#建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/

#初始化数据库
[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
2020-08-06T14:18:46.668749Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-06T14:18:46.887930Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-06T14:18:46.923311Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-06T14:18:46.998617Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: bdd14729-d7ef-11ea-8207-000c29f66cbc.
2020-08-06T14:18:47.013567Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-06T14:18:47.452070Z 0 [Warning] CA certificate ca.pem is self signed.
2020-08-06T14:18:47.906673Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.


//配置mysql
[root@localhost ~]#  ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig
 
#配置mysql配置文件
[root@localhost ~]# cat > /etc/my.cnf < set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye





3. 安装php

#配置php的源
[root@localhost ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@localhost ~]# yum -y localinstall remi-release-7.rpm 
[root@localhost ~]# yum makecache --enablerepo=remi-php74

#安装依赖包
[root@localhost ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel sqlite-devel oniguruma-devel php74-php-mysqlnd
[root@localhost ~]# yum -y install https://mirrors.aliyun.com/centos/8.2.2004/AppStream/x86_64/os/Packages/libzip-1.5.2-1.module_el8.2.0%2B314%2B53b99e08.x86_64.rpm
[root@localhost ~]# yum -y install https://mirrors.aliyun.com/centos/8.2.2004/AppStream/x86_64/os/Packages/libzip-devel-1.5.2-1.module_el8.2.0%2B314%2B53b99e08.x86_64.rpm


#下载php,安装
[root@localhost ~]# wget https://www.php.net/distributions/php-7.4.9.tar.gz
[root@localhost ~]# tar xf php-7.4.9.tar.gz 
[root@localhost ~]# cd php-7.4.9
[root@localhost php-7.4.9]# ./configure --prefix=/usr/local/php7  \
 --with-config-file-path=/etc \
 --enable-fpm \
 --enable-inline-optimization \
 --disable-debug \
 --disable-rpath \
 --enable-shared \
 --enable-soap \
 --with-openssl \
 --enable-bcmath \
 --with-iconv \
 --with-bz2 \
 --enable-calendar \
 --with-curl \
 --enable-exif  \
 --enable-ftp \
 --enable-gd \
 --with-jpeg \
 --with-zlib-dir \
 --with-freetype \
 --with-gettext \
 --enable-json \
 --enable-mbstring \
 --enable-pdo \
 --with-mysqli=mysqlnd \
 --with-pdo-mysql=mysqlnd \
 --with-readline \
 --enable-shmop \
 --enable-simplexml \
 --enable-sockets \
 --with-zip \
 --enable-mysqlnd-compression-support \
 --with-pear \
 --enable-pcntl \
 --enable-posix

[root@localhost php-7.4.9]# make -j `nproc` && make install
......

#安装后配置环境变量
[root@localhost php-7.4.9]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@localhost php-7.4.9]# source /etc/profile.d/php7.sh
[root@localhost php-7.4.9]# php -v
PHP 7.4.9 (cli) (built: Aug  6 2020 11:11:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies


#配置php-fpm
[root@localhost php-7.4.9]# cp php.ini-production /etc/php.ini
[root@localhost php-7.4.9]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.9]# chmod +x /etc/rc.d/init.d/php-fpm

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





#编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
#配置fpm的相关选项为你所需要的值:
[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.conf
.....
.....
pm.max_children = 50    ;最多同时提供50个进程提供50个并发服务
pm.start_servers = 5    ;启动时启动5个进程
pm.min_spare_servers = 2    ;最小空闲进程数
pm.max_spare_servers = 8 


#启动php-fpm,设置开机自启
[root@localhost php-7.4.9]# service php-fpm start
Starting php-fpm  done
[root@localhost php-7.4.9]# chkconfig --add php-fpm
[root@localhost php-7.4.9]# ss -tanl
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
LISTEN      0      100                     127.0.0.1:25                                          *:*                  
LISTEN      0      128                     127.0.0.1:9000                                        *:*                  
LISTEN      0      128                             *:80                                          *:*                  
LISTEN      0      128                             *:22                                          *:*                  
LISTEN      0      100                         [::1]:25                                       [::]:*                  
LISTEN      0      80                           [::]:3306                                     [::]:*                  
LISTEN      0      128                          [::]:22                                       [::]:*   

[root@localhost php-7.4.9]# vim /usr/local/php7/etc/php-fpm.d/www.conf
#修改listen = 127.0.0.1:9000为listen = 0.0.0.0:9000
[root@localhost php-7.4.9]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@localhost php-7.4.9]# ss -antl
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
LISTEN      0      100                     127.0.0.1:25                                          *:*                  
LISTEN      0      128                             *:10050                                       *:*                  
LISTEN      0      128                             *:10051                                       *:*                  
LISTEN      0      128                             *:9000                                        *:*                              
LISTEN      0      128                             *:22                                          *:*                  
LISTEN      0      100                         [::1]:25                                       [::]:*                  
LISTEN      0      80                           [::]:3306                                     [::]:*                  
LISTEN      0      128                          [::]:22                                       [::]:*                 



4. 配置nginx

#在server大括号内配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
.....

        location / {
            root   html;
            index  index.php index.html index.htm;
        }


        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

#生成测试页面
[root@localhost ~]# cat > /usr/local/nginx/html/index.php <
EOF


#重载nginx配置文件
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ss -tanl
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
LISTEN      0      100                     127.0.0.1:25                                          *:*                  
LISTEN      0      128                     127.0.0.1:9000                                        *:*                  
LISTEN      0      128                             *:80                                          *:*                  
LISTEN      0      128                             *:22                                          *:*                  
LISTEN      0      100                         [::1]:25                                       [::]:*                  
LISTEN      0      80                           [::]:3306                                     [::]:*                  
LISTEN      0      128                          [::]:22                                       [::]:*        

5. 访问测试页面

6.nginx开机自启

systemd方式

[root@localhost ~]# vi /usr/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 daemon-reload
systemctl start nginx

通过chkconfig方式

参考:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

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:     /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/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/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

修改内容:

注意修改为自己的实际路径:
nginx="/usr/local/nginx/sbin/nginx"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

#增加权限
chmod a+x /etc/init.d/nginx
#加入chkconfig并设置开机自启
chkconfig --add /etc/init.d/nginx
chkconfig nginx on