lnmp编译安装

一、环境准备

centos7.4 nginx1.14.2  php7.1.8 mariadb-10.2.25官网自己下载,准备好yum源,epel源

二、Nginx 编译安装

1.基础环境

yum install -y gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zip unzip zlib-devel bash-completion automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed

2.安装nginx

tar -xf nginx-1.14.2.tar.gz

cd nginx-1.14.2

./configure --prefix=/apps/nginx --user=nginx  --group=nginx --with-http_ssl_module  --with-http_v2_module --with-http_realip_module  --with-http_stub_status_module  --with-http_gzip_static_module  --with-pcre --with-stream  --with-stream_ssl_module --with-stream_realip_module

make&&make install 

useradd nginx -s /sbin/nologin 

chown nginx.nginx -R /apps/nginx/

用nginx命令启动nginx,/app/nginx/sbin/nginx

或者用nginx启动脚本启动,访问本机ip

nginx默认页截图

3.虚拟主机

3.1 https访问

cd /apps/nginx/

cd certs/

 openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt #自签名CA证书

 req -newkey rsa:4096 -nodes -sha256 -keyout www.zy.com.key -out www.zy.com.csr

openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.zy.com.key -out www.zy.com.csr

#生成kry和csr

 openssl x509 -req -days 3650 -in www.zy.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.zy.com.crt

#签发证书

vi /apps/nginx/conf/conf.d/zy.conf  #配置虚拟主机

server{

  listen 80;

  listen 443 ssl;

  server_name www.zy.com;

  ssl_certificate /apps/nginx/certs/www.zy.com.crt;   

  ssl_certificate_key /apps/nginx/certs/www.zy.com.key;   

  ssl_session_cache shared:sslcache:20m;

  ssl_session_timeout 10m;

  access_log /data/nginx/logs/www.zy.access.log access_json;

  location / {

  root /data/nginx/zy;

  index index.html;

}

  error_page 500 502 503 504 404 /error.html;

  location = /error.html {

  root /data/nginx/html/zy;

  }

}


https访问截图

3.2 json日志格式

vi /apps/nginx/conf/nginx.conf  #自定义日志为json格式,为了今后方便日志处理

log_format access_json '{"@timestamp":"$time_iso8601",'

'"host":"$server_addr",'

'"clientip":"$remote_addr",'

'"size":$body_bytes_sent,'

'"responsetime":$request_time,'

'"upstreamtime":"$upstream_response_time",'

'"upstreamhost":"$upstream_addr",'

'"http_host":"$host",'

'"uri":"$uri",'

'"domain":"$host",'

'"xff":"$http_x_forwarded_for",'

'"referer":"$http_referer",'

'"tcp_xff":"$proxy_protocol_addr",'

'"http_user_agent":"$http_user_agent",'

'"status":"$status"}';

4.测试

mkdir -p /data/nginx/zy

mkdir  -p root /data/nginx/html/zy

echo "zy page" >> /data/nginx/zy/index.html

echo " zy error page" >>  /data/nginx/html/zy/error.html

修改测试客户端主机hosts表 www.zy.com 192.168.25.129,测试效果如图


错误页测试截图
日志截图

三 mysql编译安装

注意mysql编译安装时间长

1.基础环境

yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-

devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-

devel libevent-devel libaio-devel

做准备用户和数据目录

useradd –r –s /sbin/nologin –d /data/mysql/ mysql

mkdir /data/mysql

chown mysql.mysql /data/mysql

2.编译安装

tar xvf mariadb-10.2.15.tar.gz

cd mariadb-10.2.15/

cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql -DMYSQL_DATADIR=/data/mysql/ -DSYSCONFDIR=/etc/ -DMYSQL_USER=mysql DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITHOUT_MROONGA_STORAGE_ENGINE=1 -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DENABLED_LOCAL_INFILE=1 -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

make && make install

3.配置文件和启动脚本

生成数据库文件

cd /app/mysql/

scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql

准备配置文件

vi /etc/my.cnf

[mysqld]

    character-set-server=utf8mb4

    collation-server=utf8mb4_general_ci

    basedir=/app/mysql/

    datadir=/data/mysql/

    socket=/var/lib/mysql/mysql.sock

    skip_name_resolve = on

[client]

    socket=/var/lib/mysql/mysql.sock

[mysqld_safe]

    log-error=/var/log/mariadb/mariadb.log

    pid-file=/var/run/mariadb/mariadb.pid

准备启动脚本

cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld

启动服务

chkconfig --add mysqld ;service mysqld start

登陆mysql

php编译安装

php编译安装时间很长

1.基础环境

yum -y install make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libzip libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap lsof wget python-devel cmake libxslt-devel libgcrypt-devel readline-devel libgpg-error-devel libmcrypt libmcrypt-devel mcrypt

2.编译安装

tar -zxvf php-7.1.18.tar.gz

cd php-7.1.18

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc/ --enable-fpm --with-fpm-user=www  --with-fpm-group=www --enable-inline-optimization --disable-debug --disable-rpath --enable-shared  --enable-soap --with-libxml-dir --with-xmlrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir  --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets  --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache

cp php.ini-production /usr/local/php/etc/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

3.配置文件启动脚本

vim /usr/local/php/etc/php-fpm.d/www.conf

[www]

user = nginx #php-fpm启动的用户和组,会涉及到后期文件的权限问题 ,主要改两项

group = nginx#

4.测试nginx支持php,php连接mysql

4.1 显示php信息页

mkdir -p /data/nginx/php

cat /data/nginx/php/index.php #php测试页面

phpinfo();

?>

vi /apps/nginx/conf/conf.d/zy.conf 

location ~ \.php$ {

      root /data/nginx/php;

      fastcgi_pass 127.0.0.1:9000;# 如果php不在本机要改地址

      fastcgi_index index.php;

      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

      include fastcgi_params;

      }


phpinfo

4.2.创建PHP页面,测试LNMP架构能否解析PHP页面,mysql连接

cat /data/nginx/php/mysql.php

$links=mysqli_connect("127.0.0.1","root","");        //root为mysql账户名称,密码需要修改为实际mysql密码,无密码则留空即可

if($links){

echo "link db ok!!!";

}

else{

echo "link db no!!!";

}

?>

打开php.ini修改mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket 的值为/var/lib/mysql/mysql.sock

你可能感兴趣的:(lnmp编译安装)