1. 程式人生 > >centos7下編譯安裝lnmp和nginx代理實驗

centos7下編譯安裝lnmp和nginx代理實驗

ket orm pass glib href status log nal listen

1、先安裝mysql

cd /usr/local/src
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 
tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
cd /usr/local/mysql
useradd mysql
mkdir /data/
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
cp support-files/my-default.cnf  /etc/my.cnf 
cp support-files/mysql.server /etc/init.d/mysqld
vi /etc/init.d/mysqld

定義basedir和datadir

/etc/init.d/mysqld start

2、安裝php

編譯安裝php-fpm

什麽是PHP-FPM

PHP-FPM是一個PHP FastCGI管理器,是只用於PHP的,可以在 http://php-fpm.org/download下載得到.

PHP-FPM其實是PHP源代碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP源代碼中,在編譯安裝PHP後才可以使用。

新版PHP已經集成php-fpm了,不再是第三方的包了,推薦使用。PHP-FPM提供了更好的PHP進程管理方式,可以有效控制內存和進程、可以平滑重載PHP配置,比spawn-fcgi具有更多優點,所以被PHP官方收錄了。在./configure的時候帶 –enable-fpm參數即可開啟PHP-FPM,其它參數都是配置php的,具體選項含義可以查看這裏。

安裝前準備
centos下執行

yum -y install gcc automake autoconf libtool make

yum -y install gcc gcc-c++ glibc

yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel

axel http://mirrors.sohu.com/php/php-7.2.1.tar.xz
tar Jxf php-7.2.1.tar.xz
 useradd -s /sbin/nologin php-fpm
 cd php-7.2.1

./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt 
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath 
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets 
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex 
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli 
--with-gd --with-jpeg-dir

make all install
cp php.ini-production /usr/local/php-fpm/etc/php.ini

vim /usr/local/php-fpm/etc/php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
 chmod 755 /etc/init.d/php-fpm
 chkconfig --add php-fpm
 chkconfig php-fpm on
 service php-fpm start
 ps aux |grep php-fpm

報錯 Cannot find OpenSSL‘s <evp.h>
執行 yum install openssl openssl-devel

報錯 Please reinstall the libcurl distribution
執行 yum -y install curl-devel

錯誤 jpeglib.h not found
執行 yum install libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y
和執行 yum install libjpeg-devel

錯誤:checking for BZip2 in default path... not found configure: error: Please reinstall the BZip2 distribution 這是bzip2軟件包沒有安裝
執行 yum install bzip2-devel.x86_64 -y

錯誤:configure: error: xpm.h not found.
執行yum install libXpm-devel

錯誤: Unable to locate gmp.h
執行 yum install gmp-devel

錯誤:Unable to detect ICU prefix or /usr//bin/icu-config failed. Please verify ICU install prefix and make sure icu-config works
執行 yum install -y icu libicu libicu-devel

錯誤:mcrypt.h not found. Please reinstall libmcrypt.
執行 yum install php-mcrypt libmcrypt libmcrypt-devel

錯誤: configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path
執行 yum install postgresql-devel

錯誤: configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
執行yum install libxslt-devel

3、安裝nginx

axel https://nginx.org/download/nginx-1.12.2.tar.gz
Initializing download: https://nginx.org/download/nginx-1.12.2.tar.gz

tar zxf nginx-1.12.2.tar.gz 
cd nginx-1.12.2/
useradd nginx -s /sbin/nologin 

Nginx編譯參數
–user 指定啟動程序所屬用戶
–group 指定組
–prefix 指定安裝路徑
–sbin-path 設置nginx二進制文件的路徑名
–conf-path 指定配置文件路徑
–error-log-path 錯誤日誌文件路徑
–http-log-path 指定訪問日誌文件路徑
–http-client-body-temp-path 設置存儲HTTP客戶端請求主體的臨時文件路徑
–http-proxy-temp-path 設置存儲HTTP代理臨時文件的路徑
–http-fastcgi-temp-path 設置存儲HTTP fastcgi的臨時文件的路徑
–pid-path 設置nginx.pid文件路徑
–lock-path 設置nginx.lock文件路徑
–with-openssl 啟用SSL
–with-pcre 啟用正則表達式
–with-http_stub_status_module 安裝可以監控nginx狀態的模塊
–with-http_ssl_module 啟用SSL支持
–with-http_gzip_static_module 啟用gzip壓縮

