欢迎光临
我们一直在努力

[Centos]安装LNMP服务器

1、安装支持扩展(根据自己需求决定安装哪些扩展)

1.1、安装Autoconf

#下载地址:http://mirrors.kernel.org/gnu/autoconf/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf autoconf-2.69.tar.gz
cd autoconf-2.69/
./configure --prefix=/usr/local/autoconf-2.69
make && make install
ln -s /usr/local/autoconf-2.69/ /usr/local/autoconf
rm -rf autoconf-2.69/

1.2、安装libiconv(PHP必须)

#下载地址:http://www.gnu.org/software/libiconv/#downloading
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf libiconv-1.15.tar.gz
cd libiconv-1.15
./configure --prefix=/usr/local/libiconv-1.15 --enable-static
make && make install
ln -sf /usr/local/libiconv-1.15/ /usr/local/libiconv
ln -sf /usr/local/libiconv/lib/libiconv.so.2 /usr/lib/libiconv.so.2
ln -sf /usr/local/libiconv/lib/libiconv.so /usr/lib/libiconv.so
ldconfig
rm -rf libiconv-1.15

1.3、安装libmcrypt

#下载地址:https://sourceforge.net/projects/mcrypt/files/Libmcrypt/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make && make install
ldconfig
cd libltdl/
./configure --enable-ltdl-install
make && make install
ln -sf /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -sf /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ldconfig
rm -rf libmcrypt-2.5.8

1.4、安装mhash

#下载地址:http://mhash.sf.net/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf mhash-0.8.15.tar.gz
cd mhash-0.8.15/
./configure
make && make install
ln -sf /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -sf /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -sf /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -sf /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ldconfig
rm -rf mhash-0.8.15

1.5、安装mcrypt

#下载地址:https://sourceforge.net/projects/mcrypt/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure --prefix=/usr/local/mcrypt-2.6.8
make && make install
ln -sf /usr/local/mcrypt-2.6.8/ /usr/local/mcrypt
rm -rf mcrypt-2.6.8 

1.6、安装freetype

