Centos 系统 LNMP环境源码编译安装以及配置

一:环境介绍

1.操作系统:Centos 7.* ( 双核4G、硬盘至少20G)
2.应用软件:nginx-1.8.1、mysql-5.6.29、php-5.6.30

二:源码编译安装配置nginx、mysql、php

1.源码编译安装nginx-1.8.1

官网下载nginx-1.8.1.tar.gz

http://nginx.org/en/download.html

下载pcre-8.40.tar.gz

https://ftp.pcre.org/pub/pcre/

yum安装依赖包

yum install perl gcc-c++ make elinks zlib-devel openssl openssl-devel -y

pcre 解压即可

tar -zxf pcre-8.4.0.tar.gz

开始编译安装nginx-1.8.1

tar -zxf nginx-1.8.1.tar.gz

cd nginx-1.8.1

./configure --prefix=/usr/local/nginx --with-pcre=/root/pcre-8.40 --with-http_stub_status_module --with-http_ssl_module

make

make install

修改nginx主配置文件,让其包含/usr/local/nginx/conf/conf.d下的所有*.conf文件

mkdir /usr/local/nginx/conf/conf.d

sed -i '116s/$/include \/usr\/local\/nginx\/conf\/conf.d\/*.conf;/g' /usr/local/nginx/conf/nginx.conf

至此nginx安装完毕

启动nginx

/usr/local/nginx/sbin/nginx

查看80端口

netstat -ntpl |grep "80"

关闭防火墙/etc/init.d/iptables stop 或者防火墙打开80端口

(关闭nginx)

killall nginx
2.源码编译安装mysql-5.6.29

官网下载mysql-5.6.29.tar.gz

https://downloads.mysql.com/archives/community/

下载cmake-3.4.0

https://cmake.org/files/v3.4/cmake-3.4.0.tar.gz

yum安装依赖包

yum install gcc-c++ ncurses-devel perl-Module-Install.noarch libtool openssl-devel make -y

安装cmake

tar -zxf cmake-3.4.0.tar.gz

cd cmake-3.4.0

./configure && make && make install

cmake安装完毕

开始编译安装mysql-5.6.29

tar -zxf mysql-5.6.29.tar.gz

cd mysql-5.6.29

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/usr/local/mysql/data -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysqld.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DWITH_EXTRA_CHARSETS=all -DWITH_DEBUG=0 -DENABLE_DEBUG_SYNC=0 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_READLINE=1 -DZLIB_INCLUDE_DIR=/usr -DWITH_READLINE=1
make
make install

创建mysql系统用户组和用户,并将mysql安装目录赋予root组和root用户

groupadd mysql

useradd -g mysql -s /sbin/nologin -M mysql

chown mysql.mysql -R /usr/local/mysql

初始化mysql数据库

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

复制mysql启动脚本

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

直接执行修改文件

sed -i '46s/$/\/usr\/local\/mysql/g' /etc/init.d/mysqld
sed -i '47s/$/\/usr\/local\/mysql\/data/g' /etc/init.d/mysqld
sed -i '263s/datadir/basedir/g' /etc/init.d/mysqld

写入mysql配置文件my.cnf

echo "[mysqld]

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysqld.sock
user = mysql
port = 3306
server_id = 2
#log-bin = mysql-bin
#log_bin_index = binlog.index
character_set_server = utf8
#lower_case_table_names = 1
#binlog_ignore_db = mysql
#replicate-do-db = mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
innodb_file_per_table=1

[mysql.server]
character_set_server = utf8
socket=/usr/local/mysql/mysqld.sock

[client]
socket=/usr/local/mysql/mysqld.sock
default-character-set = utf8

[mysqld_safe]
character_set_server = utf8

[mysql]
socket=/usr/local/mysql/mysqld.sock
default-character-set = utf8


[mysqldump]
socket=/usr/local/mysql/mysqld.sock
default-character-set = utf8


[mysqladmin]
socket=/usr/local/mysql/mysqld.sock
character_set_server = utf8
" > /usr/local/mysql/my.cnf

将mysql加入centos系统环境变量
vim /etc/profile

echo -e "export MYSQL_HOME=\"/usr/local/mysql\"
export PATH=\"\$PATH:\$MYSQL_HOME/bin\"" >> /etc/profile

刷新环境变量

