码迷,mamicode.com
首页 > Web开发 > 详细

使用LNMP构建企业web站点

时间:2019-09-22 21:38:36      阅读:25      评论:0      收藏:0      [点我收藏+]

标签:开机自启动   lib   body   安装nginx   htm   type   下载   yum源   index   

实验目的:

使用LNMP架构搭建一个web站点

一、环境准备

1、一台Centos7服务器,配置好IP地址,服务器名称为nginx.web.com;

2、为每台服务器配置yum源;

3、准备好源码编译环境;

4、实验所需要的软件包可使用以下方式获取。

链接:https://pan.baidu.com/s/1pi1XsjFE8FL4LChfbDVoJg
提取码:04as

二、服务器初始状态准备

1、为服务器配置好IP地址192.168.4.150(可以自己设定);

2、检查防火墙是否关闭,如果没关闭可使用iptables -F关闭

技术图片

3、检查selinux是否关闭,如果没有关闭,可使用setenforce 0命令临时修改

技术图片

三、部署Nginx服务

1、将准备好的软件包传入虚拟机

[[email protected] ~]# mkdir tools    //创建存放软件包的目录

[[email protected] ~]# cd tools 

[[email protected] tools]# rz         //上传工具,通过yum install -y lrzsz来安装

[[email protected] tools]# ls         //查看软件包是否都上传成功(共六个软件包)

cmake-2.8.6.tar.gz      php-5.3.28.tar.gz

mysql-5.5.22.tar.gz     SKYUC.v3.4.2.SOURCE.zip

nginx-1.6.0.tar.gz        ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz

(也可以联网,使用wget直接在网站上下载)

2、安装编译环境以及Nginx的依赖包

[[email protected] ~]# yum -y install gcc gcc-c++ make    //安装编译环境

[[email protected] ~]# rpm -aq gcc gcc-c++ make           //检查软件包是否安装成功

make-3.82-21.el7.x86_64

gcc-4.8.5-4.el7.x86_64

gcc-c++-4.8.5-4.el7.x86_64

[[email protected] ~]# yum -y install pcre-devel zlib-devel openssl-devel    //安装Nginx的依赖包

[[email protected] ~]# rpm -aq pcre-devel zlib-devel openssl-devel

pcre-devel-8.32-15.el7.x86_64

openssl-devel-1.0.1e-42.el7.9.x86_64

zlib-devel-1.2.7-15.el7.x86_64

3、对Nginx进行编译安装:

检测系统是否安装Apache服务,如果安装则需要卸载Apache;

[[email protected] ~]# rpm -aq httpd   

(1)创建Nginx程序用户

[[email protected] ~]# useradd -M -s /sbin/nologin nginx          //创建Nginx用户

[[email protected] ~]# tail -1 /etc/passwd;tail -1 /etc/group       //检查用户是否创建成功

(2)编译安装

[[email protected] ~]# cd tools/                          //进入存放软件包的目录

[[email protected] tools]# tar xf nginx-1.6.0.tar.gz -C /usr/src/       //解压nginx这个包到 /usr/src目录

[[email protected] tools]# cd /usr/src/nginx-1.6.0/                        //进入解压完的目录文件中

