1. 养乐多|杨城的个人博客首页
  2. 技术杂谈
  3. Other

CentOS 6.8编译安装LNMP

昨天有人在群里问到了LNMP安装,于是博主把去年的相关笔记翻出来看了下,思考过后认为将其通过文章形式发布出来,既能温故而知新,又能利于有需要的人参考。
一. 安装Nginx
1. 检查依赖包是否有安装

[root@centos110 yangcheng]# rpm -qa pcre pcre-devel openssl openssl-devel
# 若没有安装,则先安装。安装Pcre库,使用Ng具备支持URL重写功能的rewrite模块
[root@centos110 yangcheng]# yum install -y pcre pcre-devel
# 安装openssl-devel,Ng在使用HTTPS服务的时候要用到此模块,如果不安装openssl相关包,安装Ng的过程中会报错
[root@centos110 yangcheng]# yum install -y openssl openssl-devel

2. 开始安装nginx

[root@centos110 yangcheng]# mkdir -p /home/yangcheng/tools
[root@centos110 yangcheng]# cd /home/yangcheng/tools/
[root@centos110 tools]# wget -q http://nginx.org/download/nginx-1.10.1.tar.gz
[root@centos110 tools]# ls -l nginx-1.10.1.tar.gz
[root@centos110 tools]# useradd nginx -s /sbin/nologin -M
[root@centos110 tools]# tar xf nginx-1.10.1.tar.gz
[root@centos110 tools]# cd nginx-1.10.1
[root@centos110 nginx-1.10.1]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.10.1 --with-http_stub_status_module --with-http_ssl_module
[root@centos110 nginx-1.10.1]# make
[root@centos110 nginx-1.10.1]# make install
[root@centos110 nginx-1.10.1]# ln -s /application/nginx-1.10.1 /application/nginx # 做个软连接,便于日后维护升级切换服务目录
# 启动nginx服务
[root@centos110 nginx-1.10.1]# ./application/nginx/sbin/nginx

# 启动成功后,可通过查看nginx服务对应的端口确认是否成功启动,可输入命令:lsof -i :80 或 netstat -lnt|grep 80 均可查看端口情况
# 检查nginx启动的实际效果,可以打开浏览器输入IP地址回车后会显示ng的欢迎信息页面:Welcome to nginx!
# 也可通过wget或curl命令检测

[root@centos110 yangcheng]# wget 192.168.245.110
--2017-08-17 22:37:52--  http://192.168.245.110/
Connecting to 192.168.245.110:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `index.html'

    [ <=>                           ] 90,026      --.-K/s   in 0.02s  

2017-08-17 22:37:52 (4.76 MB/s) - `index.html'
saved [90026]

3. 设置开机自启动
略…
二. 安装MySQL
1. 创建MySQL组及用户账号

[root@centos110 tools]# groupadd mysql
[root@centos110 tools]# useradd -s /sbin/nologin -g mysql -M mysql

2. 下载MySQL安装包(二进制安装包,仅需解压即可,不需要编译)

[root@centos110 tools]# wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.51-linux2.6-x86_64.tar.gz

3. 采用二进制方式安装MySQL
1)解压并移动MySQL二进制软件包到指定的安装路径
2)创建软链接,生成去掉版本号的访问路径并查看

[root@centos110 tools]# tar xf mysql-5.5.51-linux2.6-x86_64.tar.gz
[root@centos110 tools]# mv mysql-5.5.51-linux2.6-x86_64 /application/mysql-5.5.51
[root@centos110 tools]# ln -s /application/mysql-5.5.51 /application/mysql

4. 初始化MySQL配置文件my.cnf,路径/application/mysql/support-files

[root@centos110 support-files]# cp my-small.cnf /etc/my.cnf
[root@centos110 support-files]# ls  
binary-configure  config.medium.ini  magic        my-innodb-heavy-4G.cnf  my-medium.cnf  mysql-log-rotate  mysqld_multi.server
config.huge.ini   config.small.ini   my-huge.cnf  my-large.cnf            my-small.cnf   mysql.server      ndb-config-2-node.ini

本例为测试安装环境,因此选择参数配置小的my-small.cnf配置模板,如果是生产环境可以根据硬件选择更高级的配置文件,上述配置文件模板对硬件的要求从低到高依次为:
my-medium.cnf==>my-small.cnf==>my-large.cnf==>my-huge.cnf==>my-innodb-heavy-4G.cnf
5. 初始化MySQL数据库文件

[root@centosll0 mysql]# mkdir -p /application/mysql/data
[root@centosll0 mysql]# chown -R mysql.mysql /application/mysql
[root@centosll0 mysql]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data/ --user=mysql
Installing MySQL system tables...
160824 14:30:13 [Note] /application/mysql/bin/mysqld (mysqld 5.5.51) starting as process 3777
OK
Filling help tables...
160824 14:30:14 [Note] /application/mysql/bin/mysqld (mysqld 5.5.51) starting as process 3784
OK

