LNMP环境搭建

说明:该教程基于CentOS7.9版本安装!

准备

升级所有已安装包

# yum update

创建软件下载目录

# mkdir /root/soft
# cd /root/soft

可以把所有下载的安装包都放在这个目录下

内存小于1G的,需要创建Swap分区

# dd if=/dev/zero of=/home/swap bs=1024 count=2048000
# mkswap /home/swap
# swapon /home/swap

Nginx

  1. 安装依赖包
# yum install gcc-c++ make
  1. 下载解压库源码备用

下载解压pcre

# wget https://nchc.dl.sourceforge.net/project/pcre/pcre/8.44/pcre-8.44.tar.gz
# tar zxvf pcre-8.44.tar.gz

下载解压zlib:

# wget http://zlib.net/zlib-1.2.13.tar.gz 
# tar -zxvf zlib-1.2.13.tar.gz

下载解压openssl:

# wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
# tar zxvf openssl-1.1.1g.tar.gz
  1. 添加一个不能登录且没有主目录的用户
# sudo useradd www -M -s /sbin/nologin
  1. 编译Nginx并安装
# wget http://nginx.org/download/nginx-1.17.10.tar.gz
# tar zxvf nginx-1.17.10.tar.gz
# cd nginx-1.17.10
# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-zlib=/root/soft/zlib-1.2.13 --with-openssl=/root/soft/openssl-1.1.1g --with-pcre=/root/soft/pcre-8.44
# sudo make && make install
  1. 添加启动配置文件
# vim /etc/systemd/system/nginx.service

添加如下内容后保存:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

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

常用管理命令:

# systemctl start nginx
# systemctl enable nginx
# systemctl stop nginx
# systemctl try-restart nginx
# systemctl reload nginx

注意:CentOS7开始,不在使用service管理应用程序,意在提高系统启动速度

  1. 修改nginx配置文件
# vim /usr/local/nginx/conf/nginx.conf

PHP

  1. 安装依赖包
# yum install libpng libpng-devel libmcrypt-devel mhash-devel libxslt-devel freetype-devel libxml2-devel zlib-devel glibc glibc-devel glib2-devel bzip2 bzip2-devel e2fsprogs-devel krb5-devel libidn-devel openssl-devel libcurl-devel libjpeg-turbo libjpeg-turbo-devel libzip libzip-devel
  1. 编译php并安装
# wget http://hk2.php.net/distributions/php-7.3.17.tar.gz
# tar zxvf php-7.3.17.tar.gz
# cd php-7.3.17
# ./configure --prefix=/usr/local/php --with-iconv --with-zlib --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-mysqli --with-pdo-mysql --enable-ftp --with-jpeg-dir --with-freetype-dir --with-png-dir --enable-fpm --with-fpm-user=www --with-fpm-group=www
# make && make install

常见报错: [[资料:lnmp环境搭建:php安装常见报错|PHP编译安装常见错误]]

  1. 添加启动配置文件
# cp sapi/fpm/php-fpm.service /etc/systemd/system/

常用管理命令参考Nginx的管理命令

  1. 添加php配置文件
# cp php.ini-development /usr/local/php/lib/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /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
  1. 安装yaf扩展
# wget https://github.com/laruence/yaf/archive/yaf-3.0.7.tar.gz
# tar -zxvf yaf-3.0.7.tar.gz
# cd yaf-yaf-3.0.7/
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make && make install

php.ini中添加

extension=yaf
  1. 安装phpredis扩展
# wget https://github.com/phpredis/phpredis/archive/4.1.0.tar.gz
# tar -zxvf 4.1.0.tar.gz
# cd phpredis-4.1.0
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config
# make && make install

php.ini中添加

extension=redis
  1. 安装redis并启动
# yum install redis
# systemctl start redis
# systemctl enable redis
  1. 安装intl扩展
# 安装ICU库
wget https://github.com/unicode-org/icu/releases/download/release-59-2/icu4c-59_2-src.tgz
tar -zxvf icu4c-59_2-src.tgz
cd icu/source
./configure --prefix=/usr/local/icu
make & make install

# 安装intl扩展
cd php7源码目录/ext/intl/
/usr/local/php/bin/phpize
./configure --enable-intl --with-icu-dir=/usr/local/icu --with-php-config=/usr/local/php/bin/php-config
make & make install

Mysql(yum源安装)

  1. 下载安装源
# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
# 更多版本下载: https://dev.mysql.com/downloads/repo/yum/
# yum install mysql80-community-release-el7-3.noarch.rpm
  1. 安装Mysql服务
# yum install mysql-community-server
  1. 检查systemd配置
# systemctl list-unit-files|grep mysqld
  1. 修改配置文件
  2. 初始化数据库
# mysqld --initialize
  1. 启动数据库
# systemctl start mysqld
  1. 登录数据库
    在mysqld.log文件中找到初始密码,登录数据库
# alter user 'root'@'localhost' identified by '12345678';

Mysql(编译安装)

  1. 安装依赖包
# yum -y install bison ncurses ncurses-devel cmake
  1. 下载解压库源码备用
# wget https://dl.bintray.com/boostorg/release/1.68.0/source/boost_1_67_0.tar.gz
# tar -zxvf boost_1_67_0.tar.gz
  1. 创建数据目录和mysql用户
# mkdir -p /data/mysql
# useradd -r -M -s /sbin/nologin mysql
# chown mysql:mysql -R /data/mysql
  1. 编译mysql并安装
# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.12.tar.gz
# tar -zxvf mysql-8.0.12.tar.gz
# cd mysql-8.0.12
# mkdir build (建议新建目录进行编译)
# cd build
# cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=0 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/root/soft/boost_1_67_0
# make && make install
  1. 初始化数据库
# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
  1. 添加、修改启动配置文件
# cp ../scripts/systemd/mysqld.service.in /etc/systemd/system/mysqld.service
# 修改里边带@符号的地方
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld $MYSQLD_OPTS
  1. 修改配置文件
# vim /etc/my.cnf


[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/logs/mysql/mariadb.log
pid-file=/data/mysql/mysqld.pid
  1. 启动mysqld,设置初始密码
# systemctl start mysqld
# ./bin/mysql_secure_installation

收尾

删除Swap分区

# /sbin/swapoff /home/swap
# rm -rf /home/swap