CentOS7安装LNMP环境

0x01 Nginx

由于我们是通过yum直接安装Nginx,所以要先确保我们安装了YUM的EPEL软件源。

大部分镜像会自带EPEL软件源,但是部分国外VPS的EPEL源需要重新安装才可以正常使用。

1
yum install -y epel-release

如果提示 nothing to do,那需要先删除自带的EPEL软件源,再重新安装。

1
yum remove -y epel-release

安装好EPEL源之后,我们便可以通过如下命令安装Nginx。

1
yum install -y nginx

安装好Nginx之后,我们需要启动它并将其添加到系统自启动项中。

1
2
systemctl start nginx
systemctl enable nginx

此时我们访问服务器的IP地址,应该就可以见到Nginx的欢迎页面了,如果无法联通,可能是由于没有开启80端口导致。我们使用如下命令解决。

1
firewall-cmd --zone=public --add-port=80/tcp --permanent

0x02 MariaDB

添加MariaDB仓

编辑MariaDB仓文件。

1
vim /etc/yum.repos.d/MariaDB.repo

输入如下内容(截止本文更新时,MariaDB最新版本是10.3)。

1
2
3
4
5
6
7
# MariaDB 10.3 CentOS repository list - created 2018-06-26 14:47 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

安装MariaDB

1
yum install MariaDB-server MariaDB-client

完成后启动MariaDB并添加到系统自启动项目中

1
2
systemctl start mariadb
systemctl enable mariadb

使用如下命令进行MariaDB的初始化安装。

1
mysql_secure_installation

根据相关提示信息,即可完成安装。

修改默认编码

使用mysql -uroot -p进入MariaDB之后,键入如下命令修改默认编码为UTF-8。

1
2
3
set character_set_database=utf8;
set character_set_server=utf8;
set character_set_results=utf8;

可以使用show variables like 'character_set_%';命令查看编码。

0x03 PHP

安装PHP

下载最新的php源码(截止本文更新时,php最新版本为7.2.7)并解压。

1
2
3
wget http://cn2.php.net/distributions/php-7.2.7.tar.gz
tar -xzvf php-7.2.7.tar.gz
cd php-7.2.7

有一些php的前置安装包需要安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yum -y install libxml2
yum -y install libxml2-devel
yum -y install openssl openssl-devel
yum -y install curl-devel
yum -y install libjpeg
yum -y install libjpeg-devel
yum -y install libpng
yum -y install libpng-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install libxslt
yum -y install libxslt-devel
yum -y install bzip2
yum -y install bzip2-devel

进行编译配置。

1
./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip

如果在编译中出错,请检查自己是否安装好了gcc。

1
yum install -y gcc

使用命令make编译,然后使用命令make install安装。

如果在编译过程中遇到内存不足的错误,那是由于我们默认开启了fileinfo这个扩展,如果不想放弃这个扩展,请使用如下方式添加1G的swap交换内存。

1
2
3
dd if=/dev/zero of=/home/swap bs=1024 count=1024000
mkswap /home/swap
swapon /home/swap

配置PHP

1
2
3
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 sapi/fpm/php-fpm /usr/local/bin

然后设置php.ini,使用: vim /usr/local/php/lib/php.ini 打开php配置文件找到cgi.fix_pathinfo配置项,这一项默认被注释并且值为1,根据官方文档的说明,这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为0 。

1
2
groupadd www-data
useradd -g www-data www-data
1
2
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
vim /usr/local/php/etc/php-fpm.d/www.conf

设置user和group为www-data。通过如下命令启动php。

1
/usr/local/bin/php-fpm

可以通过netstat -tln | grep 9000来查看php运行状态。

然后使用如下方式将php添加到自启动项中。

1
vim  /lib/systemd/system/php-fpm.service

输入如下内容。

1
2
3
4
5
6
7
8
9
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target

使用systemctl启动服务。

1
systemctl enable php-fpm

0x04 Nginx + php-fpm

参考配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 80;
server_name foo.com;

root /path;
index index.html index.htm index.php;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
try_files $uri =404;

include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}