#下载地址:https://download.savannah.gnu.org/releases/freetype/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf freetype-2.9.1.tar.gz
cd freetype-2.9.1/
./configure --prefix=/usr/local/freetype-2.9.1
make && make install
ln -sf /usr/local/freetype-2.9.1/ /usr/local/freetype
#cp /usr/local/freetype/lib/pkgconfig/freetype2.pc /usr/lib/pkgconfig/ 这句未执行
cat > /etc/ld.so.conf.d/freetype.conf<<EOF
/usr/local/freetype/lib
EOF
ldconfig
ln -sf /usr/local/freetype/include/freetype2/* /usr/include/

1.7、安装pcre

#下载地址:https://www.pcre.org/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf pcre-8.42.tar.gz
cd pcre-8.42/
./configure --prefix=/usr/local/pcre-8.42/
make && make install
ln -sf /usr/local/pcre-8.42/ /usr/local/pcre
rm -rf pcre-8.42

1.8、内存管理优化工具(可以不装,也可以二选一)

1.8.1、安装Jemalloc

#下载地址:https://github.com/jemalloc/jemalloc/releases
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar jxvf jemalloc-5.1.0.tar.bz2
cd jemalloc-5.1.0/
./configure
make && make install
ldconfig
ln -sf /usr/local/lib/libjemalloc* /usr/lib/
rm -rf jemalloc-5.1.0

1.8.2、安装TCMalloc(TCMalloc是gperftools的一个插件,所以直接安装gperftools即可)

因为gperftools需要libunwind,我们先来安装libunwind。

1.8.2.1、安装libunwind

#下载地址:http://download.savannah.gnu.org/releases/libunwind/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf libunwind-1.2.tar.gz
cd libunwind-1.2/
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
rm -rf libunwind-1.2

1.8.2.2、安装gperftools

#下载地址:https://github.com/gperftools/gperftools.git
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
#使用Git库安装
git clone https://github.com/gperftools/gperftools.git
#或用工具包内文件安装
tar zxvf gperftools-2.7.tar.gz
cd gperftools-2.7/
#判断系统是32还是64位
getconf LONG_BIT
./autogen.sh
#如果64位执行
./configure
#如果是32位执行
./configure --enable-frame-pointers
make
make install
ldconfig
ln -sf /usr/local/lib/libtcmalloc* /usr/lib/

=================Nginx配置====================
mkdir /tmp/tcmalloc
chown -R www:www /tmp/tcmalloc
sed -i '/nginx.pid/a\
google_perftools_profiles /tmp/tcmalloc;' /usr/local/nginx/conf/nginx.conf

1.9、安装openssl

#下载地址:https://www.openssl.org/source/snapshot/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf openssl-1.1.0-stable-SNAP-20180901.tar.gz
./config -fPIC --prefix=/usr/local/openssl-1.1.0 --openssldir=/usr/local/openssl-1.1.0
perl configdata.pm --dump
make depend && make && make install
ln -sf /usr/local/openssl-1.1.0/ /usr/local/openssl
ln -sf /usr/local/openssl/lib/libssl.so.1.1 /usr/lib/libssl.so.1.1
ln -sf /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -sf /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib/libcrypto.so.1.1
ln -sf /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
rm -rf openssl-1.1.0-stable-SNAP-20180901

1.10、安装Crul

#下载地址:https://curl.haxx.se/download.html
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf curl-7.61.0.tar.gz
cd curl-7.61.0/
./configure --prefix=/usr/local/curl-7.61.0 --enable-ares --without-nss --with-ssl=/usr/local/openssl
make && make install
ln -sf /usr/local/curl-7.65.1/ /usr/local/curl
ln -sf /usr/local/curl/bin/* /usr/bin/
rm -rf curl-7.61.0

备注:安装Crul的时候有可能提示:configure: error: c-ares library defective or too old

这里升级安装c-ares

#下载地址:https://c-ares.haxx.se/download/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar zxvf c-ares-1.14.0.tar.gz
cd c-ares-1.14.0/
ll
./configure --prefix=/usr/local/c-ares-1.14.0
make
make install
ln -sf /usr/local/c-ares-1.14.0/ /usr/local/c-ares
rm -rf c-ares-1.14.0
#安装的时候需要    --enable-ares=/usr/local/c-ares/

1.11、安装Nghttp2

#下载地址:https://github.com/nghttp2/nghttp2/releases
#工具包 https://gitee.com/bbhsky/Centos7-setup.git
tar Jxvf nghttp2-1.33.0.tar.xz
cd nghttp2-1.33.0/
./configure --prefix=/usr/local/nghttp2-1.33.0
make && make install
ln -sf /usr/local/nghttp2-1.33.0/ /usr/local/nghttp2
rm -rf nghttp2-1.33.0

1.12、安装boots(安装MySQL5.7和MySQL8.0需要)

#下载地址:https://www.boost.org/users/download/
#工具包 https://gitee.com/bbhsky/Centos7-setup.git

#解压即可
tar zxvf boost_1_68_0.tar.gz
#MySQL5.7如果报错请测试低版本,比如1.59
tar zxvf boost_1_59_0.tar.gz

2、系统环境设置

2.1、设置虚拟内存

#其他建议设置成无限制(unlimited)的一些重要设置是: 
#数据段长度:ulimit -d unlimited 
#最大内存大小:ulimit -m unlimited 
#堆栈大小:ulimit -s unlimited 
#CPU 时间:ulimit -t unlimited 
虚拟内存:ulimit -v unlimited #建议设置

2.2、设置libpng和libjpeg

#64位
ln -sf /usr/lib64/libpng.* /usr/lib/
ln -sf /usr/lib64/libjpeg.* /usr/lib/

2.3、加载新的lib库

echo "/lib" >> /etc/ld.so.conf
echo "/usr/lib" >> /etc/ld.so.conf
echo "/usr/lib64" >> /etc/ld.so.conf
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig

2.4、配置limits.conf

cat >>/etc/security/limits.conf<<eof
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
eof

2.5、配置sysctl.conf

#参数表示进程可以同时打开的最大句柄数,这个参数直接限制最大并发连接数。
echo "fs.file-max=65535" >> /etc/sysctl.conf

3、mysql安装

3.1、Docker安装(推荐)

http://www.798vps.com/304.html

3.2、编译安装5.7

#下载地址:https://dev.mysql.com/downloads/mysql/
#工具包 https://gitee.com/bbhsky/MySQL-Setup.git
tar zxvf mysql-5.7.23.tar.gz
tar zxvf boost_1_68_0.tar.gz
cd mysql-5.7.23/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57 -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_BOOST=/root/src/MySQL-Setup/boost_1_59_0/
make && make install
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql
ln -sf /usr/local/mysql57/ /usr/local/mysql
cp conf/mysql57-2048.cnf /etc/my.cnf #复制优化过的内存的配置文件
#或生成配置文件
cat > /etc/my.cnf<<EOF
[client]
#password   = your_password
port        = 3306
socket      = /tmp/mysql.sock
[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
datadir = /usr/local/mysql/var
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread_cache_size = 8
query_cache_size = 8M
tmp_table_size = 16M
performance_schema_max_table_instances = 500
explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id   = 1
expire_logs_days = 10
early-plugin-load = ""
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /usr/local/mysql/var
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /usr/local/mysql/var
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
${MySQLMAOpt}
EOF

#如果需要开启Innodb
sed -i 's/^#innodb/innodb/g' /etc/my.cnf
#如果不开启Innodb
sed -i '/^default_storage_engine/d' /etc/my.cnf
sed -i 's#default_storage_engine.*#default_storage_engine = MyISAM#' /etc/my.cnf

mkdir -p /home/mysql/var
ln -sf /home/mysql/var /usr/local/mysql/var
chown -R mysql:mysql /usr/local/mysql/var
chown -R mysql:mysql /home/mysql/var
/usr/local/mysql/bin/mysqld --initialize-insecure --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql
chgrp -R mysql /usr/local/mysql/.
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib
/usr/local/lib
EOF
ldconfig
ln -sf /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -sf /usr/local/mysql/include/mysql /usr/include/mysql
systemctl enable mysql.service
/etc/init.d/mysql start
ln -sf /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -sf /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -sf /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
ln -sf /usr/local/mysql/bin/mysqld_safe /usr/bin/mysqld_safe
ln -sf /usr/local/mysql/bin/mysqlcheck /usr/bin/mysqlcheck
/etc/init.d/mysql restart
/usr/local/mysql/bin/mysqladmin -u root password "password"
#优化数据库
mysql -uroot -p
mysql> DELETE FROM mysql.user WHERE User='';
mysql> DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
mysql> DROP DATABASE test;
mysql> FLUSH PRIVILEGES;
/etc/init.d/mysql restart

4、编译安装php

4.1、安装编译7.2

#下载地址:http://php.net/
#工具包 git@gitee.com:bbhsky/PHP-Setup.git
tar jxvf php-7.2.9.tar.bz2
cd php-7.2.9/
./configure --prefix=/usr/local/php-7.2.9 --with-config-file-path=/usr/local/php-7.2.9/etc --with-config-file-scan-dir=/usr/local/php-7.2.9/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/libiconv --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --enable-ftp --with-gd --with-openssl=/usr/local/openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-xsl --with-libzip
make ZEND_EXTRA_LIBS='-liconv' -j `grep 'processor' /proc/cpuinfo | wc -l`
make install
ln -sf /usr/local/php-7.2.9/ /usr/local/php
ln -sf /usr/local/php/bin/php /usr/bin/php
ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
ln -sf /usr/local/php/bin/pear /usr/bin/pear
ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm
mkdir -p /usr/local/php/conf.d
mkdir -p /usr/local/php/etc
cp php.ini-production /usr/local/php/etc/php.ini
sed -i 's/post_max_size =.*/post_max_size = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /usr/local/php/etc/php.ini
sed -i 's/short_open_tag =.*/short_open_tag = On/g' /usr/local/php/etc/php.ini
sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
sed -i 's/max_execution_time =.*/max_execution_time = 300/g' /usr/local/php/etc/php.ini
sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini
pear config-set php_ini /usr/local/php/etc/php.ini
pecl config-set php_ini /usr/local/php/etc/php.ini
curl -sS --connect-timeout 30 -m 60 https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
#写入配置文件
cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice
[www]
listen = /tmp/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF
#或者复制配置文件
cp conf/php-fpm-72.conf /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
chmod +x /etc/init.d/php-fpm

