2-25 源码编译搭建LNMP环境
Posted 喝茶等下班
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2-25 源码编译搭建LNMP环境相关的知识,希望对你有一定的参考价值。
1.安装配置nginx
epel源需要先装
[root@xuegod13 yum.repos.d]# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
然后装部署LNMP架构需要安装依赖包:
[root@xuegod13 ~]# yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel gmp-devel unzip libcap lsof
本节课涉及到的源码包如下,上传到/opt目录
[root@xuegod13 opt]# ls -1
boost_1_59_0.tar.gz
libiconv-1.14.tar.gz
libmcrypt_downcc.zip
mcrypt-2.6.8.tar.gz
mysql-5.7.19.tar.gz
nginx-1.14.2.tar.gz
pcre-8.41.tar.gz
php-7.1.24.tar.gz
再次安装依赖包:注意,使用epel源的时候要在yum.repo.d目录下
[root@xuegod13 yum.repos.d]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
创建Nginx运行用户
[root@xuegod13 opt]# useradd -M -s /sbin/nologin nginx
解压缩pcre和nginx到/usr/local/src/下:
[root@xuegod13 opt]# ls
boost_1_59_0.tar.gz libmcrypt_downcc.zip mhash-0.9.9.9.tar.gz nginx-1.14.2.tar.gz php-7.1.24.tar.gz
libiconv-1.14.tar.gz mcrypt-2.6.8.tar.gz mysql-5.7.19.tar.gz pcre-8.41.tar.gz
[root@xuegod13 opt]# tar xf pcre-8.41.tar.gz -C /usr/local/src/
[root@xuegod13 opt]# tar xf nginx-1.14.2.tar.gz -C /usr/local/src/
[root@xuegod13 opt]# cd !$
cd /usr/local/src/
[root@xuegod13 src]# ls
nginx-1.14.2 pcre-8.41
[root@xuegod13 src]#
安装nginx:
[root@xuegod13 src]# cd nginx-1.14.2/
[root@xuegod13 nginx-1.14.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@xuegod13 nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx
#输出内容省略
[root@xuegod13 nginx-1.14.2]# make -j 4 && make install
#输出内容省略
修改nginx配置文件
[root@xuegod13 nginx-1.14.2]# ll /usr/local/nginx/
总用量 0
drwxr-xr-x 2 root root 333 2月 10 18:30 conf
drwxr-xr-x 2 root root 40 2月 10 18:30 html
drwxr-xr-x 2 root root 6 2月 10 18:30 logs
drwxr-xr-x 2 root root 19 2月 10 18:30 sbin
[root@xuegod13 nginx]# cd conf/
[root@xuegod13 conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@xuegod13 conf]# cp nginx.conf nginx.conf.bak
[root@xuegod13 conf]# vim nginx.conf
改用户
#user nobody;
改为
user nginx;
启用支持php,66-70行,去掉前面的#号
#location ~ \\.php$
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#
加载环境变量
[root@xuegod13 conf]# cd ..
[root@xuegod13 nginx]# ll sbin/
总用量 4032
-rwxr-xr-x 1 root root 4125688 2月 10 18:30 nginx
[root@xuegod13 nginx]# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
[root@xuegod13 nginx]# . /etc/profile.d/nginx.sh
[root@xuegod13 nginx]# cd
[root@xuegod13 ~]# nginx -v #查看版本
nginx version: nginx/1.14.2
查看nginx加载了哪些模块
[root@xuegod13 ~]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx
检查配置文件语法是否有错误
[root@xuegod13 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
-T 检查语法并显示更多配置信息
[root@xuegod13 ~]# nginx -T
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# configuration file /usr/local/nginx/conf/nginx.conf:
user nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events
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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /
root html;
index index.html index.htm;
#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;
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \\.php$
# proxy_pass http://127.0.0.1;
#
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \\.php$
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
# deny access to .htaccess files, if Apaches document root
# concurs with nginxs one
#
#location ~ /\\.ht
# deny all;
#
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location /
# root html;
# index index.html index.htm;
#
#
# HTTPS server
#
#server
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location /
# root html;
# index index.html index.htm;
#
#
# configuration file /usr/local/nginx/conf/mime.types:
types
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/svg+xml svg svgz;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/webp webp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
application/font-woff woff;
application/java-archive jar war ear;
application/json json;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.apple.mpegurl m3u8;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/vnd.ms-excel xls;
application/vnd.ms-fontobject eot;
application/vnd.ms-powerpoint ppt;
application/vnd.oasis.opendocument.graphics odg;
application/vnd.oasis.opendocument.presentation odp;
application/vnd.oasis.opendocument.spreadsheet ods;
application/vnd.oasis.opendocument.text odt;
application/vnd.openxmlformats-officedocument.presentationml.presentation
pptx;
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xlsx;
application/vnd.openxmlformats-officedocument.wordprocessingml.document
docx;
application/vnd.wap.wmlc wmlc;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/xspf+xml xspf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mp2t ts;
video/mp4 mp4;
video/mpeg mpeg mpg;
video/quicktime mov;
video/webm webm;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
# configuration file /usr/local/nginx/conf/fastcgi_params:
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
启动nginx
[root@xuegod13 ~]# nginx
[root@xuegod13 ~]# ps aux|grep nginx
root 42776 0.0 0.0 18236 640 ? Ss 18:50 0:00 nginx: master process nginx
nginx 42777 0.0 0.0 18604 1360 ? S 18:50 0:00 nginx: worker process
root 42779 0.0 0.0 112724 988 pts/0 S+ 18:50 0:00 grep --color=auto nginx
[root@xuegod13 ~]# netstat -an|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
unix 3 [ ] DGRAM 22803
unix 3 [ ]
添加个脚本,并设置为开机自启动
[root@xuegod13 ~]# #生成服务启动脚本
[root@xuegod13 ~]# vim /etc/init.d/nginx
# chkconfig:- 99 2
# description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -3 $(cat $PIDF)
;;
restart)
$0 stop &> /dev/null
if [ $? -ne 0 ];then continue;fi
$0 start
;;
reload)
2-21-源码编译搭建LNMP环境