To start mysqld at boot time you have to copy
support-files/mysql. server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/application/mysql/bin/mysqladmin -u root password 'new-password'
/application/mysql/bin/mysqladmin -u root -h centos110 password 'new-password'

Alternatively you can run:
/ application/ mysql/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /application/mysql ; /application/mysql/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /application/mysql/mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

# 以上命令的主要作用是生成了如下数据库文件:(信息不完整,省略若干)

[root@centos110 mysql]# tree /application/mysql/data/
/application/mysql/data/
|-- centos110.err
|-- centos110.pid
|-- ib_logfile0
|-- ib_logfile1
|-- ibdata1
|-- lepus
|   |-- admin_log.frm
|   |-- admin_menu.frm
|   |-- admin_privilege.frm
|   |-- admin_role.frm
|   |-- admin_role_privilege.frm
|   |-- admin_user.frm
|   |-- admin_user_role.frm
|   |-- alarm.frm
|   |-- alarm_history.frm
|   |-- alarm_temp.frm
|   |-- db.opt
|   |-- db_servers_mongodb.frm
|   |-- db_servers_mysql.frm
|   |-- db_servers_oracle.frm
|   |-- db_servers_os.frm
|   |-- db_servers_redis.frm
|   |-- db_servers_sqlserver.frm
# 这些MySQL数据库文件是MySQL正确运行所必需的基本数据库文件,其功能是对MySQL权限、状态等进行管理。

6. 配置并启动MySQL数据库
1)设置MySQL启动脚本,命令如下:

[root@centosll0 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centosll0 mysql]# chmod -x /etc/init.d/mysqld
# 给脚本设置可执行权限
[root@centosll0 init.d]# chmod a+x mysqld

2)MySQL二进制默认安装路径是/usr/local/mysql,启动脚本里面是/usr/local/mysql的路径都需要替换,命令如下:

[root@centosll0 mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

3) 启动MySQL数据库,命令如下:

[root@centosll0 init.d]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
# 参数有start stop 或直接执行killall mysqld结束MySQL进程

4)检查MySQL数据库是否启动,命令如下:

[root@centos110 init.d]# netstat -lntup|grep mysql
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2363/mysqld

5)查看MySQL数据库启动结果日志,命令如下:

[root@centos110 init.d]# tail -10 /application/mysql/data/centos110.err
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
170327 22:27:04  InnoDB: Waiting for the background threads to start
170327 22:27:05 InnoDB: 5.5.51 started; log sequence number 40843869
170327 22:27:05 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
170327 22:27:05 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
170327 22:27:05 [Note] Server socket created on IP: '0.0.0.0'.
170327 22:27:06 [Note] Event Scheduler: Loaded 0 events
170327 22:27:06 [Note] /application/mysql/bin/mysqld: ready for connections.
Version: '5.5.51'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

6)设置MySQL开机自启动,命令如下:

[root@centos110 init.d]# chkconfig --add mysqld
[root@centos110 init.d]# chkconfig mysqld on
[root@centos110 init.d]# chkconfig --list mysqld # 列出mysqld服务设置情况,设定mysqld在等级3和5为开机运行服务,on表示启动,off表示关闭
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

7)配置MySQL命令的全局使用路径,命令如下:

[root@centoslld init.d]# echo 'export PATH=/app1ication/mysql/bin:$PATH' >>/etc/profile
[root@centoslld init.d]# tail -1 /etc/profile
export PATH=/app1ication/mysql/bin:$PATH
[root@centos110 init.d]# source /etc/profile
# 执行source 使上一行添加到/etc/profile的内容直接生效,以上两条命令的用途为定义MySQL全局路径,实现在任意路径执行MySQL命令。
[root@centos110 init.d]# echo $PATH # 查看设置的结果
/application/mysql/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/yangcheng/bin

8)登录MySQL测试,命令如下:

[root@centosll0 init.d]# mysql
Welcome to the MySQL monitor. Commands end with ; \g
Your MySQL connection id is 3
Server version: 5.5.51 MySQL Community Server (CPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

7. MySQL安全配置
1)为MySQL的root用户设置密码,命令如下:

[root@centoslld init.d]# mysqladmin -u root password 'yangcheng'
[root@centoslld init.d]# mysql   # 现在已经无法直接进入数据库了
ERROR 1045 (28000): Access denied for user ' root'@'localhost' (using password:NO)
[root@centos110 init.d]# mysql -uroot -p  # 需要这样进入
Enter password :  # 输入刚刚设置的密码
Welcome to the MySQL monitor. Commands end with ; \g
Your MySQL connection id is 6
Server version: 5.5.51 MySQL Community Server (CPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

2)清除无用的MySQL用户及库,命令如下:

mysql> drop user "root"@"::1";
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ""@"::localhost";
Query OK, 0 rows affected (0.00 sec)
mysql> drop user "root"@"::centos110";
Query OK, 0 rows affected (0.00 sec)
# 删除无用的数据库
mysql> drop database test;
Query OK, 0 rows affected (0.08 sec)

