Linux-CentOS 7.7编译安装LNMP,阿里云Centos 7安装LNMP(源码编译安装LNMP)

LNMP是由Linux操作系统、Nginx中间件、MySQL数据库、PHP脚本语言四大开源免费产品组合而成的Web服务框架,早前网站已分享过Nginx、MySQL、PHP单独编译安装教程,或许有些朋友仍不清楚怎么使用,今天给大家分享完整的LNMP使用配置方法。

Linux编译安装Nginx

Linux编译安装MySQL

Linux编译安装PHP

LNMP实验环境

操作系统:CentOS 7.7

主机地址:192.168.168.21

Nginx:1.16.1

MySQL:5.7.28

PHP:7.4.1

Zlib:1.2.11


Pcre:8.43

OpenSSL:1.0.2u

LNMP安装篇

第一步 安装依赖包

[root@wanghualang ~]# yum -y install epel-release
[root@wanghualang ~]# yum -y install gcc gcc-c++ autoconf automake wget vim make cmake openssl-devel bison-devel ncurses-devel libsqlite3x-devel oniguruma-devel curl-devel libxml2-devel libjpeg-devel libpng-devel freetype-devel libicu-devel libsodium-devel

第二步 下载、解压源码包

[root@wanghualang ~]# cd /usr/local/src/
[root@wanghualang src]# wget --no-check-certificate http://zlib.net/zlib-1.2.11.tar.gz
[root@wanghualang src]# wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
[root@wanghualang src]# wget --no-check-certificate http://nginx.org/download/nginx-1.16.1.tar.gz
[root@wanghualang src]# wget --no-check-certificate https://www.php.net/distributions/php-7.4.1.tar.gz
[root@wanghualang src]# wget --no-check-certificate https://www.openssl.org/source/openssl-1.0.2t.tar.gz
[root@wanghualang src]# wget --no-check-certificate https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.28.tar.gz
[root@wanghualang src]# tar xzf pcre-8.43.tar.gz  
[root@wanghualang src]# tar xzf php-7.4.1.tar.gz  
[root@wanghualang src]# tar xzf zlib-1.2.11.tar.gz
[root@wanghualang src]# tar xzf nginx-1.16.1.tar.gz
[root@wanghualang src]# tar xzf openssl-1.0.2u.tar.gz
[root@wanghualang src]# tar xzf mysql-boost-5.7.28.tar.gz

第三步 新建用户组、用户

[root@wanghualang ~]# groupadd nginx
[root@wanghualang ~]# groupadd mysql
[root@wanghualang ~]# useradd nginx -M -g nginx -s /sbin/nologin
[root@wanghualang ~]# useradd mysql -M -g mysql -s /sbin/nologin

第四步 编译安装Nginx(按需添加更多功能模块)

[root@wanghualang ~]# cd /usr/local/src/nginx-1.16.1
[root@wanghualang nginx-1.16.1]# ./configure /
--user=nginx /
--group=nginx /
--prefix=/usr/local/nginx /
--with-pcre=/usr/local/src/pcre-8.43 /
--with-openssl=/usr/local/src/openssl-1.0.2u /
--with-zlib=/usr/local/src/zlib-1.2.11 /
--with-http_gzip_static_module /
--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  /
--with-http_v2_module
[root@wanghualang nginx-1.16.1]# make
[root@wanghualang nginx-1.16.1]# make install

第五步 配置Nginx启动服务脚本、开机启动

[root@wanghualang ~]# vim /etc/init.d/nginx
#!/bin/bash    
#chkconfig: 2345 55 25    
    
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin    
NAME=nginx    
NGINX_BIN=/usr/local/nginx/sbin/$NAME    
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf    
PIDFILE=/usr/local/nginx/logs/$NAME.pid  
  
if [ -s /bin/ss ]; then    
    StatBin=/bin/ss    
else    
    StatBin=/bin/netstat    
