LNMP是由Linux操作系统、Nginx中间件、MySQL数据库、PHP脚本语言四大开源免费产品组合而成的Web服务框架,早前已分享过Nginx、MySQL、PHP单独编译安装教程,今天给大家分享完整的LNMP编译安装方法。
实验环境
操作系统:CentOS 7.9
Nginx:1.18
MySQL:5.7.32
PHP:7.4.13
Zlib:1.2.11
Pcre:8.44
OpenSSL:1.1.1i
安装篇
第一步 安装依赖包
- # 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
第二步 下载、解压源码包
- # cd /usr/local/src/
- # wget --no-check-certificate http://zlib.net/zlib-1.2.11.tar.gz
- # wget --no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
- # wget --no-check-certificate http://nginx.org/download/nginx-1.18.0.tar.gz
- # wget --no-check-certificate https://www.php.net/distributions/php-7.4.13.tar.gz
- # wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.1i.tar.gz
- # wget --no-check-certificate https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.32.tar.gz
- # tar xzf pcre-8.44.tar.gz
- # tar xzf php-7.4.13.tar.gz
- # tar xzf zlib-1.2.11.tar.gz
- # tar xzf nginx-1.18.0.tar.gz
- # tar xzf openssl-1.1.1i.tar.gz
- # tar xzf mysql-boost-5.7.32.tar.gz
第三步 新建用户组、用户
- # groupadd nginx
- # groupadd mysql
- # useradd nginx -M -g nginx -s /sbin/nologin
- # useradd mysql -M -g mysql -s /sbin/nologin
第四步 编译安装Nginx
- # cd /usr/local/src/nginx-1.18.0
- # ./configure \
- --user=nginx \
- --group=nginx \
- --prefix=/usr/local/nginx \
- --with-pcre=/usr/local/src/pcre-8.44 \
- --with-openssl=/usr/local/src/openssl-1.1.1i \
- --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
- # make
- # make install
第五步 配置Nginx启动服务脚本、开机启动
- # vim /etc/init.d/nginx
- #! /bin/sh
- # 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
- ulimit -n 8192
- case "$1" in
- start)
- echo -n "Starting $NAME... "
- if [ -f $PIDFILE ]; then
- mPID=$(cat $PIDFILE)
- isStart=$(ps ax | awk '{ print $1 }' | grep -e "^${mPID}$")
- if [ "$isStart" != '' ]; then
- echo "$NAME (pid $(pidof $NAME)) already running."
- exit 1
- fi
- fi
- $NGINX_BIN -c $CONFIGFILE
- if [ "$?" != 0 ]; then
- echo " failed"
- exit 1
- else
- echo " done"
- fi
- ;;
- stop)
- echo -n "Stoping $NAME... "
- if [ -f $PIDFILE ]; then
- mPID=$(cat $PIDFILE)
- isStart=$(ps ax | awk '{ print $1 }' | grep -e "^${mPID}$")
- if [ "$isStart" = '' ]; then
- echo "$NAME is not running."
- exit 1
- fi
- else
- 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 [ -f $PIDFILE ]; then
- mPID=$(cat $PIDFILE)
- isStart=$(ps ax | awk '{ print $1 }' | grep -e "^${mPID}$")
- if [ "$isStart" != '' ]; then
- echo "$NAME (pid $(pidof $NAME)) already running."
- exit 1
- else
- echo "$NAME is stopped"
- exit 0
- fi
- else
- echo "$NAME is stopped"
- exit 0
- fi
- ;;
- restart)
- $0 stop
- sleep 1
- $0 start
- ;;
- reload)
- echo -n "Reload service $NAME... "
- if [ -f $PIDFILE ]; then
- mPID=$(cat $PIDFILE)
- isStart=$(ps ax | awk '{ print $1 }' | grep -e "^${mPID}$")
- if [ "$isStart" != '' ]; then
- $NGINX_BIN -s reload
- echo " done"
- else
- echo "$NAME is not running, can't reload."
- exit 1
- fi
- 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}"
- exit 1
- ;;
- esac
- # chmod +x /etc/init.d/nginx
- # chkconfig --add nginx
- # chkconfig nginx on
第六步 编译安装MySQL
- # cd /usr/local/src/mysql-5.7.32
- # 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.32/boost/boost_1_59_0
- # make
- # make install
第七步 优化数据库配置(4G-8G内存优化方案,仅供参考)
- # vim /etc/my.cnf
- [client]
- #password="123456"
- port=3306
- socket=/tmp/mysql.sock
- [mysqld]
- #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
- port=3306
- socket=/tmp/mysql.sock
- datadir=/usr/local/mysql/data
- default_storage_engine=InnoDB
- performance_schema_max_table_instances=400
- table_definition_cache=400
- skip-external-locking
- key_buffer_size=384M
- max_allowed_packet=100G
- table_open_cache=384
- sort_buffer_size=1024K
- net_buffer_length=4K
- 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-name-resolve
- 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=""
- 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=50
- innodb_max_dirty_pages_pct=90
- innodb_read_io_threads=4
- innodb_write_io_threads=4
- [mysqldump]
- user=root
- #password="123456"
- quick
- max_allowed_packet=500M
- [mysql]
- no-auto-rehash
- [myisamchk]
- key_buffer_size=64M
- sort_buffer_size=1M
- read_buffer=2M
- write_buffer=2M
- [mysqlhotcopy]
- interactive-timeout
第八步 修改MySQL数据库目录、配置文件权限
- # chown mysql:mysql /etc/my.cnf
- # chown -R mysql:mysql /usr/local/mysql
第九步 配置MySQL数据库环境变量
- # echo "export PATH="/usr/local/mysql/bin:$PATH"" >> /etc/profile
- # source /etc/profile
第十步 配置MySQL启动服务脚本、开机启动
- # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- # chmod +x /etc/init.d/mysqld
- # chkconfig --add mysqld
- # chkconfig mysqld on
第十一步 初始化MySQL数据库
- # mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
第十二步 编译安装PHP
- # cd /usr/local/src/php-7.4.13
- # ./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
- # make
- # make install
第十三步 创建PHP配置文件
- # cp /usr/local/src/php-7.4.13/php.ini-production /usr/local/php/etc/php.ini
- # cp /usr/local/src/php-7.4.13/sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf
- # cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
第十四步 配置PHP启动服务脚本、开机启动
- # cp /usr/local/src/php-7.4.13/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
- # chmod +x /etc/init.d/php-fpm
- # chkconfig --add php-fpm
- # chkconfig php-fpm on
第十五步 创建PHP测试网站根目录、测试页面
- # mkdir /usr/local/nginx/html/test
- # chown -R nginx:nginx /usr/local/nginx/html/test
- # echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/test/index.php
第十六步 配置Nginx
- # mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
- # 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测试网站虚拟主机
- # mkdir /usr/local/nginx/vhost
- # 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
- # service nginx start
- # service mysqld start
- # service php-fpm start
您可以选择一种方式赞助本站
支付宝扫一扫
微信扫一扫
赏