LAMP环境在CentOS 7下的安装笔记

发表于 2018-07-24
阅读 234

准备工作

安装wget

如果环境里没有wget,通过yum安装一下

yum -y install wget

安装gcc

如果环境里没有编译工具,通过yum安装一下

yum -y install gcc gcc-c++ make

安装依赖包

yum -y install expat-devel pcre-devel zlib-devel perl libxml* curl-devel freetype-devel libaio numactl-libs libevent-devel openssl openssl-devel autoconf unzip

建立环境根目录

mkdir -p /tongfu.net/env/

建立安装包目录并进入

mkdir /packages
cd /packages

安装Apache 2.4

准备

apr-1.6.5.tar.gz
apr-util-1.6.1.tar.gz
httpd-2.4.37.tar.gz

下载安装包

wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz
wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.37.tar.gz

如果以上下载地址失效了,请自行到官网下载,下载地址是 http://httpd.apache.org/download.cgi#apache24

打开页面后找到 Mirror,进去之后选 cn(中国镜像肯定快啊),然后随便选一个镜像服务器

进入镜像服务器目录后,在 httpd 目录下载 httpd 安装包,在 apr 目录下载 apr 和 apr-util 安装包

如果安装包的版本更新了,大家不要惊慌,只要大版本(第一位数字)没有变化就没关系。

大家需要在后面的安装命令里把对应的版本号改成新版本的就好!

例如:apr-1.6.3.tar.gz 更新到 apr-1.6.5.tar.gz 了,那么在本教程内所有出现 apr-1.6.3 字样的地方都要改成 apr-1.6.5

注意:如果其他软件的安装包也更新了,同样采用以上方法解决即可!

安装apr

tar xzvf apr-1.6.5.tar.gz
cd apr-1.6.5
./configure --prefix=/tongfu.net/env/apr-1.6.5
make && make install
cd ..

安装apr-util

tar xzvf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1
./configure --prefix=/tongfu.net/env/apr-util-1.6.1 \
--with-apr=/tongfu.net/env/apr-1.6.5/bin/apr-1-config 
make && make install
cd ..

安装httpd

tar xzvf httpd-2.4.37.tar.gz
cd httpd-2.4.37
./configure --prefix=/tongfu.net/env/httpd-2.4.37 \
--with-apr=/tongfu.net/env/apr-1.6.5 \
--with-apr-util=/tongfu.net/env/apr-util-1.6.1 \
--with-pcre \
--enable-so \
--enable-rewrite \
--enable-ssl
make && make install
cd ..

初始化

打开 httpd.conf 配置文件

将 ServerAdmin 设置为 webmaster@tongfu.net

将 ServerName 设置为 localhost

将 Listen 设置为 8080

[root@tongfunet]# vi /tongfu.net/env/httpd-2.4.37/conf/httpd.conf

ServerAdmin webmaster@tongfu.net

ServerName localhost

Listen 8080

注意:为什么我们要把Apache的端口设置为 8080 呢?因为后面我们会把 80 端口让给 nginx 代理服务器使用

启动

/tongfu.net/env/httpd-2.4.37/bin/apachectl start

测试

[root@tongfunet]# curl 'http://localhost:8080/'
<html><body><h1>It works!</h1></body></html>

自动启动

添加系统服务脚本

[root@tongfunet]# cat > /lib/systemd/system/httpd.service <<EOF
[Unit]
Description=httpd
After=network.target

[Service]
Type=forking
ExecStart=/tongfu.net/env/httpd-2.4.37/bin/apachectl start
ExecReload=/tongfu.net/env/httpd-2.4.37/bin/apachectl restart
ExecStop=/tongfu.net/env/httpd-2.4.37/bin/apachectl stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

使用 systemctl 管理 httpd 服务

systemctl enable httpd # 设置自动启动

systemctl start httpd # 启动服务

systemctl stop httpd # 停止服务

systemctl restart httpd # 重启服务

安装Nginx

准备

nginx-1.12.2.tar.gz

下载安装包

wget http://nginx.org/download/nginx-1.12.2.tar.gz

安装nginx

tar xzvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/tongfu.net/env/nginx-1.12.2/ \
--with-http_ssl_module
make && make install
cd ..

启动

/tongfu.net/env/nginx-1.12.2/sbin/nginx

