Centos6.9上搭建lnmp环境

我准备把之前做的www.dy360.net重新搞起来,就在vultr上买了vps,于是乎有了下面这篇文章。
在vps上系统是Centos6.9,至于为什么使用Centos,那是因为我个人比较熟悉Centos!

yum安装Nginx

1.执行下面的yum命令安装Nginx

1
yum install nginx

如果上面命令没有执行成功,说明系统中yum源中没有Nginx的源,因此我们需要手动添加Nginx的源,步骤如下:

1
2
3
4
5
6
7
8
9
10
11
12
vi /etc/yum.repos.d/nginx.repo
输入一下内容:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/6/$basearch/
gpgcheck=0
enabled=1

保存之后执行下面的命令更新源
yum clean all
yum update

2.启动Nginx

1
service nginx start

现在Nginx已经启动了,直接访问服务器就能看到Nginx欢迎页面了的。如果还无法访问,则需配置一下Linux防火墙。
3.配置防火墙,开放80端口

1
2
3
4
5
6
7
8
vim /etc/sysconfig/iptables

-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
#复制上面一行,将22改成80,保存即可
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

#最好重启一下防火墙
service iptables restart

注意:如果是阿里云的ecs,你可能需要在阿里云的主机管理后台,添加安全组规则,方可开启80端口

4.设置开机启动

1
2
chkconfig --list | grep nginx
chkconfig nginx on

END:Nginx安装配置完成


yum 安装MySQL5.6

参考:

https://www.cnblogs.com/007sx/p/7083143.html
1.检查系统是否安装其他版本的MYSQL数据

1
2
yum list installed | grep mysql
yum -y remove mysql-libs.x86_64

2.配置MySQL源

1
2
3
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
yum repolist all | grep mysql

3.安装MYSQL数据库

1
yum install mysql-community-server -y

4.设置为开机启动(2、3、4都是on代表开机自动启动)

1
2
chkconfig --list | grep mysqld
chkconfig mysqld on

5.启动mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 service mysqld start

# 显示如图(部分)

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

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h vultr password 'new-password'

Alternatively you can run:

/usr/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.

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

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

Note: new default config file not created.
Please make sure your config file is current

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

[ OK ]
Starting mysqld: [ OK ]

6.设置mysql密码

1
/usr/bin/mysqladmin -u root password 'xxxxx'

安全处理,看不懂英文直接一路yes就行

1
/usr/bin/mysql_secure_installation

[注意]2017/12/24更新:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
今日安装MySQL执行上面的命令的时候出现下面的话
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

意思是root用户设置密码,之前我们已经设置过了,这个地方输入“no”即可,不然你还要重新设置root用户密码。

7.设置utf-8编码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
查看mysql原本编码:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

编辑mysql的配置文件/etc/my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
vi /etc/my.cnf

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
sql_mode='NO_ENGINE_SUBSTITUTION'

[mysql]
default-character-set = utf8

[mysql.server]
default-character-set = utf8


[mysqld_safe]
default-character-set = utf8


[client]
default-character-set = utf8

#重启mysql
service mysqld restart

#再次查看

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

END:到此MySQL安装配置成功


编译安装PHP7.0.26

参考:

  1. https://segmentfault.com/a/1190000005005068

  2. http://blog.csdn.net/tomspcc/article/details/71080991

1.安装PHP支持库

1
yum install gcc gcc-c++ 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 php-mcrypt

####
2.下载PHP7.0.26源码包

1
wget  http://sg2.php.net/distributions/php-7.0.26.tar.gz

3.编译安装PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
./configure \
--prefix=/usr/local/php \
--exec-prefix=/usr/local/php \
--bindir=/usr/local/php/bin \
--with-fpm-user=www \
--with-fpm-group=www \
--sbindir=/usr/local/php/sbin \
--includedir=/usr/local/php/include \
--libdir=/usr/local/php/lib/php \
--mandir=/usr/local/php/php/man \
--with-config-file-path=/usr/local/php/etc \
--with-mcrypt=/usr/include \
--with-mhash \
--with-openssl \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--without-gdbm \
--disable-fileinfo

编译报下面的错误

1
2
3
checking for stdarg.h... (cached) yes
checking for mcrypt support... yes
configure: error: mcrypt.h not found. Please reinstall libmcrypt.

执行下面的命令安装libmcrypet

1
yum install libmcrypt-devel -y

若上面的方法解决不了,可以编译安装libmcrypet

1
2
3
4
5
6
7
cd /usr/local/src
wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install

上面的方法要是还是不行,那用下面的方法:

1
2
3
4
5
安装第三方yum源
wget http://www.atomicorp.com/installers/atomic
sh ./atomic

yum install libmcrypt-devel -y

4.最后执行下面的命令,完成PHP的安装

1
2
make 
make install

5.配置PHP-FPM

  • 增加用户&用户组

    1
    2
    groupadd www
    useradd -g www www
  • 拷贝php-fpm.conf文件

    1
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  • 拷贝php-fpm include文件

    1
    cp usr/local/php/etc/php-fpm.d/www.conf.default  www.conf
  • 从源码包里拷贝php.ini

    1
    cp /usr/local/src/php-7.0.26/php.ini-production /usr/local/php/etc/php.ini

6.将PHP加入环境变量

1
2
3
4
vi /etc/profile

export PATH=$PATH:/usr/local/php/bin
source /etc/profile

7.php-fpm自启动

  • 从源码包里拷贝启动脚本
    1
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  • 加入系统启动项
    1
    chkconfig php-fpm on

8.重启PHP

1
service php-fpm restart

END:PHP安装配置完成


编译安装git

参考:
http://www.cnblogs.com/fazo/p/5578644.html

1.安装依赖的包

1
yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

2.下载源码包

1
2
3
wget https://github.com/git/git/archive/v2.9.0.zip
unzip v2.9.0.zip
cd git-2.9.0

3.编译安装

1
2
make prefix=/usr/local/git all
make prefix=/usr/local/git install

4.加入系统变量

1
2
vi /etc/profile  
export PATH=/usr/local/git/bin:$PATH

5.配置git

1
2
3
git config --global user.name 'vultr'
git config --global user.email 'vultr@dy360.net' 'vultr@dy360.net'
git config --global credential.helper store #保存密码

6.生成ssh密钥,执行下面的命令,一路回车即可,即可用密钥克隆项目。

1
ssh-keygen -t rsa -C 'vultr@dy360.net'

END:git安装配置成功


最后,终于环境搭建好了!自己搭建lnmp环境少说也有十几次了,可是每次都有新感觉!因为每次出现的问题,都不太一样!最近两次都出现了Nginx安装后,通过ip不能访问的问题,结果发现都是防火墙的问题,没有开启80端口。坑的要命!

  • 作者: Sam
  • 发布时间: 2017-11-29 23:17:13
  • 最后更新: 2020-12-06 22:01:31
  • 文章链接: https://ydstudios.gitee.io/post/123335f3.html
  • 版权声明: 本网所有文章除特别声明外, 禁止未经授权转载,违者依法追究相关法律责任!