lnmp环境搭建

安装依赖库

mkdir -p /Data/tgz
cd /Data/tgz

yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

ulimit -SHn 65535

安装nginx

wget http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-8.38.tar.gz

tar zxf pcre-8.38.tar.gz

cd pcre-8.38

./configure -prefix=/Data/app/pcre

make && make install

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

tar zxf nginx-1.12.2.tar.gz

cd nginx-1.12.2

./configure  --user=www --group=www --prefix=//Data/app//nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --with-stream --with-http_image_filter_module

make

make install

cd ../

安装Mysql

yum install -y libaio

wget http://downloads.mysql.com/archives/mysql-5.6/mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz

tar zxvf mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz

mv mysql-5.6.10-linux-glibc2.5-x86_64 /Data/app/mysql

安装PHP依赖库

编译安装PHP库

wget http://www.ijg.org/files/jpegsrc.v9.tar.gz

tar zxvf jpegsrc.v9.tar.gz

cd jpeg-9/

./configure --prefix=/Data/app/libs --enable-shared --enable-static

make

make install

cd ../

wget http://prdownloads.sourceforge.net/libpng/libpng-1.6.2.tar.gz

tar zxvf libpng-1.6.2.tar.gz

cd libpng-1.6.2/

./configure --prefix=/Data/app/libs

make

make install

cd ../

wget http://download.savannah.gnu.org/releases/freetype/freetype-2.4.12.tar.gz

tar zxvf freetype-2.4.12.tar.gz

cd freetype-2.4.12/

./configure --prefix=/Data/app/libs

make

make install

cd ../../

编译PHP 7.2

export LIBS="-lm -ltermcap -lresolv"
export DYLD_LIBRARY_PATH="/Data/app/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"
export LD_LIBRARY_PATH="/Data/app/mysql/lib/:/lib/:/usr/lib/:/usr/local/lib:/lib64/:/usr/lib64/:/usr/local/lib64"

wget https://www.php.net/distributions/php-7.2.20.tar.gz
tar zxf php-7.2.20.tar.gz
cd php-7.2.20

./configure \
--prefix=/Data/app/php7 \
--with-config-file-path=/Data/app/php7/etc \
--disable-cgi \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-phpdbg \
--enable-phpdbg-webhelper \
--disable-debug \
--disable-phpdbg \
--enable-mysqlnd \
--enable-bcmath \
--enable-calendar \
--enable-fpm \
--with-jpeg-dir=/Data/app/libs \
--with-png-dir=/Data/app/libs \
--with-iconv \
--enable-intl \
--enable-mbstring \
--with-openssl \
--enable-pcntl \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-png-dir=/Data/app/libs \
--with-curl=/usr/include/curl \
--with-recode \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-zip \
--enable-xml \
--disable-fileinfo  \
--enable-maintainer-zts \
--disable-rpath

make && make install

复制php配置文件

cp php.ini-production /Data/app/php7/etc/php.ini
mv /Data/app/php7/etc/php-fpm.conf.default /Data/app/php7/etc/php-fpm.conf

开启PHP错误日志和慢日志

php-fpm.conf修改

error_reporting = E_ALL
display_errors = On
short_open_tag = On
date.timezone = Asia/Shanghai 
error_log=/Data/app/php7/var/log/php_error.log

编译安装PHP扩展

redis扩展

3.0只支持php7

wget https://github.com/phpredis/phpredis/archive/3.1.5.tar.gz

tar zxf 2.2.8.tar.gz

cd phpredis-2.2.8

/Data/app/php/bin/phpize

./configure –with-php-config=/Data/app/php/bin/php-config

make

make install 

加载redis扩展

echo extension=redis.so >> /Data/app/php/etc/php.ini

cd ../

安装composer

php -r  "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php –install-dir=/Data/app/php/bin –filename=composer
php -r "unlink('composer-setup.php');"

设置从中国镜像下载

composer config -g repo.packagist composer https://packagist.phpcomposer.com

安装Redis

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

tar zxf redis-3.2.5.tar.gz

cd redis-3.2.5/src

make

mkdir -p /Data/app/redis/etc

mkdir -p /Data/app/redis/bin

mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb /Data/app/redis/bin

mv /Data/tgz/redis-3.2.5/redis.conf /Data/app/redis/etc

cd ../..

安装Memcached

wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz

tar zxf libevent-2.0.22-stable.tar.gz

cd libevent-2.0.22-stable

./configure

make && make install

cd ..
wget http://www.memcached.org/files/memcached-1.4.33.tar.gz

tar zxf memcached-1.4.33.tar.gz

cd memcached-1.4.33

./configure -prefix=/Data/app/memcached

make 

make install

cd ..

把命令路径加入到PATH环境变量中

export PATH=/Data/app/php7/bin:/Data/app/php7/sbin:/Data/app/mysql/bin:/Data/app/nginx/sbin:/Data/app/redis/bin:$PATH

配置Nginx

创建一个目录用来保存自定义的虚拟主机配置文件。