[[email protected] nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio  --with-http_mp4_module --with-http_ssl_module && make && make install  //编译安装

[[email protected] nginx-1.6.0]# echo $?    //检查上一条命令是否执行成功,0表示成功

0

(3)修改Nginx配置文件

[[email protected] ~]# cd /usr/local/nginx/conf/                //进入存放配置文件目录

[[email protected] conf]# cp nginx.conf nginx.conf.bak    //备份

[[email protected] conf]# vim nginx.conf                         //修改配置文件,以下是修改的文件的内容

2 user  nginx nginx;

5 worker_processes  1;

7 error_log  logs/error.log  info;

9 pid        logs/nginx.pid;

12 events {

13     use epoll; 

14     worker_connections  10240;

15 }

18 http {

19     include       mime.types;

20     default_type  application/octet-stream;

21

22     log_format  main  $remote_addr - $remote_user [$time_local] "$request" 

23                       $status $body_bytes_sent "$http_referer" 

24                       "$http_user_agent" "$http_x_forwarded_for";

25

26     access_log  logs/access.log  main;

27

28     sendfile        on;

29     tcp_nopush     on;

36     server {

37         listen       80;

38         server_name  localhost;

39

40         charset utf-8;

41

42         access_log  logs/nginx.yunban.cn.access.log  main;

43

44         location / {

45             root   html;

46             index  index.html index.htm;

47         }

 

修改完保存并退出!

(4)创建软连接

[[email protected] conf]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

(5)修改系统最大打开文件数

[[email protected] conf]# ulimit -HSn 65535

[[email protected] conf]# ulimit –n

65535

[[email protected] conf]# echo "ulimit -HSn 65535" >> /etc/profile

[[email protected] conf]# tail -1 /etc/profile

ulimit -HSn 65535

(6)配置文件语法检测

[[email protected] conf]# 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

4、启动Nginx服务,并设置为开机自启动

[[email protected] ~]# cd /usr/local/nginx/sbin

[[email protected] sbin]# ./nginx

[[email protected] sbin]# netstat -anput|grep 80  

技术图片

5、测试Nginx访问情况

在浏览器中输入服务器的ip地址,出现以下页面表示成功;

技术图片

四、安装MySQL服务

1、 检查是否安装Mariadb数据库,如果安装则使用rpm卸载,安装依赖包

[[email protected] ~]# rpm -aq mariadb mariadb-server mariadb-client              //检查是否安装mariadb数据库

[[email protected] ~]# yum -y install ncurses-devel                          //安装数据库依赖包

[[[email protected] ~]# rpm -aq ncurses-devel  

ncurses-devel-5.9-13.20130511.el7.x86_64

2、 安装cmake为作为MySQL编译工具

[[email protected] ~]# cd tools/

[[email protected] tools]# tar xf cmake-2.8.6.tar.gz -C /usr/src/

[[email protected] tools]# cd /usr/src/cmake-2.8.6/

[[email protected] cmake-2.8.6]# ./configure && gmake &&gmake install      //编译安装

[[email protected] cmake-2.8.6]# echo $?

0

3、 编译安装MySQL数据库

[[email protected] cmake-2.8.6]# cd /root/tools/

[[email protected] tools]# tar xf mysql-5.5.22.tar.gz -C /usr/src/

[[email protected] tools]# cd /usr/src/mysql-5.5.22/

[[email protected] mysql-5.5.22]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc && make && make install           //编译安装数据库

4、 安装后配置

[[email protected] mysql-5.5.22]# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile 

[[email protected] mysql-5.5.22]# . /etc/profile 

[[email protected] mysql-5.5.22]# echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin

准备相关配置文件:

[[email protected]~]# /bin/cp-p /usr/src/mysql-5.5.22/support-files/my-medium.cnf /etc/my.cnf

[[email protected] ~]# /bin/cp -p /usr/src/mysql-5.5.22/support-files/mysql.server /etc/init.d/mysqld

[[email protected] ~]# chmod +x /etc/init.d/mysqld         //授予执行权限

[[email protected] ~]# chkconfig --add mysqld

[[email protected] ~]# chkconfig mysqld on

5、 初始化数据库

[[email protected] ~]# useradd -M -s /sbin/nologin mysql              //创建mysql用户

[[email protected] ~]# chown -R mysql:mysql /usr/local/mysql/     //修改所属组

[[email protected] ~]# /usr/local/mysql/scripts/mysql_install_db  --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql

6、 启动MySQL数据库,并为MySQL的root用户创建密码

[[email protected] ~]# /etc/init.d/mysqld start       //启动数据库

Starting MySQL... SUCCESS!

[[email protected] ~]# netstat -anptu|grep 3306    //查看数据库是否启动

技术图片

[[email protected] ~]# mysqladmin -uroot  password "123456" ;history -c    //为root用户创建密码

7、 登录数据库

技术图片

登录成功!

五、部署PHP服务

1、 安装PHP服务

[[email protected] ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel    

[[email protected] ~]# cd tools/

[[email protected] tools]# tar xf php-5.3.28.tar.gz -C /usr/src/

[[email protected] tools]# cd /usr/src/php-5.3.28/

[[email protected] php-5.3.28]# ./configure  --prefix=/usr/local/php5  --with-gd  --with-zlib --with-mysql=/usr/local/mysql/  --with-config-file-path=/usr/local/php5  --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib && make && make install                                //编译安装php服务

[[email protected] php-5.3.28]# echo $?

0

2、 安装后调整优化

[[email protected] php-5.3.28]# cp -p /usr/src/php-5.3.28/php.ini-development /usr/local/php5/php.ini

[[email protected] php-5.3.28]# ln -s /usr/local/php5/bin/* /usr/local/bin/                 //创建软连接

[[email protected] php-5.3.28]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/

3、 安装 ZendGuardLoader (PHP  的优化模块)

[[email protected] php-5.3.28]# cd /root/tools/

[[email protected] tools]#tar xf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz -C /usr/src/

[[email protected] tools]cp /usr/src/ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so /usr/local/php5/lib/php/

[[email protected] tools]# echo -e "zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so\nzend_loader.enable=1" >> /usr/local/php5/php.ini

[[email protected] tools]# tail -2 /usr/local/php5/php.ini

zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so

zend_loader.enable=1

4、 配置并启动

[[email protected] tools]# cd /usr/local/php5/etc/

[[email protected] etc]# cp -p php-fpm.conf.default php-fpm.conf

[[email protected] etc]# vim php-fpm.conf

25 pid = run/php-fpm.pid //指定 pid 文件位置

140 user = nginx //程序用户

141 group = nginx //程序组

217 pm.max_children = 50 //子进程的最大数

222 pm.start_servers = 20 //启动时开启的进程数

227 pm.min_spare_servers = 5 //最少空闲进程数

232 pm.max_spare_servers = 35 //最大空闲进程数

5、启动PHP

[[email protected] etc]# php-fpm

[[email protected] etc]# netstat -anput|grep php    //查看php是否启动

技术图片

6、编写Nginx服务控制脚本

[[email protected] etc]# vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
PROG_FPM="/usr/local/sbin/php-fpm"
PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"
case "$1" in
        start)
                if [ -f $PIDF ];then
                        echo "Nginx is running.. Start it is error"
                elif [ -f $PIDF_FPM ];then
                        echo "PHP is running.. Start it is error"
                else
                        $PROG &>/dev/null
                        $PROG_FPM &>/dev/null
                fi
        ;;
        stop)
                if [ -f $PIDF ] && [ -f $PIDF_FPM ];then
                        kill -s QUIT $(cat $PIDF) &> /dev/null
                        kill -s QUIT $(cat $PIDF_FPM) &>/dev/null
                else
                        echo "Nginx or PHP is not running.. Stop it is error"
                fi
        ;;
        restart)
                $0 stop
                $0 start
        ;;
        reload)
                kill -s HUP $(cat $PIDF)
        ;;
        *)
                echo "Usage: $0 (start|stop|restart|reload)"
                exit 1