source /etc/profile

至此mysql安装完毕

启动mysql

/etc/init.d/mysqld start

查看3306端口

netstat -ntpl |grep "3306"

(停止或重启mysql)

/etc/init.d/mysqld stop

/etc/init.d/mysqld restart

shell界面进入mysql 删除默认的多余root账户

mysql -uroot -p


delete from mysql.user where Host='::1';
delete from mysql.user where Host='localhost.localdomain';
delete from mysql.user where User='';

将所有的root用户更改密码

update mysql.user set password=password("root") where user="root";

创建一个可以从其他任何地方访问mysql的用户 密码为root

grant all privileges on *.* to 'root'@'%' identified by "root";

对用户进行增删改后需要重启数据库或者执行flush privileges;

查看mysql用户

select user,host,password from mysql.user;
3.源码编译安装php-5.6.30

官网下载php-5.6.30

http://php.net/downloads.php#v5.6.30

yum安装依赖包

yum install gcc-c++ gd libxml2-devel libjpeg-devel libpng-devel net-snmp-devel curl-devel libxslt-devel pcre-devel libjpeg libpng libxml2 libcurl4-openssl-dev libcurl-devel libcurl freetype-config freetype freetype-devel unixODBC libxslt -y

开始编译安装php-5.6.30

tar -zxf php-5.6.30.tar.gz

cd php-5.6.30

./configure --prefix=/usr/local/php-5.6.30 --with-curl --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysql --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-freetype-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml && make && make install

从php源码包复制php.ini配置文件到php安装目录,并做系统的软连接

cp -a /root/php-5.6.30/php.ini-production /usr/local/php-5.6.30/etc/php.ini
ln -s /usr/local/php-5.6.30/etc/php.ini /etc/php.ini

从php源码包复制php-fpm.conf配置文件到php安装目录,并做系统的软连接

cp -a /usr/local/php-5.6.30/etc/php-fpm.conf.default /usr/local/php-5.6.30/etc/php-fpm.conf
ln -s /usr/local/php-5.6.30/etc/php-fpm.conf /etc/php-fpm.conf

修改php-fpm.conf配置参数

sed -i '25s/;//g' /usr/local/php-5.6.30/etc/php-fpm.conf

sed -i '303s/disable_functions =/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status, ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,esca peshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,p osix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid, posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,pos ix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkf ifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posi x_times,posix_ttyname,posix_uname/g' /usr/local/php-5.6.30/etc/php.ini

sed -i '936s/;date.timezone =/date.timezone = PRC/g' /usr/local/php-5.6.30/etc/php.ini

sed -i '151s/; short_open_tag/short_open_tag = ON/g' /usr/local/php-5.6.30/etc/php.ini

sed -i '1905s/;opcache.enable=0/opcache.enable=1/g' /usr/local/php-5.6.30/etc/php.ini

sed -i '1908s/;opcache.enable_cli=0/opcache.enable_cli=0/g' /usr/local/php-5.6.30/etc/php.ini

sed -i '$a zend_extension=opcache.so' /usr/local/php-5.6.30/etc/php.ini

sed -i '509s/;env[PATH] = /usr/local/bin:/usr/bin:/bin/env[PATH] = /usr/local/bin:/usr/bin:/bin/:/usr/local/php5.6.30/bin/g' /usr/local/php-5.6.30/etc/php-fpm.conf

复制php启动脚本

cp -a /root/php-5.6.30/sapi/fpm/php-fpm /etc/init.d/

启动php

/etc/init.d/php-fpm

查看9000端口

netstat -ntpl |grep "9000"

(停止php)

killall php-fpm

测试nginx+php

mkdir /opt/test

echo "<?php
Phpinfo();
?>" > /opt/test/index.php

nginx配置

vim /usr/local/nginx/conf/conf.d/test.conf

server {
        listen          80;
        server_name     192.168.1.2;
        index           index.php;
        root            /opt/test;

        location /
        {
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ ^(.+.php)(.*)$ {
                fastcgi_split_path_info ^(.+.php)(.*)$;
                include fastcgi.conf;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param  PATH_INFO          $fastcgi_path_info;
        }
}

浏览器输入192.168.1.2查看php页面

觉得麻烦的同学可以私聊我给予帮助,或者提供脚本。

推荐阅读更多精彩内容