centos7 lnmp

1.多线程下载工具

yum install axel
yum install gcc gcc-c++ cmake make autoconf

2.安装nginx

2.1 安装依赖  

    nginx 依赖ssl、rewrite、gzip

2.2 建立目录

cd /home

mkdir webserver

cd webserver

mkdir nginx mysql php library

cd library

mkdir openssl pcre zlib

2.3 ssl依赖 openssl,rewrite依赖pcre,gzip依赖zlib

#openssl

wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
yum install libtool perl-core zlib-devel -y  #安装响应的组件
tar -zxvf openssl-1.1.1a.tar.gz
cd openssl-1.1.1a
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared zlib
make && make install

vi /etc/profile.d/openssl.sh
# /etc/profile.d/openssl.sh 
pathmunge /usr/local/openssl/bin

vi /etc/ld.so.conf.d/openssl-1.1.0g.conf
# /etc/ld.so/conf.d/openssl-1.1.0g.conf 
/usr/local/openssl/lib
#执行命令使openssl新版本lib路径生效
ldconfig -v
#重开shell


#zlib
axel -n 10 http://www.zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/home/webserver/library/zlib 
make && make install


#pcre
axel -n 10 https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz
cd pcre-8.41
./configure --prefix=/home/webserver/library/pcre 
make && make install

2.4安装nginx  --with-pcre=路径 为源码的路径

cd ../
axel -n 10 http://nginx.org/download/nginx-1.14.2.tar.gz
tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure \
--prefix=/home/weserver/nginx \
--with-http_realip_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_dav_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-pcre=/home/webserver/library/pcre-8.41 \
--with-openssl=/home/webserver/library/openssl-1.1.1a \
--with-openssl-opt='enable-tls1_3 enable-weak-ssl-ciphers' \
--with-http_ssl_module \
--with-http_v2_module \
--with-zlib=/home/webserver/library/zlib-1.2.11 
make && make install

2.5常用命令

#测试是否安装成功
cd /home/webserver/nginx/sbin
./nginx -t

查看Nginx的版本号:./nginx -v
启动Nginx:./nginx
快速停止或关闭Nginx:./nginx -s stop
正常停止或关闭Nginx:./nginx -s quit
配置文件修改重装载命令:./nginx -s reload


2.6将nginx加入系统命令

vim /etc/init.d/nginx

#!/bin/bash
#nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/alidata/server/nginx/sbin/nginx
nginx_config=/alidata/server/nginx/conf/nginx.conf
nginx_pid=/alidata/server/nginx/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ [${NETWORKING} = "no"] ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /alidata/server/nginx/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL

保存,加入服务

chmod 755 /etc/init.d/nginx
chkconfig --add nginx

2.7创建nginx.service,加入服务

vim /lib/systemd/system/nginx.service

[Unit] 
Description=nginx 
After=network.target 
 
[Service] 
Type=forking 
ExecStart=/alidata/server/nginx/sbin/nginx 
ExecReload=/alidata/server/nginx/sbin/nginx -s reload 
ExecStop=/alidata/server/nginx/sbin/nginx -s quit
PrivateTmp=true 
 
[Install] 
WantedBy=multi-user.target

保存,重新加载

systemctl daemon-reload

设置开机启动

systemctl enable nginx

2.8通过服务操作nginx

service nginx start 启动nginx
service nginx stop 关闭nginx
service nginx restart 重启nginx
service nginx reload 重新加载nginx


三、安装php

3.1安装依赖

yum -y install libxml2 
yum -y install libxml2-devel 
yum -y install openssl 
yum -y install openssl-devel 
yum -y install curl-devel 
yum -y install libjpeg-devel 
yum -y install libpng-devel 
yum -y install freetype-devel
yum -y install bzip2-devel
yum -y install libmcrypt libmcrypt-devel
yum -y install postgresql-devel
yum -y install aspell-devel
yum -y install readline-devel
yum -y install libxslt-devel
yum -y install net-snmp-devel
yum -y install unixODBC-devel
yum -y install libicu-devel
yum -y install libc-client-devel
yum -y install libXpm-devel
yum -y install libvpx-devel
yum -y install enchant-devel
yum -y install openldap
yum -y install openldap-devel
yum -y install db4-devel
yum -y install gmp-devel
yum -y install sqlite-devel
yum -y install mysql-devel

3.2其他依赖

cmake 需要大于3.0.2