esac
exit 0

[[email protected] ~]# chmod +x /etc/init.d/nginx          //给脚本授予执行权限

[[email protected] ~]# echo "/etc/init.d/nginx start">> /etc/rc.d/rc.local

[[email protected] ~]# chmod +x /etc/rc.d/rc.local

[[email protected] ~]# tail -1 /etc/rc.d/rc.local

/etc/init.d/nginx start

7、测试脚本

[[email protected] ~]# /etc/init.d/nginx start

[[email protected] ~]# netstat -anput|egrep "nginx|php"     //查看是否启动成功

技术图片

 8、配置 Nginx支持 PHP解析

[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

46             index  index.php index.html index.htm;

60         location ~ \.php$ {

61                 root html;

62                 fastcgi_pass 127.0.0.1:9000;

63                 fastcgi_index index.php;

64                 include fastcgi.conf;

65         }

修改完保存并退出!

[[email protected] ~]# 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

[[email protected] ~]# /etc/init.d/nginx reload     //重启脚本

9、测试

[[email protected] ~]# vim /usr/local/nginx/html/index.php

<?php

phpinfo();

?>

10、测试结果如下

技术图片

11、测试通过PHP访问数据库

[[email protected] ~]# vim /usr/local/nginx/html/test.php

[[email protected] ~]# cat /usr/local/nginx/html/test.php

<?php

$link=mysql_connect(‘localhost‘,‘root‘,‘123123‘);

if($link) echo "<h1>successful</h1>";

mysql_close();

?>

12、测试结果如下

如果没出现以下页面就去检查配置文件是否有错误;

技术图片

六、部署SKYUC应用

1、 解压SKYUC,部署程序代码  

[[email protected] ~]# yum -y install unzip     //安装解压缩工具

[[email protected] ~]# rpm -aq unzip

unzip-6.0-15.el7.x86_64

[[email protected] ~]# cd tools/

[[email protected] tools]# unzip SKYUC.v3.4.2.SOURCE.zip

[[email protected] tools]# cd SKYUC.v3.4.2.SOURCE

[[email protected] SKYUC.v3.4.2.SOURCE]# cp -rp wwwroot/ /usr/local/nginx/html/skyuc

[[email protected] SKYUC.v3.4.2.SOURCE]# cd /usr/local/nginx/html/skyuc/

[[email protected] skyuc]# chown -R nginx:nginx ./

2、 创建数据库,并进行授权

[[email protected] ~]# mysql -uroot -p123123

mysql> create database skyucdb;        //创建名为skyucdb的数据库

Query OK, 1 row affected (0.00 sec)

mysql> grant all on skyucdb.* to [email protected] identified by ‘admin123‘;    //创建runskyuc用户密码为admin123

Query OK, 0 rows affected (0.00 sec)

mysql> quit

Bye

3、web界面安装

技术图片

点击下一步:

技术图片

填写数据库信息,设置管理员:

技术图片

安装成功:

技术图片

4、删除install的安装目录:

[[email protected] ~]# cd /usr/local/nginx/html/skyuc/

[[email protected] skyuc]# rm -rf install/

5、登录页面:

技术图片

管理员登录:

技术图片

登录管理界面:

技术图片

实验成功!(* ̄︶ ̄)

 

使用LNMP构建企业web站点

标签:开机自启动   lib   body   安装nginx   htm   type   下载   yum源   index   

原文地址:https://www.cnblogs.com/hmm01031007/p/11569070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!