fi    
    
    
case "$1" in    
    start)    
        echo -n "Starting $NAME... "    
    
        if $StatBin -tnpl | grep -q nginx;then    
            echo "$NAME (pid `pidof $NAME`) already running."    
            exit 1    
        fi    
    
        $NGINX_BIN -c $CONFIGFILE    
    
        if [ "$?" != 0 ] ; then    
            echo " failed"    
            exit 1    
        else    
            echo " done"    
        fi    
        ;;    
    
    stop)    
        echo -n "Stoping $NAME... "    
    
        if ! $StatBin -tnpl | grep -q nginx; then    
            echo "$NAME is not running."    
            exit 1    
        fi    
    
        $NGINX_BIN -s stop    
    
        if [ "$?" != 0 ] ; then    
            echo " failed. Use force-quit"    
            exit 1    
        else    
            echo " done"    
        fi    
        ;;    
    
    status)    
        if $StatBin -tnpl | grep -q nginx; then    
            PID=`pidof nginx`    
            echo "$NAME (pid $PID) is running..."    
        else    
            echo "$NAME is stopped."    
            exit 0    
        fi    
        ;;    
    
    force-quit|kill)    
        echo -n "Terminating $NAME... "    
    
        if ! $StatBin -tnpl | grep -q nginx; then    
            echo "$NAME is is stopped."    
            exit 1    
        fi    
    
        kill `pidof $NAME`    
    
        if [ "$?" != 0 ] ; then    
            echo " failed"    
            exit 1    
        else    
            echo " done"    
        fi    
        ;;    
    
    restart)    
        $0 stop    
        sleep 1    
        $0 start    
        ;;    
    
    reload)    
        echo -n "Reload service $NAME... "    
    
        if $StatBin -tnpl | grep -q nginx; then    
            $NGINX_BIN -s reload    
            echo " done"    
        else    
            echo "$NAME is not running, can't reload."    
            exit 1    
        fi    
        ;;    
    
    configtest)    
        echo -n "Test $NAME configure files... "    
    
        $NGINX_BIN -t    
        ;;    
    
    *)    
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"    
        exit 1    
        ;;
esac
[root@wanghualang ~]# chmod +x /etc/init.d/nginx
[root@wanghualang ~]# chkconfig --add nginx
[root@wanghualang ~]# chkconfig nginx on

第六步 编译安装MySQL(按需添加更多功能模块)

[root@wanghualang ~]# cd /usr/local/src/mysql-5.7.28
[root@wanghualang mysql-5.7.28]# cmake  /
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql  /
-DSYSCONFDIR=/etc  /
-DMYSQL_TCP_PORT=3306  /
-DEXTRA_CHARSETS=all  /
-DDEFAULT_CHARSET=utf8mb4  /
-DDEFAULT_COLLATION=utf8mb4_general_ci  /
-DWITH_MYISAM_STORAGE_ENGINE=1  /
-DWITH_INNOBASE_STORAGE_ENGINE=1  /
-DWITH_PARTITION_STORAGE_ENGINE=1  /
-DWITH_FEDERATED_STORAGE_ENGINE=1  /
-DWITH_EMBEDDED_SERVER=1  /
-DENABLED_LOCAL_INFILE=1  /
-DWITH_BOOST=/usr/local/src/mysql-5.7.28/boost/boost_1_59_0
[root@wanghualang mysql-5.7.28]# make
[root@wanghualang mysql-5.7.28]# make install

第七步 优化数据库配置(4G内存优化方案,仅供参考)

[root@wanghualang ~]# mv /etc/my.cnf /etc/my.cnf.bak
[root@wanghualang ~]# vim /etc/my.cnf
[client]     
port=3306      
socket=/tmp/mysql.sock      
    
[mysql]      
no-auto-rehash      
      
[mysqld]      
port=3306      
bind_address=127.0.0.1      
binlog_cache_size=128K      
thread_stack=256K      
join_buffer_size=2048K      
query_cache_type=1      
max_heap_table_size=512M      
pid-file=/tmp/mysql.pid      
socket=/tmp/mysql.sock      
datadir=/usr/local/mysql/data      
skip-external-locking      
performance_schema_max_table_instances=400      
table_definition_cache=400      
key_buffer_size=384M      
max_allowed_packet=100G      
table_open_cache=384      
sort_buffer_size=1024K      
net_buffer_length=8K      
read_buffer_size=1024K      
read_rnd_buffer_size=768K      
myisam_sort_buffer_size=16M      
thread_cache_size=128      
query_cache_size=192M      
tmp_table_size=512M      
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES      
explicit_defaults_for_timestamp=true      
#skip-networking      
max_connections=300      
max_connect_errors=100      
open_files_limit=65535      
#log-bin=mysql-bin      
#binlog_format=mixed      
server-id=1      
expire_logs_days=10      
slow_query_log=1      
slow-query-log-file=/usr/local/mysql/data/mysql-slow.log      
long_query_time=3      
#log_queries_not_using_indexes=on      
early-plugin-load=""      
default_storage_engine=InnoDB      
innodb_data_home_dir=/usr/local/mysql/data      
innodb_data_file_path=ibdata1:10M:autoextend      
innodb_log_group_home_dir=/usr/local/mysql/data      
innodb_buffer_pool_size=512M      
innodb_log_file_size=128M      
innodb_log_buffer_size=32M      
innodb_flush_log_at_trx_commit=1      
innodb_lock_wait_timeout=120      
innodb_max_dirty_pages_pct=90      
innodb_read_io_threads=2      
innodb_write_io_threads=2      
      