wget https://cmake.org/files/v3.13/cmake-3.13.0.tar.gz
tar -zxvf cmake-3.13.0.tar.gz
cd cmake-3.13.0
./bootstrap --prefix=/usr/
make && make install
cmake --version

安装libzip

wget https://libzip.org/download/libzip-1.5.1.tar.gz
tar -zxvf libzip-1.5.1.tar.gz
cd libzip-1.5.1
mkdir build && cd build && cmake .. && make && make install

echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf&&ldconfig -v

安装oniguruma(php7.4需要)

git clone https://github.com/kkos/oniguruma
cd oniguruma
autoreconf -vfi
./configure
make
make install
#  再执行
autoreconf -fi

export PKG_CONFIG_PATH= 'oniguruma的路径'

安装libiconv(php7.4需要)

wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
tar -zxvf libiconv-1.16.tar.gz
cd libiconv-1.16
./configure --prefix=/usr/local 
make  
make install
ldconfig

编译php7.4的时候加上-liconv 
make ZEND_EXTRA_LIBS='-liconv'

在更新php版本后,所有的php扩展都需要使用make clean 清理编译缓存,重新编译安装。否则会由于版本不一致报

Unable to initialize module 无法初始化对应模块的错误


3.3添加用户和组

groupadd -r www && adduser -r -g www -s /bin/false -d /home/www -M www


3.4安装php

cd /home/webserver/

axel -n 10 http://cn2.php.net/distributions/php-7.4.1.tar.gz

tar -zxvf php-7.4.1.tar.gz

cd php-7.4.1

下面代码按需求修改后全部复制进去一次性执行(php7.3去掉 --with-mcrypt, --enable-gd-native-ttf, --with-libmbfl)

./configure \
--prefix=/home/webserver/php \
--with-config-file-path=/home/webserver/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-xmlrpc \
--with-openssl \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache \
--enable-pcntl \
--enable-posix
make && make install

查看版本信息
/home/webserver/php/bin/php -v

3.4创建配置信息

3.4.1创建www.conf

cd /home/webserver/php/etc/php-fpm.d
cp

3.4.2创建php-fpm.conf配置文件

cd ../
cp php-fpm.conf.default php-fpm.conf

3.4.3创建php.ini配置文件

将安装源文件目录里的php.ini-production或者php.ini-development修改后缀拷贝到php安装目录的etc文件夹内

cd /home/webserver/php-7.4.1
cp php.ini-production /home/webserver/php/etc/php.ini

3.5加入环境变量

vim /etc/profile

PATH=/home/webserver/php/bin:$PATH
export PATH

#保存
source /etc/profile

3.6加入到服务

cp /home/webserver/php-7.4.1/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm

配置php-fpm.conf

vim /home/webserver/php/etc/php-fpm.conf
#去掉下面语句前面的分号
pid = run/php-fpm.pid

3.7配置开机启动

chkconfig --add /etc/init.d/php-fpm
chkconfig php-fpm on

3.8配置nginx解析php

cd /home/webserver/nginx/conf
vim nginx.conf

#去掉php前的#
#保存
location ~ .*\.(php|php5)?$ {
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       include        fastcgi.conf;
}

#重新加载配置文件
service nginx reload


四、安装mysql

4.1安装依赖

yum -y install make bison-devel ncurses-devel libaio libaio-devel perl-Data-Dumper net-tools

4.2编译配置

axel -n 10  

tar -zxvf mysql-8.0.13.tar.gz

cd mysql-8.0.13

cmake . \
    -DCMAKE_INSTALL_PREFIX=/home/webserver/mysql \
    -DMYSQL_DATADIR=/home/webserver/mysql/data \
    -DSYSCONFDIR=/etc \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_MEMORY_STORAGE_ENGINE=1 \
    -DWITH_READLINE=1 \
    -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
    -DMYSQL_TCP_PORT=3306 \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DEXTRA_CHARSETS=all \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DDOWNLOAD_BOOST=1 \
    -DWITH_BOOST=/home/webserver/boost
    
make && make install

4.3配置用户权限

groupadd -r mysql && adduser -r -g mysql -s /bin/false -M mysql

chown -R mysql:mysql /home/webserver/mysql

4.5数据库初始化

cd /home/webserver/mysql/bin
./mysqld --initialize --basedir=/home/webserver/mysql --datadir=/home/webserver/mysql/data --user=mysql

4.6加入到系统服务

cp /home/webserver/mysql/support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --add mysql

4.7修改配置

vim /etc/my.cnf