测试

[root@tongfunet]# curl 'http://localhost/'
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

映射Apache

通过下面的配置,我们将前面安装好的Apache服务的8080端口通过Nginx的80端口发布出来

在 http {} 内的默认 server {} 前面增加如下配置

[root@tongfunet]# vi /tongfu.net/env/nginx-1.12.2/conf/nginx.conf
    include upstreams.conf;
    include servers.conf;

建立upstreams.conf文件

[root@tongfunet]# cat > /tongfu.net/env/nginx-1.12.2/conf/upstreams.conf <<EOF
upstream apache {
    server 127.0.0.1:8080;
}
EOF

建立servers.conf文件

[root@tongfunet]# cat > /tongfu.net/env/nginx-1.12.2/conf/servers.conf <<EOF
server {
    listen       80;
    server_name  localhost;
    location / {
        proxy_pass              http://apache;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-Ip \$remote_addr;
        proxy_set_header X-Forwarded-For \$remote_addr;
    }
}
EOF

并且需要把默认的 server {} 全部删除掉

重新启动

/tongfu.net/env/nginx-1.12.2/sbin/nginx -s reload

重新测试

可以看到通过Nginx的80端口可以访问到Apache的8080端口上面的网页内容了

[root@tongfunet]# curl 'http://localhost/'
<html><body><h1>It works!</h1></body></html>

自动启动

添加系统服务脚本

[root@tongfunet]# cat > /lib/systemd/system/nginxd.service <<EOF
[Unit]
Description=nginxd
After=network.target

[Service]
Type=forking
ExecStart=/tongfu.net/env/nginx-1.12.2/sbin/nginx
ExecReload=/tongfu.net/env/nginx-1.12.2/sbin/nginx -s reload
ExecStop=/tongfu.net/env/nginx-1.12.2/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

手动停止当前运行着的 nginx 服务,否则自动启动脚本会报错

/tongfu.net/env/nginx-1.12.2/sbin/nginx -s quit

使用 systemctl 管理 nginxd 服务

systemctl enable nginxd # 设置自动启动

systemctl start nginxd # 启动服务

systemctl stop nginxd # 停止服务

systemctl restart nginxd # 重启服务

安装PHP 7

准备

jpegsrc.v9c.tar.gz
libpng-1.6.34.tar.gz
php-7.2.8.tar.gz

下载安装包

wget http://www.ijg.org/files/jpegsrc.v9c.tar.gz
wget http://jaist.dl.sourceforge.net/project/libpng/libpng16/1.6.34/libpng-1.6.34.tar.gz
wget http://cn2.php.net/get/php-7.2.8.tar.gz/from/this/mirror -O php-7.2.8.tar.gz

安装jpegsrc

tar xzvf jpegsrc.v9c.tar.gz
cd jpeg-9c
./configure --prefix=/tongfu.net/env/jpeg-9c
make && make install
cd ..

安装libpng

tar xzvf libpng-1.6.34.tar.gz
cd libpng-1.6.34
./configure --prefix=/tongfu.net/env/libpng-1.6.34
make && make install
cd ..

安装php

tar -xzvf php-7.2.8.tar.gz
cd php-7.2.8
./configure --prefix=/tongfu.net/env/php-7.2.8 \
--with-apxs2=/tongfu.net/env/httpd-2.4.37/bin/apxs \
--enable-mbstring \
--enable-exif \
--enable-soap \
--with-curl \
--with-gd \
--with-jpeg-dir=/tongfu.net/env/jpeg-9c \
--with-png-dir=/tongfu.net/env/libpng-1.6.34 \
--with-freetype-dir \
--enable-fpm \
--enable-mysqlnd \
--with-pdo-mysql \
--with-config-file-path=/tongfu.net/env/php-7.2.8/etc \
--with-mysqli
make && make install
cp php.ini-* /tongfu.net/env/php-7.2.8/etc/
cp php.ini-development /tongfu.net/env/php-7.2.8/etc/php.ini
cd ..

初始化

搜索 DirectoryIndex,在最后添加 index.php

搜索 AddType application/x-gzip .gz .tgz,在后面添加

[root@tongfunet]# vi /tongfu.net/env/httpd-2.4.37/conf/httpd.conf

DirectoryIndex index.html index.htm index.php