4.2、安装编译7.3

5、编译安装Nginx

6、设置自启动

systemctl enable mysql.service
systemctl enable php-fpm.service
systemctl enable nginx.service

7、安装phpmyadmin

#下载地址:https://www.phpmyadmin.net
#工具包 https://gitee.com/bbhsky/PHP-Setup.git
unzip phpMyAdmin-4.8.3-all-languages.zip
mv phpMyAdmin-4.8.3-all-languages /home/wwwroot/default/phpmyadmin
cp conf/config.inc.php /home/wwwroot/default/phpmyadmin/config.inc.php
sed -i 's/LNMPORG/FoolTiger/g' /home/wwwroot/default/phpmyadmin/config.inc.php
mkdir /home/wwwroot/default/phpmyadmin/upload
mkdir /home/wwwroot/default/phpmyadmin/save
chmod 755 -R /home/wwwroot/default/phpmyadmin/
chown www:www -R /home/wwwroot/default/phpmyadmin/

8、安装Memcached

#下载地址:http://memcached.org/download
#工具包 https://gitee.com/bbhsky/PHP-Setup.git
cd memcached-1.5.10/
./configure --prefix=/usr/local/memcached-1.5.10
make
make install
ln -sf /usr/local/memcached-1.5.10/ /usr/local/memcached
ln -sf /usr/local/memcached/bin/memcached /usr/bin/memcached
cp init.d/memcached /etc/init.d/memcached
chmod +x /etc/init.d/memcached
useradd -s /sbin/nologin nobody
#下载地址:https://launchpad.net/libmemcached/+download
#工具包 https://gitee.com/bbhsky/PHP-Setup.git
tar zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18/
./configure --prefix=/usr/local/libmemcached-1.0.18 --with-memcached
make && make install
ln -sf /usr/local/libmemcached-1.0.18/ /usr/local/libmemcached
赞(0)
未经允许不得转载:798VPS » [Centos]安装LNMP服务器
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址