【Linux運維】LNMP環境配置

旅行者-Travel發表於2018-04-09

安裝準備:

Centos7.3

MYSQL5.6

PHP5.6

NGINX1.10.3

一、安裝Mysql

mysql:
[root@host129 src]#cd
/usr/local/src/ [root@host129 src]# wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz
[root@host129 src]# tar zxvf mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz  [root@host129 src]# mv mysql-5.6.41-linux-glibc2.12-x86_64 /usr/local/mysql [root@host129 src]# useradd -s /sbin/nologin mysql [root@host129 src]# cd /usr/local/mysql/ [root@host129 mysql]# mkdir -p /data/mysql [root@host129 mysql]# chown -R mysql:mysql /data/mysql/ [root@host129 mysql]# [root@host129 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql [root@host129 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db: Data::Dumper 出現以上報錯:安裝一下軟體,再次執行指令碼 [root@host129 mysql]# yum -y install autoconf [root@host129 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql [root@host129 mysql]# cp support-files/my-default.cnf /etc/my.cnf [root@host129 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@host129 mysql]# chmod 755 /etc/init.d/mysqld [root@host129 mysql]# vim /etc/init.d/mysqld datadir=/data/mysql [root@host129 mysql]# chkconfig --add mysqld [root@host129 mysql]# chkconfig mysqld on [root@host129 mysql]# service mysqld start Starting MySQL.Logging to `/data/mysql/host129.err`. . SUCCESS! [root@host129 mysql]# ps aux | grep mysqld 首次進入mysql: [root@host129 ~]# /usr/local/mysql/bin/mysql -uroot

Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.41 MySQL Community Server (GPL)

mysql> exit
Bye

二、安裝PHP

PHP:
[root@host129 src]#cd /usr/local/src/
[root@host129 src]#wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
[root@host129 src]#tar zxf php-5.6.30.tar.gz
[root@host129 src]# useradd -s /sbin/nologin php-fpm
[root@host129 src]# cd php-5.6.30
[root@host129 php-5.6.30]# 

[root@host129 php-5.6.30]# ./configure 
--prefix=/usr/local/php-fpm 
--with-config-file-path=/usr/local/php-fpm/etc 
--enable-fpm 
--with-fpm-user=php-fpm 
--with-fpm-group=php-fpm 
--with-mysql=/usr/local/mysql 
--with-mysql-sock=/tmp/mysql.sock 
--with-libxml-dir 
--with-gd 
--with-jpeg-dir 
--with-png-dir 
--with-freetype-dir 
--with-iconv-dir 
--with-zlib-dir 
--with-mcrypt 
--enable-soap 
--enable-gd-native-ttf 
--enable-ftp 
--enable-mbstring 
--enable-exif 
--disable-ipv6 
--with-pear 
--with-curl 
--with-openssl



error1:
configure: error: xml2-config not found. Please check your libxml2 installation.
[root@host129 php-5.6.30]# yum -y install libxml2
已載入外掛:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
軟體包 libxml2-2.9.1-6.el7_2.3.x86_64 已安裝並且是最新版本
無須任何處理
解決:
[root@host129 php-5.6.30]# yum -y install libxml2-devel