./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-http_sub_module
echo $?
make && make install

vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/sbin/nginx"
NGINX_CONF="/etc/nginx/nginx.conf"
NGINX_PID="/var/run/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}
stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL
chmod 755 !$
chkconfig --add nginx
service nginx start
ps aux | grep nginx
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload 

增加mysql和php的環境變量

vim /etc/profile.d/mysql.sh
PATH=$PATH:/usr/local/mysql/bin

vim /etc/profile.d/php.sh 
PATH=$PATH:/usr/local/php-fpm/bin

非虛擬主機的配置

vim /etc/nginx/nginx.conf

user nginx nginx;
worker_processes 2;
error_log /var/log/nginx/error.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
    ‘ $host "$request_uri" $status‘
    ‘ "$http_referer" "$http_user_agent"‘;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /tmp/nginx/client_body;
    proxy_temp_path /tmp/nginx/proxy;
    fastcgi_temp_path /tmp/nginx/fastcgi;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
        }    
    }
}
Vim /data/www/1.php

<?php
    echo "test php scripts.";
?>

curl localhost/1.php

test php scripts.

配置默認虛擬主機:
mkdir /etc/nginx/vhost
修改nginx.conf,
增加

 include vhost/*.conf
user nginx nginx;
worker_processes 2;
error_log /var/log/nginx/error.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip ‘$remote_addr $http_x_forwarded_for [$time_local]‘
    ‘ $host "$request_uri" $status‘
    ‘ "$http_referer" "$http_user_agent"‘;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /tmp/nginx/client_body;
    proxy_temp_path /tmp/nginx/proxy;
    fastcgi_temp_path /tmp/nginx/fastcgi;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    include vhost/*.conf;
}
Vim /etc/nginx/vhost/default.conf

server
{
listen 80;
server_name oceanwing.com;
index index.html index.htm index.php;
root /data/www/default;
location ~ \.php$ 
    {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/default$fastcgi_script_name;
    }
}

nginx -t
nginx -s reload

mkdir -p /data/www/default/

mv /data/www/*.php /data/www/default
 curl -x 127.0.0.1:80 oceanwing.com

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<title>LNMP</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Gavin Zhao">
<meta name="keywords" content="Centos7服務器">
<meta name="description" content="">
<link rel="shortcut icon" href="favicon.ico" type="image/ico">
<style type="text/css">



恭喜,lnmp 已安裝成功!

查看服務器WEB環境:探針信息 phpinfo信息

為安全起見,本頁可刪除或重命名

添加虛擬主機代理: 1、confluence代理 80端口代理confluence的8090 vim /etc/ng``inx/vhost/itks.oceanwing.com.conf server { server_name itks.oceanwing.com listen 80; location / { proxy_pass http://172.16.20.247:8090/; proxy_set_header Host $host:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 2、Jenkins代理 ``` vim /etc/nginx/vhost/jenkins.oceanwing.com.conf server { server_name jenkins.oceanwing.com listen 80; location / { proxy_pass http://172.16.22.245:8080; proxy_set_header Host $host:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` 3、jira的代理 ``` vim /etc/nginx/vhost/jira.oceanwing.com.conf server { server_name jira.oceanwing.com listen 80; location / { proxy_pass http://172.16.20.247:8080/; proxy_set_header Host $host:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` ![](http://i2.51cto.com/images/blog/201802/08/f6d353a850cef04d710df548c9141875.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

centos7下編譯安裝lnmp和nginx代理實驗