[mysqldump]      
quick      
max_allowed_packet=16M      
      
[myisamchk]
key_buffer_size=64M
sort_buffer_size=1M
read_buffer=2M
write_buffer=2M
[mysqlhotcopy]
interactive-timeout

第八步 修改MySQL数据库目录、配置文件权限

[root@wanghualang ~]# chown mysql:mysql /etc/my.cnf
[root@wanghualang ~]# chown -R mysql:mysql /usr/local/mysql

第九步 配置MySQL数据库环境变量

[root@wanghualang ~]# echo "export PATH="/usr/local/mysql/bin:$PATH"" >> /etc/profile
[root@wanghualang ~]# source /etc/profile

第十步 配置MySQL启动服务脚本、开机启动

[root@wanghualang ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@wanghualang ~]# chmod +x /etc/init.d/mysqld
[root@wanghualang ~]# chkconfig --add mysqld
[root@wanghualang ~]# chkconfig mysqld on

第十一步 初始化MySQL数据库

[root@wanghualang ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

第十二步 编译安装PHP(按需添加更多功能模块)

[root@wanghualang ~]# cd /usr/local/src/php-7.4.1
[root@wanghualang php-7.4.1]# ./configure /
--prefix=/usr/local/php /
--with-config-file-path=/usr/local/php/etc  /
--enable-mysqlnd  /
--enable-xml  /
--enable-bcmath  /
--enable-shmop  /
--enable-sysvsem  /
--enable-inline-optimization  /
--enable-mbregex  /
--enable-mbstring  /
--enable-intl  /
--enable-ftp  /
--enable-pcntl  /
--enable-sockets  /
--enable-soap  /
--enable-opcache  /
--enable-fpm  /
--enable-gd  /
--with-fpm-user=nginx  /
--with-fpm-group=nginx  /
--with-mysqli=mysqlnd  /
--with-pdo-mysql=mysqlnd  /
--with-iconv-dir  /
--with-freetype  /
--with-jpeg  /
--with-zlib  /
--with-libxml  /
--with-curl  /
--with-openssl  /
--with-mhash  /
--with-xmlrpc  /
--with-gettext  /
--with-sodium  /
--disable-fileinfo  /
--disable-rpath  /
--disable-debug
[root@wanghualang ~]# make
[root@wanghualang ~]# make install

第十三步 创建PHP配置文件

[root@wanghualang ~]# cp /usr/local/src/php-7.4.1/php.ini-production /usr/local/php/etc/php.ini
[root@wanghualang ~]# cp /usr/local/src/php-7.4.1/sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf
[root@wanghualang ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

第十四步 配置PHP启动服务脚本、开机启动

[root@wanghualang ~]# cp /usr/local/src/php-7.4.1/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@wanghualang ~]# chmod +x /etc/init.d/php-fpm
[root@wanghualang ~]# chkconfig --add php-fpm
[root@wanghualang ~]# chkconfig php-fpm on

第十五步 创建PHP测试网站根目录、测试页面

[root@wanghualang ~]# mkdir /usr/local/nginx/html/test  
[root@wanghualang ~]# chown -R nginx:nginx /usr/local/nginx/html/test  
[root@wanghualang ~]# echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/test/index.php

第十六步 配置Nginx

[root@wanghualang ~]# mv /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.bak
[root@wanghualang ~]# vim /usr/local/nginx/conf/nginx.conf
#运行用户    
user nginx nginx;    
    
#工作进程    
worker_processes auto;    
    
#最大文件打开数    
worker_rlimit_nofile 51200;    
    
#进程PID    
pid /usr/local/nginx/logs/nginx.pid;    
    
#错误日志    
error_log /usr/local/nginx/logs/error.log crit;    
    
#工作模式及连接数上限    
events {    
  use epoll;    
  worker_connections 51200;    
  multi_accept on;    
}    
    
http {    
    
  #加载虚拟主机配置文件    
  include /usr/local/nginx/vhost/*.conf;    
    
  #文件扩展名与类型映射表    
  include mime.types;    
    
  #默认文件类型    
  default_type application/octet-stream;    
    
  #请求缓存    
  server_names_hash_bucket_size 512;    
  client_header_buffer_size 32k;    
  large_client_header_buffers 4 32k;    
  client_max_body_size 50m;    
    
  #高效传输模式    
  sendfile on;    
  tcp_nopush on;    
  tcp_nodelay on;    
    
  #连接超时时间    
  keepalive_timeout 60;    
    
  #FastCGI优化    
  fastcgi_connect_timeout 300;    
  fastcgi_send_timeout 300;    
  fastcgi_read_timeout 300;    
  fastcgi_buffer_size 64k;    
  fastcgi_buffers 4 64k;    
  fastcgi_busy_buffers_size 128k;    
  fastcgi_temp_file_write_size 256k;    
  fastcgi_intercept_errors on;    
    
  #开启GZIP压缩功能    
  gzip on;    
  gzip_min_length 1k;    
  gzip_buffers 4 16k;    
  gzip_http_version 1.1;    
  gzip_comp_level 2;    
  gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;    
  gzip_vary on;    
  gzip_proxied expired no-cache no-store private auth;    
  gzip_disable "MSIE [1-6]/.";    
    
  #限制访问频率    
  #limit_conn_zone $binary_remote_addr zone=perip:10m;    
  #limit_conn_zone $server_name zone=perserver:10m;    
    
  #隐藏响应header和错误通知中的版本号    
  server_tokens off;    
  access_log off;    
}

第十七步 配置PHP测试网站虚拟主机

[root@wanghualang ~]# mkdir /usr/local/nginx/vhost
[root@wanghualang ~]# vim /usr/local/nginx/vhost/test.conf
server {    
    
  #监听端口    
  listen 80;    
    
  #网站根目录    
  root /usr/local/nginx/html/test/;    
    
  #虚拟主机名称    
  server_name 192.168.168.21;    
    
  #网站主页排序    
  index index.php index.html index.htm default.php default.htm default.html;    
    
  #网站访问、错误日志    
  access_log /usr/local/nginx/logs/test.access.log;    
  error_log /usr/local/nginx/logs/test.error.log;    
    
  #流量限制(网站最大并发数500|单IP访问最大并发数50|每个请求流量上限1024KB)    
  #limit_conn perserver 500;    
  #limit_conn perip 50;    
  #limit_rate 1024k;    
      
  #配置错误页面    
  #error_page 404 /404.html;    
  #error_page 500 502 503 504 /50x.html;    
    
  #禁止访问文件和目录    
  location ~ ^/(/.user.ini|/.htaccess|/.git|/.svn|/.project|LICENSE|README.md) {    
    return 404;    
  }    
    
  #配置资源防盗链    
  location ~ .*/.(jpg|jpeg|gif|png|js|css)$ {    
    expires 30d;    
    access_log /dev/null;    
    valid_referers none blocked 192.168.168.21;    
    if ($invalid_referer) {    
      return 404;    
    }    
  }    
    
  #配置图片资源缓存时间    
  location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$ {    
    expires 30d;    
    error_log off;    
    access_log /dev/null;    
  }    
      
  #设置样式资源缓存时间    
  location ~ .*/.(js|css)?$ {    
    expires 12h;    
    error_log off;    
    access_log /dev/null;    
  }    
    
  #解析PHP    
  location ~* /.php$ {    
    fastcgi_index index.php;    
    fastcgi_pass 127.0.0.1:9000;    
    include fastcgi_params;    
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;    
  }
}

第十八步 启动Nginx、MySQL、PHP

[root@wanghualang ~]# service nginx start
[root@wanghualang ~]# service mysqld start
[root@wanghualang ~]# service php-fpm start

第十九步 测试PHP网站

Linux-CentOS 7.7编译安装LNMP,阿里云Centos 7安装LNMP(源码编译安装LNMP)

官方QQ群号码:922069959(空)1093596563(空)

您可能感兴趣的文章

One thought on “Linux-CentOS 7.7编译安装LNMP,阿里云Centos 7安装LNMP(源码编译安装LNMP)

留言评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code