AddType application/x-httpd-php .php

 重启Apache

systemctl restart httpd

安装错误解决(1)

在make命令时提示一堆undefined reference to `libiconv_open'的错误,搜了下网上找到解决办法,安装libiconv就好了。

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
tar -xzvf php-5.6.37.tar.gz
cd libiconv-1.15
./configure --prefix=/tongfu.net/env/libiconv-1.15/
make && make install
cd ..

安装完了libiconv重新编译php,增加以下编译参数

--with-iconv-dir=/tongfu.net/env/libiconv-1.15/

编译完了make的时候增加参数

make ZEND_EXTRA_LIBS='-liconv'

这样就可以编译通过了!

测试

按下面的操作执行后,应该会得到 phpinfo 打印的信息

[root@tongfunet]# cat > /tongfu.net/env/httpd-2.4.37/htdocs/test.php <<EOF
<?php phpinfo();
EOF
[root@tongfunet]# curl -s "http://localhost/test.php" | grep "PHP API"
<tr><td class="e">PHP API </td><td class="v">20170718 </td></tr>

如果没有正确返回信息的话,可以使用先反复 stop 直到提示“not running”后再 start 的方式重启Apache

安装redis扩展

安装redis扩展

wget http://pecl.php.net/get/redis-4.1.0.tgz
tar -xzvf redis-4.1.0.tgz
cd redis-4.1.0
/tongfu.net/env/php-7.2.8/bin/phpize
./configure --with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config
make && make install
cd ..

配置redis扩展

找到 extension 位置,添加redis

[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini

extension=redis

重启Apache

systemctl restart httpd

测试

[root@tongfunet]# curl -s "http://localhost/test.php" | grep redis
<h2><a name="module_redis">redis</a></h2>
<tr><td class="e">Registered save handlers </td><td class="v">files user redis rediscluster  </td></tr>
This program is free software; you can redistribute it and/or modify it under the terms of the PHP License as published by the PHP Group and included in the distribution in the file:  LICENSE

安装libmemcached库

安装libmemcached库

wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -xzvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/tongfu.net/env/libmemcached-1.0.18 \
--with-memcached=/tongfu.net/env/memcached-1.5.10/
make && make install
cd ..

安装memcached扩展

安装memcached扩展

wget http://pecl.php.net/get/memcached-3.0.4.tgz
tar -xzvf memcached-3.0.4.tgz
cd memcached-3.0.4
/tongfu.net/env/php-7.2.8/bin/phpize
./configure --with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config \
--with-libmemcached-dir=/tongfu.net/env/libmemcached-1.0.18/ \
--disable-memcached-sasl
make && make install
cd ..

配置mecached扩展

找到 extension 位置,添加memcached

[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini

extension=memcached

重启Apache

systemctl restart httpd

测试

[root@tongfunet]# curl -s "http://localhost/test.php" | grep memcached
<h2><a name="module_memcached">memcached</a></h2>
<tr class="h"><th>memcached support</th><th>enabled</th></tr>
<tr><td class="e">libmemcached version </td><td class="v">1.0.18 </td></tr>
<tr><td class="e">memcached.compression_factor</td><td class="v">1.3</td><td class="v">1.3</td></tr>
<tr><td class="e">memcached.compression_threshold</td><td class="v">2000</td><td class="v">2000</td></tr>
<tr><td class="e">memcached.compression_type</td><td class="v">fastlz</td><td class="v">fastlz</td></tr>
<tr><td class="e">memcached.default_binary_protocol</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.default_connect_timeout</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.default_consistent_hash</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.serializer</td><td class="v">php</td><td class="v">php</td></tr>
<tr><td class="e">memcached.sess_binary_protocol</td><td class="v">1</td><td class="v">1</td></tr>
<tr><td class="e">memcached.sess_connect_timeout</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.sess_consistent_hash</td><td class="v">1</td><td class="v">1</td></tr>
<tr><td class="e">memcached.sess_lock_expire</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.sess_lock_max_wait</td><td class="v">not&nbsp;set</td><td class="v">not&nbsp;set</td></tr>
<tr><td class="e">memcached.sess_lock_retries</td><td class="v">5</td><td class="v">5</td></tr>
<tr><td class="e">memcached.sess_lock_wait</td><td class="v">not&nbsp;set</td><td class="v">not&nbsp;set</td></tr>
<tr><td class="e">memcached.sess_lock_wait_max</td><td class="v">2000</td><td class="v">2000</td></tr>
<tr><td class="e">memcached.sess_lock_wait_min</td><td class="v">1000</td><td class="v">1000</td></tr>
<tr><td class="e">memcached.sess_locking</td><td class="v">1</td><td class="v">1</td></tr>
<tr><td class="e">memcached.sess_number_of_replicas</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.sess_persistent</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.sess_prefix</td><td class="v">memc.sess.</td><td class="v">memc.sess.</td></tr>
<tr><td class="e">memcached.sess_randomize_replica_read</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.sess_remove_failed_servers</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.sess_sasl_password</td><td class="v"><i>no value</i></td><td class="v"><i>no value</i></td></tr>
<tr><td class="e">memcached.sess_sasl_username</td><td class="v"><i>no value</i></td><td class="v"><i>no value</i></td></tr>
<tr><td class="e">memcached.sess_server_failure_limit</td><td class="v">0</td><td class="v">0</td></tr>
<tr><td class="e">memcached.store_retry_count</td><td class="v">2</td><td class="v">2</td></tr>
<tr><td class="e">Registered save handlers </td><td class="v">files user redis rediscluster memcached  </td></tr>

安装imap扩展

安装imap扩展

yum -y install libc-client
ln -s /usr/lib64/libc-client.so /usr/lib/libc-client.so
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/l/libc-client-2007f-16.el7.x86_64.rpm
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/u/uw-imap-2007f-16.el7.x86_64.rpm
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/u/uw-imap-devel-2007f-16.el7.x86_64.rpm
rpm -ivh libc-client-2007f-16.el7.x86_64.rpm
rpm -ivh uw-imap-2007f-16.el7.x86_64.rpm
rpm -ivh uw-imap-devel-2007f-16.el7.x86_64.rpm
cd php-7.2.8/ext/imap
/tongfu.net/env/php-7.2.8/bin/phpize
./configure --with-imap \
--with-imap-ssl \
--with-kerberos \
--with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config
make && make install
cd ../../..

配置imap扩展

找到 extension 位置,添加imap

[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini

extension=imap

重启Apache

systemctl restart httpd

测试

[root@tongfunet]# curl -s "http://localhost/test.php" | grep imap
<tr><td class="e">Protocols </td><td class="v">dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp </td></tr>
<h2><a name="module_imap">imap</a></h2>

安装zlib扩展

安装zlib扩展

cd php-7.2.8/ext/zlib/
mv config0.m4 config.m4
/tongfu.net/env/php-7.2.8/bin/phpize
./configure --with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config
make && make install
cd ../../

配置zlib扩展

找到 extension 位置,添加zlib

[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini

extension=zlib

重启Apache

systemctl restart httpd

测试

[root@tongfunet]# curl -s "http://localhost/test.php" | grep zlib
<tr><td class="e">Registered PHP Streams</td><td class="v">php, file, glob, data, http, ftp, compress.zlib, phar</td></tr>
<tr><td class="e">Registered Stream Filters</td><td class="v">convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, zlib.*</td></tr>
<h2><a name="module_zlib">zlib</a></h2>
<tr><td class="e">Stream Wrapper </td><td class="v">compress.zlib:// </td></tr>
<tr><td class="e">Stream Filter </td><td class="v">zlib.inflate, zlib.deflate </td></tr>
<tr><td class="e">zlib.output_compression</td><td class="v">Off</td><td class="v">Off</td></tr>
<tr><td class="e">zlib.output_compression_level</td><td class="v">-1</td><td class="v">-1</td></tr>
<tr><td class="e">zlib.output_handler</td><td class="v"><i>no value</i></td><td class="v"><i>no value</i></td></tr>

安装MySQL 5.7

准备

mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

下载安装包

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

创建账号

useradd mysql

安装mysql

tar xzvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /tongfu.net/env/
cd /tongfu.net/env/
mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql-5.7.22

初始化

mkdir mysql-5.7.22/data/
chown -R mysql.mysql mysql-5.7.22/
./mysql-5.7.22/bin/mysqld --initialize --user=mysql --basedir=/tongfu.net/env/mysql-5.7.22/ --datadir=/tongfu.net/env/mysql-5.7.22/data/

注意:执行完上面的命令后,会有初始化密码打印出来,一定记得保存起来

2018-07-25T23:47:56.430639Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-25T23:47:57.762501Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-07-25T23:47:57.891333Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-25T23:47:58.002282Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 287aa3d9-9065-11e8-93c5-02420a100164.
2018-07-25T23:47:58.003611Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-07-25T23:47:58.004209Z 1 [Note] A temporary password is generated for root@localhost: [初始化密码]

启动

修改启动脚本

[root@tongfunet]# vi ./mysql-5.7.22/support-files/mysql.server

basedir=/tongfu.net/env/mysql-5.7.22/

datadir=/tongfu.net/env/mysql-5.7.22/data/

启动服务

./mysql-5.7.22/support-files/mysql.server start

修改默认密码

使用前面得到的初始化密码修改root密码

./mysql-5.7.22/bin/mysqladmin -uroot -p password "abcdef"

登录

./mysql-5.7.22/bin/mysql -uroot -pabcdef

自动启动

添加自动启动脚本

[root@tongfunet]# cat > /lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=mysqld
After=network.target

[Service]
Type=forking
ExecStart=/tongfu.net/env/mysql-5.7.22/support-files/mysql.server start
ExecReload=/tongfu.net/env/mysql-5.7.22/support-files/mysql.server restart
ExecStop=/tongfu.net/env/mysql-5.7.22/support-files/mysql.server stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

使用 systemctl 管理 mysqld 服务

systemctl enable mysqld # 设置自动启动

systemctl start mysqld # 启动服务

systemctl stop mysqld # 停止服务

systemctl restart mysqld # 重启服务

常见错误1

在新版本的mysql里是找不到my.cnf配置文件的,因为mysql把大部分参数都设置了默认值,原则上我们不需要设置太多参数

如果想要设置的话,可以从旧版本的mysql环境下复制过来,放到mysql的安装目录下

当然,你也可以像以前那样放到 /etc/my.cnf,不过,这样不利用管理和迁移

常见错误2

在新版本的mysql里使用group by语句的时候有了限制,简单说就是group by的字段必须在select内出现,这样的话select * from xxx group by yyy就行不通了。

which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决方法,在my.cnf里增加一行设置即可

sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

这样就可以随便写了~

安装Memcached

准备

memcached-1.5.10.tar.gz

现在安装包

wget http://memcached.org/files/memcached-1.5.10.tar.gz

安装memcached

tar -xzvf memcached-1.5.10.tar.gz
cd memcached-1.5.10
./configure --prefix=/tongfu.net/env/memcached-1.5.10
make && make install
cd ..

启动脚本

[root@tongfunet]# cat > /tongfu.net/env/memcached-1.5.10/bin/memcached-start <<EOF
#!/bin/sh

# env
export PATH

# argv
memcacheroot=/tongfu.net/env/memcached-1.5.10
user=daemon
memory=128m

# start
\$memcacheroot/bin/memcached -u \$user -m \$memory -d
EOF

停止脚本

编写停止脚本

[root@tongfunet]# cat > /tongfu.net/env/memcached-1.5.10/bin/memcached-quit <<EOF
#!/bin/sh

# env
export PATH

# argv
pid="\`/bin/ps -ef | grep 'memcached-1.5.10' | grep -v 'quit' | grep -v 'grep' | awk '{print \$2}'\`"

# quit
if [ ! "" = "\$pid" ] ; then
    /bin/kill \$pid
fi
EOF

授权脚本

chmod 0755 /tongfu.net/env/memcached-1.5.10/bin/memcached-start
chmod 0755 /tongfu.net/env/memcached-1.5.10/bin/memcached-quit

自动启动

添加自动启动脚本

[root@tongfunet]# cat > /lib/systemd/system/memcached.service <<EOF
[Unit]
Description=memcached
After=network.target

[Service]
Type=forking
ExecStart=/tongfu.net/env/memcached-1.5.10/bin/memcached-start
ExecStop=/tongfu.net/env/memcached-1.5.10/bin/memcached-quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

使用 systemctl 管理 memcached 服务

systemctl enable memcached # 设置自动启动

systemctl start memcached # 启动服务

systemctl stop memcached # 停止服务

systemctl restart memcached # 重启服务

安装Redis 4

准备

redis-4.0.10.tar.gz

下载安装包

wget http://download.redis.io/releases/redis-4.0.10.tar.gz

安装redis

tar -xzvf redis-4.0.10.tar.gz
cd redis-4.0.10
make
cd src
make install
mkdir -p /tongfu.net/env/redis-4.0.10/
mkdir -p /tongfu.net/env/redis-4.0.10/bin/
mkdir -p /tongfu.net/env/redis-4.0.10/conf/
cp redis-server /tongfu.net/env/redis-4.0.10/bin/
cp redis-cli /tongfu.net/env/redis-4.0.10/bin/
cd ..
cp redis.conf /tongfu.net/env/redis-4.0.10/conf/
cd ..

修改配置文件

修改配置文件为daemon启动方式

增加bind 0.0.0.0,允许通过本地所有IP访问

设置初始密码为tongfu.net

[root@tongfunet]# vi /tongfu.net/env/redis-4.0.10/conf/redis.conf

daemonize yes

bind 0.0.0.0

requirepass tongfu.net

启动脚本

编写启动脚本

[root@tongfunet]# cat > /tongfu.net/env/redis-4.0.10/bin/redis-start <<EOF
#!/bin/sh

# env
export PATH

# argv
redisroot=/tongfu.net/env/redis-4.0.10

# start
\$redisroot/bin/redis-server \$redisroot/conf/redis.conf
EOF

停止脚本

编写停止脚本

[root@tongfunet]# cat > /tongfu.net/env/redis-4.0.10/bin/redis-quit <<EOF
#!/bin/sh

# env
export PATH

# argv
pidfile=/var/run/redis_6379.pid

# quit
pid="\`cat \${pidfile}\`"
if [ "" = "\`ps -ax|awk '{print \$1}'|grep -e "^\${pid}\$"\`" ] ; then
    /bin/rm -f \${pidfile}
else
    /bin/kill \$pid
fi
EOF

授权脚本

chmod 0755 /tongfu.net/env/redis-4.0.10/bin/redis-start
chmod 0755 /tongfu.net/env/redis-4.0.10/bin/redis-quit

自动启动

添加自动启动脚本

[root@tongfunet]# cat > /lib/systemd/system/redis.service <<EOF
[Unit]
Description=redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/tongfu.net/env/redis-4.0.10/bin/redis-start
ExecStop=/tongfu.net/env/redis-4.0.10/bin/redis-quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

使用 systemctl 管理 redis 服务

systemctl enable redis # 设置自动启动

systemctl start redis # 启动服务

systemctl stop redis # 停止服务

systemctl restart redis # 重启服务

安装其他工具

安装ssh

yum -y install openssh openssh-clients

安装rsync

yum -y install rsync

安装crontabs

yum -y install crontabs

安装sshd

安装ssh服务器端

yum -y install openssh-server

开机自动启动

systemctl enable sshd

加入系统命令

svn

ln -s /tongfu.net/env/subversion-1.10.2/bin/svn /usr/sbin/svn

php

ln -s /tongfu.net/env/php-7.2.8/bin/php /usr/sbin/php

mysql & mysqldump

ln -s /tongfu.net/env/mysql-5.7.22/bin/mysql /usr/sbin/mysql
ln -s /tongfu.net/env/mysql-5.7.22/bin/mysqldump /usr/sbin/mysqldump

redis-cli

ln -s /tongfu.net/env/redis-4.0.10/bin/redis-cli /usr/sbin/redis-cli

附录

问题Nginx添加ssl支持

已经安装好了的nginx发现没有安装ssl模块,这可怎么办?

nginx: [emerg] unknown directive "ssl" in

解决

下载同样版本的nginx源码包,解压缩

wget http://xxxx/...../nginx-1.12.2.tar.gz
tar xzvf nginx-1.12.2.tar.gz
cd nginx-1.12.2

查看运行着的nginx的编译参数(果然没有开启ssl)

> /tongfu.net/env/nginx-1.12.2/sbin/nginx -V

nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
configure arguments: --prefix=/tongfu.net/env/nginx-1.12.2

添加ssl选项,重新编译

./configure --prefix=/tongfu.net/env/nginx-1.12.2 \
--with-http_ssl_module

执行make,不要执行make install,切记切记~~

make

使用新的nginx覆盖老的nginx,只覆盖一个文件

cp -f objs/nginx /tongfu.net/env/nginx-1.12.2/sbin/nginx

现在就可以使用新的nginx添加ssl了

设置时区

CentOS 7.x

设置时区

timedatectl set-timezone Asia/Shanghai

开启NTP

timedatectl set-ntp yes

关闭RTC

timedatectl set-local-rtc no

CentOS 6.x

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

服务命令加入系统目录

apachectl

ln -s /tongfu.net/env/httpd-2.4.37/bin/apachectl /usr/bin/

mysql

ln -s /tongfu.net/env/mysql-5.7.22/bin/mysql /usr/bin/

mysqldump

ln -s /tongfu.net/env/mysql-5.7.22/bin/mysqldump /usr/bin/

mysqladmin

ln -s /tongfu.net/env/mysql-5.7.22/bin/mysqladmin /usr/bin/

nginx

ln -s /tongfu.net/env/nginx-1.12.2/sbin/nginx /usr/bin/

redis-cli

ln -s /tongfu.net/env/redis-4.0.10/bin/redis-cli /usr/bin/

自制ssl证书

自助创建ssl证书文件

创建 server.key 文件

openssl genrsa -des3 -out server.key 1024

创建 server.csr 文件,有五项信息必须填写

命令

openssl req -new -key server.key -out server.csr -config /etc/pki/tls/openssl.cnf

必填项

Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:District east
Organization Name (eg, company) [Default Company Ltd]:TONGFU.net
Common Name (eg, your name or your server's hostname) []:tongfu.net

创建 ca.crt 和 ca.key 文件,有五项信息必须填写

命令

openssl req -new -x509 -keyout ca.key -out ca.crt -config /etc/pki/tls/openssl.cnf

必填项

Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:District east
Organization Name (eg, company) [Default Company Ltd]:TONGFU.net
Common Name (eg, your name or your server's hostname) []:tongfu.net

如果没有文件 /etc/pki/CA/index.txt 就创建一个

touch /etc/pki/CA/index.txt

如果没有文件 /etc/pki/CA/serial 就创建一个

echo "00" > /etc/pki/CA/serial

创建 server.crt 文件

openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key -config /etc/pki/tls/openssl.cnf

最后会生成如下文件

ca.crt  ca.key  server.crt  server.csr  server.key

创建免密key文件

创建后使用server.key.unsecure就可以在重启web服务的时候不需要输入密码了

openssl rsa -in server.key -out server.key.unsecure

Apache开启SSL

获取证书

可以去腾讯云,阿里云,百度云去申请ssl证书

配置

去掉 ssl 模块和 shmcb 模块前面的 #

LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

将 Include conf/extra/httpd-ssl.conf 前面的 # 去掉

> vi /tongfu.net/env/httpd-2.4.27/conf/httpd.conf

# Secure (SSL/TLS) connections
Include conf/extra/httpd-ssl.conf

一般情况下我们会得到3个证书文件,把它们都上传到服务器的 /tongfu.net/env/httpd-2.4.27/conf/ 目录下面


双击其中一个看到颁发给后面是自己域名的是【第一个证书文件】


另外一个颁发给后面不是域名的是【第二个证书文件】


最后一个非证书类型的就是【公钥文件】

tongfu.net.key

打开 httpd-ssl.conf 文件,按顺序把三个文件的路径写进去

> vi /tongfu.net/env/httpd-2.4.27/conf/extra/httpd-ssl.conf

#   Server Certificate:
#   Point SSLCertificateFile at a PEM encoded certificate.  If
#   the certificate is encrypted, then you will be prompted for a
#   pass phrase.  Note that a kill -HUP will prompt again.  Keep
#   in mind that if you have both an RSA and a DSA certificate you
#   can configure both in parallel (to also allow the use of DSA
#   ciphers, etc.)
#   Some ECC cipher suites (http://www.ietf.org/rfc/rfc4492.txt)
#   require an ECC certificate which can also be configured in
#   parallel.
SSLCertificateFile "/tongfu.net/env/httpd-2.4.27/conf/【第一个证书文件】"

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
#   ECC keys, when in use, can also be configured in parallel
SSLCertificateKeyFile "/tongfu.net/env/httpd-2.4.27/conf/【公钥文件】"

#   Server Certificate Chain:
#   Point SSLCertificateChainFile at a file containing the
#   concatenation of PEM encoded CA certificates which form the
#   certificate chain for the server certificate. Alternatively
#   the referenced file can be the same as SSLCertificateFile
#   when the CA certificates are directly appended to the server
#   certificate for convenience.
SSLCertificateChainFile "/tongfu.net/env/httpd-2.4.27/conf/【第二个证书文件】"

打开 httpd-ssl.conf 文件设置 VirtualHost

DocumentRoot /xxx/
ServerName tongfu.net

重启

重启 httpd

systemctl restart httpd

测试

现在我们就可以通过 https 访问网站了!

通过浏览器打开网址 https://yourdomain/ 试试看吧!

Apache开启Status

开启apache的status就可以通过特殊地址来查看apache的运行状态了

配置

去掉 status 模块前面的 #

LoadModule status_module modules/mod_status.so

在指定的虚拟主机下配置路径

这里推荐放到内部端口的虚拟主机下面


Nginx开启SSL

获取证书

可以去腾讯云,阿里云,百度云去申请ssl证书

配置

一般情况下我们会得到2个证书文件,把它们都上传到服务器的 /tongfu.net/env/nginx-1.12.2/conf/ 目录下


双击可以看到颁发给后面是自己的域名的就是【证书文件】


另一个非证书类型的文件就是【公钥文件】

打开 nginx.conf 设置证书参数

server {
    listen       443;
    server_name  yourdomain;
    ssl                  on;
    ssl_certificate      [证书文件];
    ssl_certificate_key  [公钥文件];
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    location / {
        proxy_pass              http://127.0.0.1:80/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Ip $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

重启

重启 nginx

systemctl restart nginx

测试

注意:如果 nginx 作为代理服务器使用,则源站(Apache等等)就不要再做ssl配置了!!!

Samba使用

共享Linux目录给Windows使用

在Linux上创建一个用于共享目录的用户

useradd [共享用户名称]

选择一个目录授权给上面创建的用户

chown [共享用户名称].[共享用户名称] [linux 共享目录]

在samba配置文件里添加共享设置

> vi /etc/samba/smb.conf

[[linux 共享目录别名]]
        comment = share folder in CentOS 7 Work
        path = /data/[linux 共享目录]
        writeable = yes
        browseable = yes
        ready only = no
        valid users = [共享用户名称]

在samba里添加一遍共享用户,并设置用户密码

smbpasswd -a [共享用户名称]

重启samba服务

systemctl restart smb

查看共享目录列表

smbclient -L //[linux IP地址] -U [共享用户名称]

这样我们就可以用Windows访问Linux的共享目录了!

Linux挂载Windows共享目录

在Windows上创建用于共享目录的用户

选择一个目录共享给上面创建的用户

安装samba client

yum -y install samba-client

在Linux上查看Windows共享目录列表

smbclient -L //[windows IP地址] -U [共享用户名称]

挂载共享目录

选项里的 rw 表示可读写权限

选项里的 uid 和 gid 表示挂载后的所有者ID和用户组ID,默认是 root

mount -t cifs //[windows IP地址]/[windows 共享目录] /mnt/[linux 挂载目录] -o username=[共享用户名称],password=[共享用户密码],rw,uid=[挂载目录对应linux所有者],gid=[挂载目录对应linux所属组]

这样我们就可以用Linux服务Windows的共享目录了!

注意:不要尝试在Docker内使用mount挂载Windows共享文件夹,因为它永远都会提示你permission denied,且没有任何错误码

PHP7兼容

preg_replace

php7里使用preg_replace替换的时候,不能使用 /e 修饰符,其实我们一般情况下也不会用。但是Smarty到处都是,OMG!

<b>Warning</b>:  preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead

define

使用define自定义常量的时候必须将常量名称用引号括起来,否则会被视为引用常量而告警。

(大概是为了优化解析速度,因为不加引号的一律视为常量引用)

Use of undefined constant RESTYPE_IMAGE - assumed 'XXX'

完结

现在我们就可以通过 https 访问网站了!

通过浏览器打开网址 https://yourdomain/ 试试看吧!

鬼谷子叔叔

跟福哥学编程吧~~
日志
212
浏览
1626