lnmp服务器源码编译
一、 安装前准备
1、 配置好IP、DNS、网关,确保xshell可以远程连接到linux服务器
2、 如果是centos7以上版本(自带firewall)
Systemctl stop firewalld.service #停止firewall
Systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd –state #查看centos7以上防火墙状态
3、 配置防火墙,开启80端口、3306端口、8088端口
Vi /etc/sysconfig/iptable
-A INPUT –m state NEW –m tcp –p tcp –dport 80 –j ACCEPT #允许80端口通过防火墙
-A INPUT –m state NEW –m tcp –p tcp –dport 8088 –j ACCEPT #允许8088端口通过防火墙
-A INPUT –m state NEW –m tcp –p tcp –dport 3306 –j ACCEPT #允许3306端口通过防火墙
/etc/init.d/iptables restart #重启防火墙使配置生效
4、 关闭selinux
Vi /etc/selinux/config
注释 SELINUX=enforcing
注释 SELINUXTYPE=targeted
增加 SELINUX=disabled
Wq 保存 关闭
shutdown –r now #重启系统
5、 系统约定
软件源代码包存放位置:/usr/local/src
源码包编译安装位置: /usr/local/软件名字
数据存放目录:挂载盘
二、 编译安装nginx
1、 更新yum安装源
yum intall epel-release –y
yum update
2、 安装依赖
yum install -y gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel
3、 编译安装nginx
编译安装命令:
./configure --user=www --group=www --prefix=/usr/local/nginx/ --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-http_realip_module make && make install cd /usr/local/nginx/sbin测试以及启动
Cd /usr/local/nginx/sbin #进入nginx启动文件
./nginx #启动 ./nginx –s reload #重启nginx ./nginx –s stop停止nginx
./nginx –t #验证配置文件是否正确
测试是否允许:
Ps –ef|grep nginx
Linux下本地测试:
Curl localhost
浏览器测试:
三、 编译安装mysql
1、 安装cmake(作为mysql的安装工具)
Tar –zxvf cmake-3.8.0.tar.gz
Cd cmake-3.8.0
./bootstrap
Gmake
Gmake install
2、 安装boost_1_59_0
Tar –zxvf boost_1_59_0.tar.gz
Cd boost_1_59_0
./bootstrap.sh
./b2
./b2 install
3、 创建用户、配置目录
添加mysql用户和所属组
/usr/sbin/groupadd mysql
/usr/sbin/useradd –g mysql mysql
创建mysql安装目录和数据目录
Mkdir /usr/local/mysql
Mkdir /usr/local/mysql/data
修改mysql目录所有者
Chown –R mysql:mysql /usr/local/mysql
4、 yum –y install ncurses-devel
5、 编译安装mysql
(编译命令在同一行,否则会出现找不到目录的情况)
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/src/boost_1_59_0 -DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0
make
make install
6、 配置mysql并初始化数据库
配置文件:cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
初始化数据库:
/usr/local/mysql/bin/mysqld –initialize –user=mysql –basedire=/usr/local/mysql –datadir=/usr/local/mysql/data
启动mysql:( f9haELW!fjQo)
/usr/local/mysql/bin/mysqld_safe –defaults-file=/etc/my.cnf –basedir=/usr/local/mysql --datadir=/usr/local/mysql/data –user=mysql &
建立软链接:
Ln –s /usr/local/mysql/bin/mysql /usr/bin
修改密码:
进入mysql命令行
Set password = password(‘sixstar123’);
Alter user ‘root’@’localhost’ password expire never;
Flush privileges;
设置开机启动:
Cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #复制到启动目录
Chkconfig mysqld on #设置开机启动
Chkconfig –list|grep mysqld 查看开机选项
四、 PHP编译安装
1、 依赖包更新
Yum install epel-release –y
Yum update
2、 依赖扩展安装
yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
3、 创建运行用户和建立软链接
3.1、创建运行用户
Groupadd www
Useradd –g www www
3.2、建立软链接
Cp –frp /usr/lib64/libldap* /usr/lib/
Ln –s /usr/local/lib/libiconv.so.2 /usr/lib64/
4、 编译PHP
编译命令:
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-mysqlnd-compression-support \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--with-mcrypt \
--with-libmbfl \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--with-pear \
--enable-maintainer-zts \
--with-ldap=shared \
--without-gdbm \
make -j 4 (如果编译出现错误使用这个编辑命令代替 make ZEND_EXTRA_LIBS='-liconv')
make install
配置php.ini文件
cp php.ini-development /usr/local/php/etc/php.ini
$ cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
$ cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
修改php.ini相关参数
vi /usr/local/php/etc/php.ini
expose_php = Off
short_open_tag = ON
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M
date.timezone = Asia/Shanghai
mbstring.func_overload=2
extension = "/usr/local/php/lib/php/extensions/no-debug-zts-20160303/ldap.so"
设置 OPcache 缓存:
[opcache]
zend_extension=/usr/local/php/lib/php/extensions/no-debug-zts-20160303/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
设置php安全函数:
$ vim /usr/local/php/etc/php.ini
默认值:
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,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_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,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
或通配:
disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru
配置www.conf
取消以下注释并修改优化其参数:
listen = /var/run/www/php-cgi.sock
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
listen.backlog = -1
pm.max_children = 180
pm.start_servers = 50
pm.min_spare_servers = 50
pm.max_spare_servers = 180
request_terminate_timeout = 120
request_slowlog_timeout = 50
slowlog = var/log/slow.log
创建php-cgi.sock存放目录
$ mkdir /var/run/www/
$ chown -R www:www /var/run/www
配置php-fpm.conf
取下以下注释并填写完整路径:
pid = /usr/local/php/var/run/php-fpm.pid
· 运行php-fpm
/usr/local/php/sbin/php-fpm
· 将php-fpm加入全局
vi /root/.bash_profile#将 /usr/local/php/bin 加到后面,用:隔开
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/lcoal/php/bin#重启
source /root/.bash_profile
· 增加php-fpm启动脚本
cp /temp/php-7.1.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm#修改权限
cd /etc/init.d
chmod +x php-fpm