8. 设置远程访问权限

# 允许root用户使用密码从任意主机访问MySQL服务,则进入mysql命令行执行如下命令:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yangcheng' WITH GRANT OPTION;
# 若想指定主机远程链接MySQL服务,则执行如下命令:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.245.200' IDENTIFIED BY 'yangcheng' WITH GRANT OPTION;

OK,MySQL整个安装过程,到这一步就很好了,后面可以更深入了解相关技术。
三. LNMP之PHP(FastCGI方式)服务的安装准备
1. 检查Nginx和MySQL的安装情况(略)
2. 检查安装PHP所需的lib库,命令如下:

[root@centos110 tools]# rpm -qa zlib-devel libxm12-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
# 执行下面的命令安装相关lib包:
[root@centos110 tools]# yum install zlib-devel libxm12-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel -y

3. 安装libmcrypt库

# centos默认yum源里面没有该libmcrypt-devel,因此需要事先配置epel第三方yum源,命令:
[root@centos110 tools]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
# 执行完后,再进行yum安装,命令:
[root@centos110 tools]# yum -y install libmcrypt-devel

4. 安装mhash加密扩展库
使用yum安装,需要事先配置epel第三方yum源,上一步骤中已经安装过了,因此可以直接yum安装

[root@centos110 tools]# yum -y install mhash

5. 安装mcrypt加密扩展库

[root@centos110 tools]# yum -y install mcrypt

6. 开始安装PHP(FastCGI方式)服务
1)获取PHP软件包,此处使用的是PHP7版本。去官网 http://php.net/downloads.php 下载好后,通过FTP工具上传至服务器相关目录。解压安装包,并编译PHP,命令如下:(扩展模块参数可选)

[root@centos110 php-7.0.10]# ./configure --prefix=/application/php-7.0.10 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=/application/mysql --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp
[root@centos110 php-7.0.10]# ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[root@centos110 php-7.0.10]# touch ext/phar/phar.phar
[root@centos110 php-7.0.10]# make
[root@centos110 php-7.0.10]# make install
[root@centos110 php-7.0.10]# ln -s /application/php-7.0.10 /application/php

7. 配置PHP引擎配置文件php.ini

[root@centos110 php-7.0.10]# ls php.ini*  # 查看php.ini配置文件
php.ini-development  php.ini-production
# 可以对比两个文件,开发环境更多的是开启日志,调式信息,而生产环境都是关闭状态。
# 拷贝PHP配置文件到PHP默认目录,并更改文件名称为php.ini,命令如下:
[root@centos110 php-7.0.10]# cp php.ini-production /application/php/lib/php.ini

8. 配置PHP服务(FastCGI方式)的配置文件php-fpm.conf,命令如下:

[root@centos110 php-7.0.10]# cd /application/php/etc/
[root@centos110 etc]# cp php-fpm.conf.default php-fpm.conf
# 关于php-fpm.conf,暂时可用默认的配置,先把服务搭好,后面会对php-fpm.conf进行优化。

9. 启动PHP服务(FastCGI方式)

# 将/application/php/etc/php-fpm.d 下面的 www.conf.default 复制一份取名为www.conf,暂用默认配置
[root@centos110 php-fpm.d]# cp www.conf.default www.conf
# 启动PHP服务:
[root@centos110 php-fpm.d]# /application/php/sbin/php-fpm
# 检查服务进程及端口启动情况
[root@centos110 etc]# ps -ef|grep php-fpm

10. 配置Nginx支持PHP程序访问

[root@centos110 etc]# cd /application/nginx/conf/extra
[root@centos110 extra]# vim www.conf
server {
        listen       80;
        server_name  192.168.245.110;
        location / {
            root   html/www;
            index  index.php index.html index.htm;
        }
        access_log logs/access_www.log main gzip buffer=32k flush=5s;
        location ~ .*\.(php|php5)?$ {
             root html/www;
             fastcgi_pass 127.0.0.1:9000;
             fastcgi_index index.php;
             include fastcgi.conf;
        }
    }
# 检查配置是否正确
[root@centos110 etc]# cd /application/nginx/sbin
[root@centos110 sbin]# ./nginx -t
nginx: the configuration file /application/nginx-1.10.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.1/conf/nginx.conf test is successful
[root@centos110 sbin]# ./nginx -s reload

11. 测试LNMP环境生效的情况

# 进入指定的默认站点目录,新增index.php文件,内容为<?php phpinfo();
[root@centos110 nginx]# cd html/www/
[root@centos110 www]# vim index.php
<?php phpinfo();
# 保存退出即可
# 浏览器访问 192.168.245.110 即可看到PHP相关信息....

整个过程,至此结束,谢谢!

原创文章,作者:iConan,如若转载,请注明出处:https://www.aspyc.com/archives/434.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据