Centos搭建LNMP环境

1、先做准备工作:

①、安装LNMP环境所需的基础组件:
yum -y install yum-fastestmirror
yum -y install glibc-static libstdc++-static glibc-devel GeoIP GeoIP-devel sqlite-devel
yum -y install patch make flex bison tar pcre pcre-devel screen vsftpd ftp mailx jwhois postfix cronie
yum -y install libtool libtool-libs kernel-devel autoconf
yum -y install libjpeg libjpeg-devel libpng libpng-devel
yum -y install libtiff libtiff-devel gettext gettext-devel
yum -y install freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel
yum -y install file glib2 glib2-devel bzip2 openldap-devel
yum -y install bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs
yum -y install e2fsprogs-devel krb5-devel libidn libidn-devel
yum -y install openssl openssl-devel vim-minimal unzip
yum -y install libevent mcrypt mash libssh2 libssh2-devel libmetalink libmetalink-devel nghttp2
yum -y install gd recode recode-devel libatomic_ops-devel
yum -y install libXpm libXpm-devel gmp gmp-devel gd-devel
yum -y update
export PHP_AUTOCONF=/usr/bin/autoconf
export PHP_AUTOHEADER=/usr/bin/autoheader
②、CentOS设置防火墙由iptables改为firewallfirewall使用方法:
#安装firewalld
yum -y install firewalld
#开启firewalld
systemctl start firewalld
#开机启动firewalld
systemctl enable firewalld
#关闭firewalld
systemctl stop firewalld
#取消开机启动firewalld
systemctl disable firewalld
#为public域开放tcp协议的8080端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
#为public域添加http服务
firewall-cmd --zone=public --add-service=http --permanent
#重启firewall服务
firewall-cmd --reload

2、安装MySQL:

PS.官方有详细的安装说明,详情请戳:MySQL安装方法详解

①、添加MySQL Yum Repository:

前往MySQL Yum Repository下载页,页面地址:MySQL Yum Repository下载页,根据服务器版本信息及所需的MySQL版本信息,下载对应的MySQL Yum Repository,本服务器是centos7 64位操作系统,想要使用MySQL5.7,可用就是:http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm,命令如下:

cd ~ && wget http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm
sudo yum -y localinstall mysql57-community-release-el7-9.noarch.rpm
②、安装MySQL:
sudo yum -y install mysql-devel mysql-community-server
③、启动MySQL:
sudo service mysqld start
④、修改root密码,创建用户:

PS.MySQL5.7在启动时会创建一个随机密码,查看设定的root密码:

sudo grep 'temporary password' /var/log/mysqld.log

找到显示结果中的密码提示:

 [Note] A temporary password is generated for root@localhost: xxxxx

之后的就是系统分配的随机密码,根据随机密码登录MySQL:

mysql -uroot -pxxxxx

登录MySQL之后使用命令行修改root密码:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

设置密码的时候一定要设置一个强密码,如果不是强密码,会有如下提示:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

关于密码强度判断及强密码的规则,官方有以下说明:

Note
MySQL's validate_password plugin is installed by default. This will require that passwords contain at least one upper case letter, one lower case letter, one digit, and one special character, and that the total password length is at least 8 characters.

为了方便使用,我们经常会创建任意连接的root用户:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;

刷新权限使命令生效:

flush privileges;

退出MySQL:

exit

接下来修改系统配置,

⑤、如果系统的防火墙开着(or behind a hardware firewall or NAT)你必须放开mysql使用的TCP端口,默认是3306;
#为public域开放tcp协议的3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
#为public域添加mysql服务
firewall-cmd --zone=public --add-service=mysql --permanent
#重启firewall服务
firewall-cmd --reload
⑥、修改设定,去掉MySQL中大小写敏感:

用root帐号登录后,在/etc/my.cnf中设置不区分大小写:

[mysqld]
lower_case_table_names=1

PS.lower_case_table_names参数详解: 0:区分大小写,1:不区分大小写