error2:
configure: error: Cannot find OpenSSL`s <evp.h>
解決:
[root@host129 php-5.6.30]#yum -y install  openssl.x86_64 openssl-devel.x86_64



error3:
checking for T1lib support... no
checking whether to enable truetype string function in GD... yes
checking whether to enable JIS-mapped Japanese font support in GD... no
If configure fails try --with-vpx-dir=<DIR>
configure: error: jpeglib.h not found.
解決:
[root@host129 php-5.6.30]# yum -y install libjpeg libjpeg-devel

error4:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解決:
[root@host129 php-5.6.30]#yum -y install libmcrypt libmcrypt-devel


[root@host129 php-5.6.30]#make install
[root@host129 php-5.6.30]# echo $?
0
[root@host129 php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
[root@host129 php-5.6.30]# 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

[root@host129 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t
[09-Apr-2018 23:49:31] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful

[root@host129 php-5.6.30]# cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@host129 php-5.6.30]# chmod 755 /etc/init.d/php-fpm 
[root@host129 php-5.6.30]# useradd -s /sbin/nologin php-fpm
useradd:使用者“php-fpm”已存在
[root@host129 php-5.6.30]# service php-fpm start
Starting php-fpm  done
[root@host129 php-5.6.30]# chkconfig php-fpm on
[root@host129 php-5.6.30]# ps aux |grep php-fpm

三、安裝Nginx

NGINX:

NGINX官網https://nginx.org/
[root@host129 src]#cd /usr/local/src/
[root@host129 src]# wget https://nginx.org/download/nginx-1.10.3.tar.gz
[root@host129 src]# tar zxvf nginx-1.10.3.tar.gz 
[root@host129 src]# cd nginx-1.10.3/
[root@host129 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx
[root@host129 nginx-1.10.3]# make & make install

編寫nginx啟動指令碼:
[root@host129 nginx-1.10.3]# 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/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/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

儲存以上指令碼更改許可權:
[root@host129 nginx-1.10.3]# chmod 755 /etc/init.d/nginx 
新增nginx服務項命令:
[root@host129 nginx-1.10.3]# chkconfig --add nginx
新增完設定啟動項:
[root@host129 nginx-1.10.3]# chkconfig nginx on

清空原配置檔案:
[root@host129 nginx-1.10.3]# > /usr/local/nginx/conf/nginx.conf

重新寫入一下內容:
[root@host129 nginx-1.10.3]#vim /usr/local/nginx/conf/nginx.conf

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/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 /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    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 /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

驗證一nginx下配置檔案:
[root@host129 nginx-1.10.3]# /usr/local/nginx/sbin/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

(檢視一下:
[root@host129 nginx-1.10.3]#ps aux |grep httpd
[root@host129 nginx-1.10.3]#service stop httpd
也可以直接解除安裝:yum -y remove httpd  )如果沒下邊啟動報錯可以不用做這一步

啟動Nginx:
[root@host129 nginx-1.10.3]# service nginx start 
StartStarting nginx (via systemctl):  Job for nginx.service failed because the control process exited with error code. 
See "systemctl status nginx.service" and "journalctl -xe" for details

如果報以上錯誤:
通過檢視日誌:/var/log/messages這個日誌
[root@host129 nginx-1.10.3]# less /var/log/messages
Apr 12 10:30:29 host129 nginx: Starting Nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 12 10:30:29 host129 nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 12 10:30:30 host129 nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 12 10:30:30 host129 nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 12 10:30:31 host129 nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Apr 12 10:30:31 host129 nginx: nginx: [emerg] still could not bind()
Apr 12 10:30:31 host129 nginx: [失敗]
Apr 12 10:30:31 host129 systemd: nginx.service: control process exited, code=exited status=1
Apr 12 10:30:31 host129 systemd: Failed to start SYSV: http service..
Apr 12 10:30:31 host129 systemd: Unit nginx.service entered failed state.
Apr 12 10:30:31 host129 systemd: nginx.service failed.

發現nginx埠被佔用:
[root@host129 nginx-1.10.3]# ps aux |grep httpd
root        983  0.0  0.3 221948  4964 ?        Ss   09:29   0:00 /usr/sbin/httpd -DFOREGROUND
apache     2203  0.0  0.2 224032  3096 ?        S    09:29   0:00 /usr/sbin/httpd -DFOREGROUND
apache     2204  0.0  0.2 224032  3096 ?        S    09:29   0:00 /usr/sbin/httpd -DFOREGROUND
apache     2205  0.0  0.2 224032  3096 ?        S    09:29   0:00 /usr/sbin/httpd -DFOREGROUND
apache     2207  0.0  0.2 224032  3096 ?        S    09:29   0:00 /usr/sbin/httpd -DFOREGROUND
apache     2208  0.0  0.2 224032  3096 ?        S    09:29   0:00 /usr/sbin/httpd -DFOREGROUND
root       6189  0.0  0.0 112676   972 pts/0    R+   10:38   0:00 grep --color=auto httpd
[root@host129 nginx-1.10.3]# service stop httpd
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
[root@host129 nginx-1.10.3]# systemctl stop httpd       
[root@host129 nginx-1.10.3]# service nginx start   
Starting nginx (via systemctl):                            [  確定  ]

檢視nginx啟動程式:
[root@host129 nginx-1.10.3]# ps aux |grep nginx
root       6248  0.0  0.0  24960   796 ?        Ss   10:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     6249  0.0  0.2  29440  3688 ?        S    10:38   0:00 nginx: worker process
nobody     6250  0.0  0.2  29440  3428 ?        S    10:38   0:00 nginx: worker process
root       6341  0.0  0.0 112676   968 pts/0    R+   10:45   0:00 grep --color=auto nginx

驗證php解析:
[root@host129 nginx-1.10.3]# vim /usr/local/nginx/html/2.php
<?php
        echo "test php scripts"
?>
[root@host129 nginx-1.10.3]# curl localhost/2.php
test php scripts
證明php解析正常

 四、配置虛擬主機

[root@host129 ~]# vim /usr/local/nginx/conf/nginx.conf
結束符之前:
include vhost
/*.conf; } [root@host129 ~]# mkdir /usr/local/nginx/conf/vhost [root@host129 ~]# cd /usr/local/nginx/conf/vhost/ [root@host129 vhost]# vim default.conf server { listen 80 default_server; server_name yanglt.com; index index.html index.htm index.php; root /data/nginx/default; } [root@host129 vhost]# /usr/local/nginx/sbin/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 # /usr/local/nginx/sbin/nginx -s reload #makdir -r /data/nginx/default #echo "default_server" > /data/nginx/default/index.html [root@host129 nginx]# curl -x127.9.9.1:80 yanglt.com default_server [root@host129 nginx]# curl -x127.9.9.1:80 ssss.com default_server

 

相關文章