您好,UncleToo欢迎您!  为了更好的浏览本站,请使用高版本浏览器
RSS  Tag     设为首页 | 加入收藏
 您所在的位置:首页 > PHP技术 > PHP应用

Centos7搭建LNMP环境

作者:未知  来源:转载  日期:2016-02-24 21:26:49
收藏  评论:( 0 )  阅读:278

1、先做准备工作:

①、安装并使用zsh作为默认的shell:

具体安装方法请移步:centos安装使用zsh

②、安装LNMP环境所需的基础组件:

yum -y install yum-fastestmirror
yum -y update
yum -y install glibc-static libstdc++-static glibc-devel GeoIP GeoIP-devel
yum -y install patch make flex bison tar pcre pcre-devel screen
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 libmcrypt mash libmcrypt libmcrypt-devel
yum -y install gd re2c recode recode-devel libatomic_ops-devel
yum -y install libXpm libXpm-devel gmp gmp-devel gd-devel
export PHP_AUTOCONF=/usr/bin/autoconf
export PHP_AUTOHEADER=/usr/bin/autoheader

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-7.noarch.rpm,命令如下:

cd ~ && wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
sudo yum -y localinstall mysql57-community-release-el7-7.noarch.rpm

②、安装MySQL:

sudo yum -y install 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密码:

AlterUSER'root'@'localhost' IDENTIFIED BY'MyNewPass4!';

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

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

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

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

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

GRANTALLPRIVILEGESON *.* TO'root'@'%' IDENTIFIED BY'MyNewPass4!'WITHGRANTOPTION;

刷新权限使命令生效:

flush privileges;

退出MySQL:

exit

接下来修改系统配置,

⑤、如果系统的防火墙开着(or behind a hardware firewall or NAT)你必须放开mysql使用的TCP端口,默认是3306;

⑥、修改设定,去掉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.0.3,找到php7.0.3的下载包地址进行下载:
国内建议使用http://cn2.php.net/distributions/php-7.0.3.tar.gz
国外建议使用https://secure.php.net/distributions/php-7.0.3.tar.gz

wget https://secure.php.net/distributions/php-7.0.3.tar.gz
tar -zxvf php-7.0.3.tar.gz
cd php-7.0.3/

③、配置编译php7:

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--with-bz2--with-curl--with-fpm-user=www --with-fpm-group=www --with-freetype-dir=/usr --with-gd--with-gettext--with-gmp--with-iconv--with-iconv-dir=/usr --with-jpeg-dir=/usr --with-mcrypt=/usr/include --with-mhash--with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli=mysqlnd --with-openssl--with-pdo-mysql=mysqlnd --with-pear--with-png-dir=/usr --with-xmlrpc--with-zlib--with-libxml-dir=/usr 

④、安装:

make && make install

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

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

查看安装的php的版本:

php -v

显示结果如下:

PHP 7.0.3 (cli) (built: Feb 24 2016 18:09:02) ( ZTS DEBUG )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, 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-20151012/"

修改完成之后找到[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.0.3 (cli) (built: Feb 24 2016 18:09:02) ( ZTS DEBUG )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
   with Zend OPcache v7.0.6-dev, 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

4、安装Nginx:

①、先做准备工作:

在编译nginx的时候需要pcre、openssl、zlib的源码,所以首先要下载这三个扩展对应的源码包,下载之前首先查看对应版本:

yuminfo pcre zlib openssl

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

cd ~ && wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.32/pcre-8.32.tar.gz
cd ~ && wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1e.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
tar -zxvf openssl-1.0.1e.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模块:

cd ~ && wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.9.32.10-beta.zip -O ngx_pagespeed-1.9.32.10-beta.zip
unzip ngx_pagespeed-1.9.32.10-beta.zip
cd ngx_pagespeed-1.9.32.10-beta/
wget https://dl.google.com/dl/page-speed/psol/1.9.32.10.tar.gz
tar -xzvf 1.9.32.10.tar.gz

③、使用google-perftools提供的TCMalloc工具优化Nginx和MySQL:

PS.由于google-perftools是放在googledrive.com上面,所以首先要能够访问googledrive.com才能完成安装google-perftools

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

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

在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://8045df1dccff66b7affc6e30bb3e31ba49cacfb3.googledrive.com/host/0B6NtGsLhIcf7MWxMMF9JdTN3UVk/gperftools-2.4.tar.gz
tar zxvf gperftools-2.4.tar.gz
cd gperftools-2.4/
./configure
make && make install

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

④、开始安装Nginx:

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

cd ~ && wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx --user=www --group=www --http-log-path=/logs/nginx/access.log --error-log-path=/logs/nginx/error.log --pid-path=/usr/local/nginx/pid/nginx.pid --lock-path=/usr/local/nginx/lock/nginx.lock --with-http_ssl_module --with-http_spdy_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-pcre --with-libatomic --with-http_stub_status_module --http-client-body-temp-path=/tmp/nginx_client --http-proxy-temp-path=/tmp/nginx_proxy --http-fastcgi-temp-path=/tmp/nginx_fastcgi --with-pcre=/root/pcre-8.32 --with-zlib=/root/zlib-1.2.7 --with-openssl=/root/openssl-1.0.1e --add-module=/root/ngx_pagespeed-1.9.32.10-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/
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
ln -s /usr/local/nginx/html /var/www
vi /etc/nginx.conf

删除原来的内容,修改配置文件内容如下:

user www;
worker_processes  4;
error_log  /var/log/nginx/error.log;
pid pid/nginx.pid;
events {
   use epoll;
   worker_connections  1024;
}
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  /var/log/nginx/access.log  main;
   sendfile        on;
   tcp_nopush     on;
   keepalive_timeout  65;
   gzip  on;
   include vhost/*.conf;
}

接下来创建Nginx的日志文件夹:

mkdir -p /var/log/nginx

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

vi /etc/nginx.conf

pid下面加入以下内容:

google_perftools_profiles /tmp/tcmalloc;

创建tcmalloc文件夹:

mkdir -p /tmp/tcmalloc

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

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

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

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For   $proxy_add_x_forwarded_for;
client_max_body_size    50m;
client_body_buffer_size 256k;
proxy_connect_timeout   30;
proxy_send_timeout      30;
proxy_read_timeout      60;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
#Nginx cache
client_body_temp_path      /tmp/client_body_temp 1 2;
proxy_temp_path            /tmp/proxy_temp 1 2;
fastcgi_temp_path          /tmp/fastcgi_temp 1 2;

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

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

pagespeed.conf中加入以下内容:

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
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; }

⑦、创建Nginx虚拟站点:

mkdir -p /usr/local/nginx/conf/vhost
ln -s /usr/local/nginx/conf/vhost /etc/nginxhost
sudo chown -R www:www /var/www/
sudo chmod -R 775 /var/www/
chown -R www:www /var/log/nginx/
chmod -R 775 /var/log/nginx/

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

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

修改default.conf为以下内容:

server {
       listen       80;
       server_name  host;
       #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  /var/log/nginx/host.access.log  main;
       error_log   /var/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 openshared object file: No such fileor 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

PS.如果在centos7下开机之后rc.local中设定的程序无法自动启动,请输入:

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

以上就是centos7下搭建LNMP的全过程



Centos7
 
PHP7
 
MySQL
 
除非特别声明,本站所有PHP教程及其他教程/文章均为原创、翻译或网友投稿,版权均归UncleToo中文网所有, 转载请注明作者及出处。
原文网址:http://www.uncletoo.com/html/application/1170.html
读完这篇文章后,你是否有所收获? 分享是一种生活的信念!
  • 0
  • 0
我来说两句
更多>>网友评论