⑦、更新MySQL:
yum -y update mysql-community-server
⑧、开机启动MySQL:
sudo chkconfig mysqld on

3、安装php7:

①、在/usr/local中创建文件夹php7:
mkdir -p /usr/local/php7
mkdir ~/php7 && cd ~/php7
②、下载并解压php7安装包:

https://secure.php.net选择php7版本,目前发行版是7.1.0,找到php7.1.0的下载包地址进行下载:
国内建议使用http://cn2.php.net/distributions/php-7.1.0.tar.gz
国外建议使用https://secure.php.net/distributions/php-7.1.0.tar.gz

wget https://secure.php.net/distributions/php-7.1.0.tar.gz
tar -zxvf php-7.1.0.tar.gz
cd php-7.1.0/
③、配置编译php7:

Ⅰ、在使用服务器时我会开启并使用SSL,所以编译安装php时会在环境中加入SSL模块,因为openssl1.0.1版本会有心脏滴血漏洞,所以这里需要更换OpenSSL版本

①、下载openssl1.0.2版本:
cd ~ && wget https://www.openssl.org/source/openssl-1.0.2-latest.tar.gz
②、解压安装包:
tar -zxvf openssl-1.0.2-latest.tar.gz
③、编译安装openssl-1.0.2:
cd  openssl-1.0.2j
./config shared zlib
make depend
make && make install
④、修改历史的OpenSSL文件设置备份:
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/include/openssl /usr/include/openssl.old
⑤、设置软连接使其使用新的OpenSSL版本 刚刚安装的OpenSSL默认安装在/usr/local/ssl下:
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/ssl/include/openssl /usr/include/openssl
⑥、更新动态链接库数据:
echo "/usr/local/ssl/lib/" >> /etc/ld.so.conf
export LD_LIBRARY_PATH=/usr/local/ssl/lib/
ldconfig -v

查看openssl版本openssl version

OpenSSL 1.0.2j  26 Sep 2016

执行命令查看openssl依赖库版本是否为1.0.2j:

strings /usr/local/ssl/lib/libssl.so |grep OpenSSL

Ⅱ、在编译配置php时需要安装mcrypt,由于版权原因CentOS源没有mcrypt,所以需要手动安装,安装方法有两个
①、使用第三方源,这种方法的好处是方便易于管理,坏处就是不安全:

cd ~ && wget http://www.atomicorp.com/installers/atomic
sh ./atomic

在安装时会显示协议说明,需要输入yes通过,然后直接回车安装源,安装成功之后安装mcrypt:

yum -y install libmcrypt  libmcrypt-devel

直接安装得到的版本是2.5.8
②、下载源码编译安装(推荐):

cd ~ && wget http://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/usr/local
make && make install

PS. 使用这两种方法安装的区别在于编译php时的--with-mcrypt路径问题

  • 使用yum源安装,路径就是--with-mcrypt=/usr/include
  • 使用源码编译安装,路径就是--with-mcrypt=/usr/local

Ⅲ、修改curl的安装,使其支持openssl
PS.如果不支持openssl,CURL无法访问https,在php中同样也无法使用CURL访问https:
查看当前CURL版本:

curl -V

可以看到结果如下:

curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.19.1 Basic ECC zlib/1.2.7 libidn/1.28 libssh2/1.4.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz

很明显可以看到是NSS模式,不支持openssl,接下来重新安装curl:

①、下载、解压缩CURL安装包:

去官网查看curl最新版本已经是7.51.0,并且支持http2,所以建议重新编译CURL并且使之支持http2,为了让 curl 支持 HTTP2 我们需要安装 nghttp2(http2 的 C 语言库),nghttp2的地址:nghttp2,git仓库:nghttp2的git仓库,使用source方式安装nghttp2:

cd ~ && git clone https://github.com/nghttp2/nghttp2.git
cd nghttp2
autoreconf -i
automake
autoconf
./configure
make && make install
②、安装CURL7.51.0:
cd ~ && wget https://curl.haxx.se/download/curl-7.51.0.tar.gz
tar -zxvf curl-7.51.0.tar.gz
cd curl-7.51.0/
./configure --prefix=/usr/local/curl --without-nss --with-ssl=/usr/local/ssl --with-nghttp2=/usr/local --with-gssapi --with-libmetalink --with-libssh2 --enable-tls-srp --enable-sspi
make && make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig
mv /usr/bin/curl /usr/bin/curl.old
ln -s /usr/local/curl/bin/curl /usr/bin/

现在查看CURL版本curl -V,显示结果如下:

curl 7.51.0 (x86_64-pc-linux-gnu) libcurl/7.51.0 OpenSSL/1.0.2j zlib/1.2.7 libssh2/1.4.3 nghttp2/1.18.0-DEV
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets Metalink

IV:开始编译安装php:

cd ~/php7/php-7.1.0

PS.内存1G以下请在结尾加上:–disable-fileinfo,

./configure --prefix=/usr/local/php7 --exec-prefix=/usr/local/php7 --bindir=/usr/local/php7/bin --sbindir=/usr/local/php7/sbin --includedir=/usr/local/php7/include --libdir=/usr/local/php7/lib/php --mandir=/usr/local/php7/php/man --with-config-file-path=/usr/local/php7/etc --enable-rpath --enable-bcmath --enable-calendar --enable-debug --enable-exif --enable-fileinfo --enable-filter --enable-fpm --enable-ftp --enable-gd-jis-conv --enable-gd-native-ttf --enable-hash --enable-inline-optimization --enable-json --enable-libxml --enable-maintainer-zts --enable-mbregex --enable-mbstring --enable-mysqlnd --enable-opcache --enable-opcache-file --enable-pcntl --enable-pdo --enable-session --enable-shared --enable-shmop --enable-simplexml --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --enable-xml --enable-zip --enable-ctype --with-bz2 --with-curl=/usr/local/curl --with-fpm-user=www --with-fpm-group=www --with-freetype-dir=/usr/include/freetype2/freetype --with-gd --with-gettext --with-gmp --with-iconv --with-iconv-dir=/usr --with-jpeg-dir=/usr --with-mcrypt=/usr/local --with-mhash --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli=mysqlnd --with-openssl=/usr/local/ssl --with-pdo-mysql=mysqlnd --with-pear --with-png-dir=/usr --with-xmlrpc --with-zlib --with-libxml-dir=/usr 

安装:

make && make install
make test

安装完成之后将php的程序引用至/usr/bin中:

ln -s /usr/local/php7/bin/* /usr/bin/
ln -s /usr/local/php7/sbin/* /usr/sbin/

查看安装的php的版本:

php -v

显示结果如下:

PHP 7.1.0 (cli) (built: Dec  2 2016 19:07:57) ( ZTS DEBUG )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies

现在php7已经安装完成,开始设置PHP7的配置文件php.ini,设定php.ini:

cp php.ini-production /usr/local/php7/etc/php.ini
ln -s /usr/local/php7/etc/php.ini /etc/php.ini
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
ln -s /usr/local/php7/etc/php-fpm.conf /etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

Ⅴ、将php编译生成的bin目录添加到当前Linux系统的环境变量中:

echo -e 'export PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH\n' >> /etc/profile && source /etc/profile

Ⅵ、更改配置,使php7支持opcache:
首先更改php.ini,修改extension_dir

extension_dir = "/usr/local/php7/lib/php/extensions/debug-zts-20160303/"

修改完成之后找到[opcache],在下面添加:

zend_extension=opcache.so

然后修改opcache的配置:

opcache.enable=1
opcache.enable_cli=1
opcache.file_cache="/tmp/opcache"

保存配置之后需要在/tmp文件夹下创建opcache文件夹,作为opcache的缓存目录:

mkdir -p /tmp/opcache

这时,再查看php的信息,就会显示opcache了:

php -v

显示结果如下:

PHP 7.1.0 (cli) (built: Dec  2 2016 19:07:57) ( ZTS DEBUG )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.1.0, Copyright (c) 1999-2016, by Zend Technologies

Ⅶ、修改fpm配置:

vi /etc/php-fpm.conf

修改配置文件如下:

pid = run/php-fpm.pid
error_log = log/php-fpm.log

Ⅷ、添加www的组和用户:
添加www的组和用户:

groupadd www
useradd -g www -s /sbin/nologin -M www

Ⅸ、启动/关闭/重启 php-fpm
启动php-fpm:

php-fpm -D

停止php-fpm的命令如下:

kill -INT `cat /usr/local/php7/var/run/php-fpm.pid`

重启php-fpm的命令:

kill -USR2 `cat /usr/local/php7/var/run/php-fpm.pid`

Ⅹ、设置php-fpm开机启动:

echo -e 'php-fpm -D\n' >> /etc/rc.local

PS.为避免rc.local中设定的程序无法自动启动,执行如下命令:

sudo chmod +x /etc/rc.d/rc.local

4、安装Nginx:

①、先做准备工作:

在编译nginx的时候需要pcre、openssl、zlib的源码,所以首先要下载这三个扩展对应的源码包,openssl在安装php时已经下载了,就不需要重新下载了,下载之前首先查看对应版本:

yum info pcre zlib

得到的结果是pcre的版本是8.32,zlib对应的版本是1.2.7,下载各自的源码:

cd ~ && wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.32/pcre-8.32.tar.gz
cd ~ && wget http://jaist.dl.sourceforge.net/project/libpng/zlib/1.2.7/zlib-1.2.7.tar.gz

解压压缩包,不需要安装:

tar -zxvf pcre-8.32.tar.gz
tar -zxvf zlib-1.2.7.tar.gz
②、安装ngx_pagespeed库 :

ngx_pagespeed是一个 Nginx 的扩展模块,可以加速你的网站,减少页面加载时间,它会自动将一些提升web性能的实践应用到网页和相关的资源(CSS、JS和图片)上,无需你修改内容和流程,

按照Google的说法,ngx_pagespeed模块已经被一些客户用于生产环境之中了,包括CDN提供商MaxCDN,按照它的报告该模块使得“页面平均加载时间降低了1.57秒、跳出率降低了1%并且退出百分比下降了2.5%”。WordPress主机服务商ZippyKid说,在使用NGINX的PageSpeed之后,“页面大小降低了75%并且页面的渲染时间提高了50%”

所以可以尝试为nginx安装ngx_pagespeed模块:
安装教程可参考:ngx_pagespeed安装(需科学上网)

cd ~
NPS_VERSION=1.11.33.4
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip -O release-${NPS_VERSION}-beta.zip
unzip release-${NPS_VERSION}-beta.zip
cd ngx_pagespeed-release-${NPS_VERSION}-beta/
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz 
tar -xzvf ${NPS_VERSION}.tar.gz
③、使用google-perftools提供的TCMalloc工具优化Nginx和MySQL:

TCMalloc (google-perftools) 是用于优化C++写的多线程应用,比glibc 2.3的malloc快。这个模块可以用来让MySQL在高并发下内存占用更加稳定.

TCMalloc是google-perftools的其中一个工具,gperftools四个工具分别是:TCMalloc、heap-checker、heap-profiler和cpu-profiler),用于优化内存分配的效率和速度,帮助在高并发的情况下很好的控制内存的使用。

在mysql 和nginx 性能优化方案中,大多数教程都是使用google-perftools提供的TCMalloc工具,TCMalloc在内存的分配上效率和速度要比malloc高得多。

为避免出现错误提示:configure: error: No frame pointers and no libunwind. The compilation will fail造成安装失败,必须先安装libunwind:

cd ~ && wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
tar zxvf libunwind-1.1.tar.gz
cd libunwind-1.1
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install

安装完成之后开始安装gperftools,按照官方的说明,必然选择最新版本:

cd ~ && wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.5/gperftools-2.5.tar.gz
tar zxvf gperftools-2.5.tar.gz
cd gperftools-2.5/
./configure
make && make install

准备工作完成,现在开始安装nginx,

④、开始安装Nginx:

先下载Nginx,下载地址:选择Nginx版本,在这个页面可以选择当前稳定版(Stable version),如果想选择其他版本,可以到nginx版本库中选择其它版本Nginx

cd ~ && wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2/
NPS_VERSION=1.11.33.4
./configure --prefix=/usr/local/nginx --user=www --group=www --pid-path=/usr/local/nginx/pid/nginx.pid --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_image_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-libatomic --http-client-body-temp-path=/tmp/nginx_client --http-proxy-temp-path=/tmp/nginx_proxy --http-fastcgi-temp-path=/tmp/nginx_fastcgi --with-http_addition_module  --with-mail --with-mail_ssl_module --with-ipv6 --with-pcre=/root/pcre-8.32 --with-zlib=/root/zlib-1.2.7 --with-openssl=/root/openssl-1.0.2j --add-module=/root/ngx_pagespeed-release-${NPS_VERSION}-beta --with-google_perftools_module

PS.需要注意的是,这里的--with-pcre--with-zlib--with-openssl以及ngx_pagespeed模块都是源码路径,而不是编译安装之后的路径。

⑤、编译、安装nginx:
make && make install

如果编译安装的时候提示如下错误:

cms.pod around line 457: Expected text after =item, not a number
cms.pod around line 461: Expected text after =item, not a number
cms.pod around line 465: Expected text after =item, not a number
cms.pod around line 470: Expected text after =item, not a number
cms.pod around line 474: Expected text after =item, not a number
POD document had syntax errors at /usr/bin/pod2man line 69.

请用root用户执行如下命令:

rm -rf /usr/bin/pod2man

然后重新编译安装Nginx,

⑥、修改Nginx的配置文件:
ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx.conf
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
vi /etc/nginx.conf

删除原来的内容,修改配置文件内容如下(我的服务器是2核4G):

user www;
#进程数,即处理请求的进程,初始可设置为CPU总核数
worker_processes  2;
error_log  /data/log/nginx/error.log;
pid pid/nginx.pid;
#最大打开文件数,可设置为系统优化后的ulimit -HSn的结果
worker_rlimit_nofile 4096;
events {
        use epoll;
        #nginx最大连接数=worker连接数*worker进程数
        worker_connections  2048;
}
http {
        #文件扩展名与文件类型映射表
        include       mime.types;
        #默认文件类型
        default_type  application/octet-stream;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  /data/log/nginx/access.log  main;
        #开启高效文件传输模式,实现内核零拷贝
        sendfile        on;
        #激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布,积极的作用是减少网络报文段的数量
        tcp_nopush     on;
        #激活tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高I/O性能
        tcp_nodelay on;
        #连接超时时间,单位是秒
        keepalive_timeout  120;
        #开启gzip压缩功能
        gzip  on;
        include vhost/*.conf;
}

开启 google_perftools调优支持,需要修改nginx.conf:

vi /etc/nginx.conf

pid下面加入以下内容:

google_perftools_profiles /tmp/tcmalloc;

然后在/usr/local/nginx/conf目录 新建 proxy.conf文件:

vi /usr/local/nginx/conf/proxy.conf

然后修改proxy.conf为以下内容:

    proxy_redirect          off;
    #在代理向后端服务器发送的http请求头中加入host字段信息,用于当后端服务器配置有多个虚拟主机时,可以识别代理的是哪个虚拟主机。这是节点服务器多虚拟主机时的关键配置。
    proxy_set_header Host  $host;
    #在代理向后端服务器发送的http请求头中加入X-Forwarded-For字段信息,用于后端服务器程序、日志等接收记录真实用户的IP,而不是代理服务器的IP。
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version      1.1;
    proxy_set_header        Upgrade $http_upgrade;
    proxy_set_header        Connection "upgrade";
    #设定读取客户端请求主体的最大大小。
    client_max_body_size    8m;
    client_body_buffer_size 512k;
    #设定反向代理与后端节点服务器连接的超时时间,即发起握手等候响应的超时时间。
    proxy_connect_timeout 60;
    #设定代理后端服务器的数据回传时间
    proxy_send_timeout 60;
    #设定Nginx从代理的后端服务器获取信息的时间
    proxy_read_timeout 60;
    #设定缓冲区的大小
    proxy_buffer_size 256k;
    #设定缓冲区的数量和大小,nginx从代理的后端服务器获取的响应信息,会放置到缓冲区。
    proxy_buffers 4 256k;
    #设定系统很忙时可以使用的proxy_buffers大小
    proxy_busy_buffers_size 256k;
    #设定proxy缓存临时文件的大小
    proxy_temp_file_write_size 256k;
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
    proxy_max_temp_file_size 10m;
    #Nginx cache
    #client_body_temp_path是指定post上传的$_FILES上传的文件地址,因为如果所有上传的文件都放在一个文件夹下,不仅很容易文件名冲突,并且容易导致一个文件夹特别大,所以有必要创建子目录,这里后面如果有值就代表存在一级,二级,三级子目录,目录名是由数字进行命名的,所以这里的具体的值就是代表目录名的数字位数
    client_body_temp_path   /tmp/nginx_client_tmp 1 2;
    proxy_temp_path     /tmp/nginx_proxy_tmp 1 2;
    fastcgi_temp_path   /tmp/nginx_fastcgi_tmp 1 2;

接下来创建ngx_pagespeed模块的配置文件:

vi /usr/local/nginx/conf/pagespeed.conf

pagespeed.conf中加入以下内容:

pagespeed on;
pagespeed FileCachePath /tmp/ngx_pagespeed_cache;
pagespeed RewriteLevel PassThrough;
pagespeed EnableFilters collapse_whitespace;
pagespeed EnableFilters canonicalize_javascript_libraries;
pagespeed EnableFilters combine_css;
pagespeed EnableFilters combine_javascript;
pagespeed EnableFilters elide_attributes;
pagespeed EnableFilters extend_cache;
pagespeed EnableFilters flatten_css_imports;
pagespeed CssFlattenMaxBytes 5120;
pagespeed EnableFilters lazyload_images;
pagespeed EnableFilters rewrite_javascript;
pagespeed EnableFilters rewrite_images;
pagespeed EnableFilters insert_dns_prefetch;
pagespeed EnableFilters prioritize_critical_css;
location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; }
location /ngx_pagespeed_global_statistics { allow 127.0.0.1; deny all; }
location /ngx_pagespeed_message { allow 127.0.0.1; deny all; }
location /pagespeed_console { allow 127.0.0.1; deny all; }
location ~ ^/pagespeed_admin { allow 127.0.0.1; deny all; }
location ~ ^/pagespeed_global_admin { allow 127.0.0.1; deny all; }
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
⑦、创建Nginx虚拟站点:
mkdir -p /usr/local/nginx/conf/vhost
ln -s /usr/local/nginx/conf/vhost /etc/nginxhost
mkdir -p /data/
ln -s /usr/local/nginx/html /data/www
mkdir -p /data/log/nginx
mkdir -p /tmp/tcmalloc
mkdir -p /tmp/nginx_client
mkdir -p /tmp/nginx_proxy
mkdir -p /tmp/nginx_fastcgi
mkdir -p /tmp/nginx_client_tmp
mkdir -p /tmp/nginx_proxy_tmp
mkdir -p /tmp/nginx_fastcgi_tmp
sudo chown -R www:www /data/
sudo chmod -R 775 /data/
sudo chown -R www:www /tmp/tcmalloc
sudo chmod -R 775 /tmp/tcmalloc
sudo chown -R www:www /tmp/nginx_*
sudo chmod -R 775 /tmp/nginx_*

在Nginx虚拟主机目录/usr/local/nginx/conf/vhost下建立一个虚拟主机:

vi /usr/local/nginx/conf/vhost/default.conf

修改default.conf为以下内容:

server {
        listen       80;
        server_name  host;
        ## Don't show the nginx version number, a security best practice
        server_tokens off;
        #charset koi8-r;
        root   html;
        index  index.php index.html index.htm;
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_intercept_errors    on;
                include /usr/local/nginx/conf/fastcgi.conf;
            }
        include proxy.conf;
        include pagespeed.conf;
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        access_log  /data/log/nginx/host.access.log  main;
        error_log   /data/log/nginx/host.error.log;

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

配置修改完毕。

⑧、启动/关闭Nginx:

启动Nginx:

nginx

如果在启动Nginx的时候有如下错误提示:

nginx: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory

首先查看libprofiler.so.0库是否存在:

whereis libprofiler.so.0

结果如下:

libprofiler.so: /usr/local/lib/libprofiler.so.0 /usr/local/lib/libprofiler.so

因为我的是64位系统,所以创建一个软连接到/usr/lib64/下就可以了:

ln -s /usr/local/lib/libprofiler.so.0 /usr/lib64/

如果遇到类似这种别的错误提示,按照以上办法解决即可。

Nginx重载配置文件:

nginx -s reload

Nginx重启:

nginx -s reopen

验证tcmalloc运行状态,这是仅开启了一个worker_processes的效果:

lsof -n|grep tcmalloc

设置Nginx开机启动:

echo -e 'nginx\n' >> /etc/rc.local

以上就是centos7下搭建LNMP的全过程,在一般使用过程中,都会配置Nginx的伪静态,修改default.conf

if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php$1 last;
}
location ~ .*\.php(\/.*)*$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_intercept_errors    on;
        include /usr/local/nginx/conf/fastcgi.conf;
}

添加对静态文件的缓存:

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
    expires       1d;
}
location ~ .*\.(js|css|html)?$
{
    expires       1d;
}

按照以上过程,开发环境LNMP就搭建完成,为了能够可以外网访问,需要开启防火墙:

firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload

CentOS7上搭建LNMP环境完成,此时就可以使用配置文件中的host来访问站点了。

  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
CentOS 7.6 LNMP搭建WordPress 1. 安装LNMP环境 LNMP环境包括Linux、Nginx、MySQLPHP。在CentOS 7.6上安装LNMP环境,可以使用yum命令: sudo yum install nginx mysql-server php php-mysql 2. 配置Nginx Nginx是一个高性能的Web服务器,可以用来代替Apache。在CentOS 7.6上配置Nginx,需要编辑Nginx配置文件: sudo vi /etc/nginx/nginx.conf 在http块中添加以下内容: server { listen 80; server_name yourdomain.com; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 保存并退出文件。然后重新启动Nginx: sudo systemctl restart nginx 3. 配置MySQL MySQL是一个流行的关系型数据库管理系统。在CentOS 7.6上配置MySQL,可以使用以下命令: sudo systemctl start mysqld sudo systemctl enable mysqld 然后运行以下命令以设置MySQL root用户的密码: sudo mysql_secure_installation 按照提示输入密码并回答其他问题。 4. 安装WordPress WordPress是一个流行的开源博客平台。在CentOS 7.6上安装WordPress,可以使用以下命令: cd /var/www/html sudo wget https://wordpress.org/latest.tar.gz sudo tar -xzvf latest.tar.gz sudo mv wordpress/* . sudo rm -rf wordpress latest.tar.gz 然后设置WordPress目录的权限: sudo chown -R nginx:nginx /var/www/html sudo chmod -R 755 /var/www/html 5. 配置WordPress 在浏览器中访问您的域名,您将看到WordPress的安装向导。按照向导的提示完成安装。 6. 完成 现在您已经成功地在CentOS 7.6上搭建LNMP环境,并安装了WordPress。您可以开始使用WordPress来创建博客和网站了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值