#修改配置
[mysqld]
datadir=/alidata/server/mysql/data
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/alidata/server/mysql/log/mariadb.log
pid-file=/alidata/server/mysql/log/run/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

保存文件后建立mysql.sock的存放目录,并分配给mysql用户和组

mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql

创建日志文件mariadb.log

touch /home/webserver/mysql/log/mariadb.log
cd /home/webserver/mysql/log
chown -R mysql:mysql mariadb.log

4.8启动mysql,修改密码

service mysql start

cd /home/webserver/mysql/bin

./mysql -uroot -p

#修改密码
alter user 'root'@'localhost' identified with mysql_native_password by '密码';
flush privileges;

查询用户的密码插件信息

use mysql
select plugin,authentication_string,host,user from user;

允许远程访问

my.cnf添加下面参数重启数据库
default_authentication_plugin=mysql_native_password
创建用户
create user 'root'@'%' identified by 'mysql的密码';
grant all on *.* to 'root'@'%';
flush privileges;

4.9配置环境变量

vim /etc/profile

export PATH=$PATH:/home/webserver/php/bin:/home/webserver/php/sbin:/home/webserver/mysql/bin

source /etc/profile


五、安装redis

axel -n 10 http://download.redis.io/releases/redis-5.0.3.tar.gz
tar -zxvf redis-5.0.3.tar.gz
cd redis-5.0.3


#安装
make PREFIX=/home/webserver/redis/ install


#配置
mkdir /home/webserver/redis/data
mkdir /home/webserver/redis/log

cp ./redis.conf /home/webserver/redis/
vim /home/webserver/redis/redis.conf


#修改配置
# IP绑定
bind 127.0.0.1 192.168.0.111
# 保护模式(开启条件为各redis之间可以互相通信,做集群不可开启)
protected-mode yes
# 访问端口
port 6379
# 连接超时,单位S,0为不启用超时
timeout 0
# 以守护进程运行
daemonize yes
# 数据文件路径
dir /alidata/server/redis/data
# 进程ID文件的路径
pidfile /alidata/server/redis/log/redis.pid
# 日志文件路径
logfile /alidata/server/redis/log/redis.log
# 设置登陆密码
requirepass [redis的密码]
# 禁用部分危险命令
rename-command FLUSHALL ""
rename-command CONFIG   ""
rename-command EVAL     ""
# 开启键过期删除通知
notify-keyspace-events Ex

性能优化

# 编辑/etc/rc.local
vim /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled

# 添加/etc/rc.local执行权限
chmod +x /etc/rc.d/rc.local

# 编辑/etc/sysctl.conf
vim /etc/sysctl.conf
vm.overcommit_memory = 1
net.core.somaxconn = 1024

# 立即解决
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 1024 > /proc/sys/net/core/somaxconn
sysctl vm.overcommit_memory=1
sysctl -p

设置用户

useradd -s /sbin/nologin -M redis
chown -R redis:redis /home/webserver/redis

启动redis并设置开机启动

# 进入单元文件目录
cd /etc/systemd/system

# 创建redis单元文件,格式为: [单元文件名].[单元文件类型]
vim redis.service

[Unit]
Description=Start redis on boot.
After=default.target network.target

[Service]
User=redis
Group=redis
Type=forking
PIDFile=/home/webserver/redis/log/redis.pid
ExecStart=/home/webserver/redis/bin/redis-server /home/webserver/redis/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
#Restart=always

[Install]
WantedBy=multi-user.target

# 修改文件权限为只有root用户可以编辑该文件
chown -R root:root /etc/systemd/system/redis.service
chmod -R 644 /etc/systemd/system/redis.service

# 更新systemd
systemctl daemon-reload
systemctl enable redis
systemctl start redis

redis加入系统服务

cp /home/redis/redis-5.0.3/utils/redis_init_script /etc/init.d/redis

# 编辑/etc/init.d/redis 直接用systemctl控制开启和关闭redis
# start 部分修改
    #$EXEC $CONF
    systemctl start redis

# stop 部分修改
    #PID=$(cat $PIDFILE)
    echo "Stopping ..."
    #$CLIEXEC -p $REDISPORT shutdown
    #while [ -x /proc/${PID} ]
    #do
       # echo "Waiting for Redis to shutdown ..."
       # sleep 1
    #done
    systemctl stop redis

配置环境变量

vim /etc/profile
PATH=/home/webserver/redis/bin:$PATH
# 使配置生效
source /etc/profile


打赏

看恩吧
网站不承担任何有关评论的责任
  • 最新评论
  • 总共条评论
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