mkdir /Data/app/nginx/conf/vhost
include vhost/*.conf;

创建一个php的网站


server {

listen 80;

server_name  www.test.com;

index index.php index.html index.htm;

root /www;
location / {
    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php/$1 last; #pathinfo
    }
}

location ~ \.php {
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi.conf;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
    expires      30d;
}

location ~ .*\.(js|css)?$
{
    expires      1h;
}
}

Mysql配置

初始化操作

/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql
mkdir  -p /Data/database/mysql

/Data/app/mysql/scripts/mysql_install_db --basedir=/Data/app/mysql --datadir=/Data/database/mysql --user=mysql
sed -i "s#/usr/local/mysql#/Data/app/mysql#g" /Data/app/mysql/bin/mysqld_safe

修改配置文件

/etc/my.conf

[mysqld]
datadir=/Data/database/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mysql.log
pid-file=/var/run/mariadb/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

创建目录

mkdir /var/lib/mysql
chown mysql:mysql /var/lib/mysql

mkdir /var/run/mariadb/
chown mysql:mysql /var/run/mariadb/

mkdir /var/log/mariadb
chown mysql:mysql /var/log/mariadb

开启mysql服务

ln -s /var/lib/mysql/mysql.sock /tmp

/Data/app/mysql/bin/mysqld_safe &

设置密码

/Data/app/mysql/bin/mysqladmin -u root password 'new-password'

注册Mysql服务

Mysql安装目录下的 support-files/mysql.server 是mysql启动、关闭的脚本。

需要修改的配置:

  1. 安装路径不是 /usr/local/mysql 就需要修改 basedir= 配置
  2. 数据目录不是 /usr/local/mysql/data 就需要修改 datadir= 配置
  3. 修改pid文件路径, mysqld_pid_file_path="$basedir/var/run/mysql.pid"
  4. 复制该文件到 /etc/init.d/中,文件名为mysqld
  5. 创建一个mysql.sock 的软连接到 /tmp中,默认在安装目录中的 /var/run/mysql.sock中
basedir=/Data/app/mysql
datadir=/Data/database/mysql
 mysqld_pid_file_path="/var/run/mariadb/mariadb.pid"

复制脚本到 /etc/init.d 目录下

启动命令 service mysqld start

注册为服务

chkconfig --add mysqld
chkconfig --level 2345 mysqld on

安全配置

安装完mysql-server 会提示可以运行/Data/app/mysql/bin/mysql_secure_installation

运行mysql_secure_installation会执行几个设置:
a)为root用户设置密码

b)删除匿名账号

c)取消root用户远程登录

d)删除test库和对test库的访问权限

e)刷新授权表使修改生效
通过这几项的设置能够提高mysql库的安全。建议生产环境中mysql安装这完成后一定要运行一次mysql_secure_installation

Mysql错误

使用 mysqld_safe 启动mysql有警告错误

[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

其原因是从 5.6开始,timestamp 的默认行为已经是 deprecated 了。

在MySQL 5.6.6之前,TIMESTAMP的默认行为:
TIMESTAMP列如果没有明确声明NULL属性,默认为NOT NULL。(而其他数据类型,如果没有显示声明为NOT NULL,则允许NULL值。)设置TIMESTAMP的列值为NULL,会自动存储为当前timestamp。

表中的第一个TIMESTAMP列,如果没有声明NULL属性、DEFAULT或者 ON UPDATE,会自动分配 DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 属性。

表中第二个TIMESTAMP列,如果没有声明为NULL或者DEFAULT子句,默认自动分配'0000-00-00 00:00:00′。插入行时没有指明改列的值,该列默认分配'0000-00-00 00:00:00′,且没有警告。

要关闭警告,需要加入下面的参数:

[mysqld]
explicit_defaults_for_timestamp=true

重启MySQL后错误消失,这时TIMESTAMP的行为如下:
TIMESTAMP如果没有显示声明NOT NULL,是允许NULL值的,可以直接设置改列为NULL,而没有默认填充行为。

TIMESTAMP不会默认分配DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP属性。

声明为NOT NULL且没有默认子句的TIMESTAMP列是没有默认值的。往数据表中插入列,又没有给TIMESTAMP列赋值时,如果是严格SQL模式,会抛出一 个错误,如果严格SQL模式没有启用,该列会赋值为'0000-00-00 00:00:00′,同时出现一个警告。(这和MySQL处理其他时间类型数据一样,如DATETIME)

(参见:http://www.williamsang.com/archives/818.html)
也就是 explicit_defaults_for_timestamp 关闭了 timestamp 类型字段锁拥有的一些会让人感到奇怪的默认行为,加入了该参数之后,如果还需要为 timestamp类型的字段指定默认行为,那么就需要显示的在创建表时显示的指定。explicit_defaults_for_timestamp 也就是这个意思:显示指定默认值为timestamp类型的字段。


配置Zsh

yum install zsh

安装 oh my zsh

sh -c  "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

你可能感兴趣的:(lnmp环境搭建)